Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Andy Tallent
 
Posts: n/a
Default Mailing an excel range in the body of an e-mail

Dear All,

I am using a piece of code I located on Ron de Bruins excellent site that
allows for a range in excel to be mailed within the body of an outlook
message. The code works beautifully but does not return any value for the
ComboBoxes I have within the range. Is it possible for these to be visible
as well in the e-mail message? I am including the code for reference.

Sub Mail_Selection_Outlook_Body2()
' You must add a reference to the Microsoft outlook Library
' Don't forget to copy the function RangetoHTML2 in the module.
' Is not working in Office 97
Dim source As Range
Dim dest As Workbook
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Set source = Nothing
On Error Resume Next
Set source = Selection.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If source Is Nothing Then
MsgBox "The selection is not a range or the sheet is protect" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If

If ActiveWindow.SelectedSheets.Count 1 Or _
Selection.Cells.Count = 1 Or _
Selection.Areas.Count 1 Then
MsgBox "An Error occurred :" & vbNewLine & vbNewLine & _
"You have more than one sheet selected." & vbNewLine & _
"You only selected one cell." & vbNewLine & _
"You selected more than one area." & vbNewLine & vbNewLine & _
"Please correct and try again.", vbOKOnly
Exit Sub
End If

Application.ScreenUpdating = False
source.Copy
Set dest = Workbooks.Add(xlWBATWorksheet)
With dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
' Paste:=8 will copy the column width in Excel 2000 and higher
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML2
.Send 'or use .Display
End With

dest.Close False
Set OutMail = Nothing
Set OutApp = Nothing
Set dest = Nothing
Application.ScreenUpdating = True
End Sub



Public Function RangetoHTML2()
' You can't use this function in Excel 97
Dim fso As Object
Dim ts As Object
Dim TempFile As String
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") &
".htm"
With ActiveWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=ActiveSheet.Name, _
source:=ActiveSheet.UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML2 = ts.ReadAll
ts.Close
Set ts = Nothing
Set fso = Nothing
Kill TempFile
End Function



--
Andy Tallent
  #2   Report Post  
Ron de Bruin
 
Posts: n/a
Default

Hi andy

See the link on top of the example page to
the KB for Excel 2002-2003(If you use that)

--
Regards Ron de Bruin
http://www.rondebruin.nl


"Andy Tallent" wrote in message ...
Dear All,

I am using a piece of code I located on Ron de Bruins excellent site that
allows for a range in excel to be mailed within the body of an outlook
message. The code works beautifully but does not return any value for the
ComboBoxes I have within the range. Is it possible for these to be visible
as well in the e-mail message? I am including the code for reference.

Sub Mail_Selection_Outlook_Body2()
' You must add a reference to the Microsoft outlook Library
' Don't forget to copy the function RangetoHTML2 in the module.
' Is not working in Office 97
Dim source As Range
Dim dest As Workbook
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Set source = Nothing
On Error Resume Next
Set source = Selection.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If source Is Nothing Then
MsgBox "The selection is not a range or the sheet is protect" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If

If ActiveWindow.SelectedSheets.Count 1 Or _
Selection.Cells.Count = 1 Or _
Selection.Areas.Count 1 Then
MsgBox "An Error occurred :" & vbNewLine & vbNewLine & _
"You have more than one sheet selected." & vbNewLine & _
"You only selected one cell." & vbNewLine & _
"You selected more than one area." & vbNewLine & vbNewLine & _
"Please correct and try again.", vbOKOnly
Exit Sub
End If

Application.ScreenUpdating = False
source.Copy
Set dest = Workbooks.Add(xlWBATWorksheet)
With dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
' Paste:=8 will copy the column width in Excel 2000 and higher
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML2
.Send 'or use .Display
End With

dest.Close False
Set OutMail = Nothing
Set OutApp = Nothing
Set dest = Nothing
Application.ScreenUpdating = True
End Sub



Public Function RangetoHTML2()
' You can't use this function in Excel 97
Dim fso As Object
Dim ts As Object
Dim TempFile As String
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") &
".htm"
With ActiveWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=ActiveSheet.Name, _
source:=ActiveSheet.UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML2 = ts.ReadAll
ts.Close
Set ts = Nothing
Set fso = Nothing
Kill TempFile
End Function



--
Andy Tallent



  #3   Report Post  
Andy Tallent
 
Posts: n/a
Default

Hi Ron,

Thank you for this. I have just one further question. Is it possible to
format the text in the body of the mail (bold, underline etc) via the VB
script?
--
Andy Tallent


"Ron de Bruin" wrote:

Hi andy

See the link on top of the example page to
the KB for Excel 2002-2003(If you use that)

--
Regards Ron de Bruin
http://www.rondebruin.nl


"Andy Tallent" wrote in message ...
Dear All,

I am using a piece of code I located on Ron de Bruins excellent site that
allows for a range in excel to be mailed within the body of an outlook
message. The code works beautifully but does not return any value for the
ComboBoxes I have within the range. Is it possible for these to be visible
as well in the e-mail message? I am including the code for reference.

Sub Mail_Selection_Outlook_Body2()
' You must add a reference to the Microsoft outlook Library
' Don't forget to copy the function RangetoHTML2 in the module.
' Is not working in Office 97
Dim source As Range
Dim dest As Workbook
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Set source = Nothing
On Error Resume Next
Set source = Selection.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If source Is Nothing Then
MsgBox "The selection is not a range or the sheet is protect" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If

If ActiveWindow.SelectedSheets.Count 1 Or _
Selection.Cells.Count = 1 Or _
Selection.Areas.Count 1 Then
MsgBox "An Error occurred :" & vbNewLine & vbNewLine & _
"You have more than one sheet selected." & vbNewLine & _
"You only selected one cell." & vbNewLine & _
"You selected more than one area." & vbNewLine & vbNewLine & _
"Please correct and try again.", vbOKOnly
Exit Sub
End If

Application.ScreenUpdating = False
source.Copy
Set dest = Workbooks.Add(xlWBATWorksheet)
With dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
' Paste:=8 will copy the column width in Excel 2000 and higher
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML2
.Send 'or use .Display
End With

dest.Close False
Set OutMail = Nothing
Set OutApp = Nothing
Set dest = Nothing
Application.ScreenUpdating = True
End Sub



Public Function RangetoHTML2()
' You can't use this function in Excel 97
Dim fso As Object
Dim ts As Object
Dim TempFile As String
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") &
".htm"
With ActiveWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=ActiveSheet.Name, _
source:=ActiveSheet.UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML2 = ts.ReadAll
ts.Close
Set ts = Nothing
Set fso = Nothing
Kill TempFile
End Function



--
Andy Tallent




  #4   Report Post  
Ron de Bruin
 
Posts: n/a
Default

The body is the same as you see on the sheet if you use my example
http://www.rondebruin.nl/mail/folder3/mail4.htm

or if you use the KB example


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Andy Tallent" wrote in message ...
Hi Ron,

Thank you for this. I have just one further question. Is it possible to
format the text in the body of the mail (bold, underline etc) via the VB
script?
--
Andy Tallent


"Ron de Bruin" wrote:

Hi andy

See the link on top of the example page to
the KB for Excel 2002-2003(If you use that)

--
Regards Ron de Bruin
http://www.rondebruin.nl


"Andy Tallent" wrote in message
...
Dear All,

I am using a piece of code I located on Ron de Bruins excellent site that
allows for a range in excel to be mailed within the body of an outlook
message. The code works beautifully but does not return any value for the
ComboBoxes I have within the range. Is it possible for these to be visible
as well in the e-mail message? I am including the code for reference.

Sub Mail_Selection_Outlook_Body2()
' You must add a reference to the Microsoft outlook Library
' Don't forget to copy the function RangetoHTML2 in the module.
' Is not working in Office 97
Dim source As Range
Dim dest As Workbook
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Set source = Nothing
On Error Resume Next
Set source = Selection.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If source Is Nothing Then
MsgBox "The selection is not a range or the sheet is protect" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If

If ActiveWindow.SelectedSheets.Count 1 Or _
Selection.Cells.Count = 1 Or _
Selection.Areas.Count 1 Then
MsgBox "An Error occurred :" & vbNewLine & vbNewLine & _
"You have more than one sheet selected." & vbNewLine & _
"You only selected one cell." & vbNewLine & _
"You selected more than one area." & vbNewLine & vbNewLine & _
"Please correct and try again.", vbOKOnly
Exit Sub
End If

Application.ScreenUpdating = False
source.Copy
Set dest = Workbooks.Add(xlWBATWorksheet)
With dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
' Paste:=8 will copy the column width in Excel 2000 and higher
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML2
.Send 'or use .Display
End With

dest.Close False
Set OutMail = Nothing
Set OutApp = Nothing
Set dest = Nothing
Application.ScreenUpdating = True
End Sub



Public Function RangetoHTML2()
' You can't use this function in Excel 97
Dim fso As Object
Dim ts As Object
Dim TempFile As String
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") &
".htm"
With ActiveWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=ActiveSheet.Name, _
source:=ActiveSheet.UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML2 = ts.ReadAll
ts.Close
Set ts = Nothing
Set fso = Nothing
Kill TempFile
End Function



--
Andy Tallent






  #5   Report Post  
Junior Member
 
Posts: 10
Default

Hi I tried this same code. I can see a temporary excel sheet with cells which i needs to be copied . But in outlook I can see a empty mail with only the subject. I dont see the excel data in the body of the mail . Please let me know what might be the prob. Also can i check whether the html file is getting created in this .

Any suggestion might be really helpful


Thanks

Vasanth


Quote:
Originally Posted by Ron de Bruin View Post
The body is the same as you see on the sheet if you use my example
http://www.rondebruin.nl/mail/folder3/mail4.htm

or if you use the KB example


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Andy Tallent" wrote in message ...
Hi Ron,

Thank you for this. I have just one further question. Is it possible to
format the text in the body of the mail (bold, underline etc) via the VB
script?
--
Andy Tallent


"Ron de Bruin" wrote:

Hi andy

See the link on top of the example page to
the KB for Excel 2002-2003(If you use that)

--
Regards Ron de Bruin
http://www.rondebruin.nl


"Andy Tallent" wrote in message
...
Dear All,

I am using a piece of code I located on Ron de Bruins excellent site that
allows for a range in excel to be mailed within the body of an outlook
message. The code works beautifully but does not return any value for the
ComboBoxes I have within the range. Is it possible for these to be visible
as well in the e-mail message? I am including the code for reference.

Sub Mail_Selection_Outlook_Body2()
' You must add a reference to the Microsoft outlook Library
' Don't forget to copy the function RangetoHTML2 in the module.
' Is not working in Office 97
Dim source As Range
Dim dest As Workbook
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Set source = Nothing
On Error Resume Next
Set source = Selection.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If source Is Nothing Then
MsgBox "The selection is not a range or the sheet is protect" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If

If ActiveWindow.SelectedSheets.Count 1 Or _
Selection.Cells.Count = 1 Or _
Selection.Areas.Count 1 Then
MsgBox "An Error occurred :" & vbNewLine & vbNewLine & _
"You have more than one sheet selected." & vbNewLine & _
"You only selected one cell." & vbNewLine & _
"You selected more than one area." & vbNewLine & vbNewLine & _
"Please correct and try again.", vbOKOnly
Exit Sub
End If

Application.ScreenUpdating = False
source.Copy
Set dest = Workbooks.Add(xlWBATWorksheet)
With dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
' Paste:=8 will copy the column width in Excel 2000 and higher
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML2
.Send 'or use .Display
End With

dest.Close False
Set OutMail = Nothing
Set OutApp = Nothing
Set dest = Nothing
Application.ScreenUpdating = True
End Sub



Public Function RangetoHTML2()
' You can't use this function in Excel 97
Dim fso As Object
Dim ts As Object
Dim TempFile As String
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") &
".htm"
With ActiveWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=ActiveSheet.Name, _
source:=ActiveSheet.UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML2 = ts.ReadAll
ts.Close
Set ts = Nothing
Set fso = Nothing
Kill TempFile
End Function



--
Andy Tallent



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
mail merge from excel want day & month only CE Instructor Excel Discussion (Misc queries) 3 July 18th 05 05:13 PM
Excel Page Range Printing Problems SkyEyes Excel Discussion (Misc queries) 2 July 11th 05 08:18 PM
Link excel and word in mail merge without losing your data source? angie Excel Discussion (Misc queries) 5 July 1st 05 06:29 PM
Merge mail excel to word T. Rebouche Excel Discussion (Misc queries) 2 February 21st 05 11:05 PM
Excel range truncates when Pasted as Picture to PPT & Word Floyd Excel Discussion (Misc queries) 2 January 28th 05 04:21 AM


All times are GMT +1. The time now is 03:52 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"