PDA

View Full Version : [SOLVED] sending email



megtoma
08-22-2014, 03:31 AM
Hello,
Can anyone help me to create a macro which would send the content of sheet2 as the body of an e-mail message?

Many thanks in advance.

megtoma
08-22-2014, 03:55 AM
I found the following code:
Sub Mail_Selection_Range_Outlook_Body()
' You need to use this module with the RangetoHTML subroutine.
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

Set rng = Nothing
On Error Resume Next
' Only send the visible cells in the selection.
Set rng = Selection.SpecialCells(xlCellTypeVisible)
' You can also use a range with the following statement.
' Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "ron@debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML(rng)
.Display
End With
On Error GoTo 0

With Application
.EnableEvents = True
.ScreenUpdating = True
End With

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


However, it does not work for me...the content is on the Sheet 1 in my file

mancubus
08-22-2014, 04:44 AM
please use code tags (# button) when posting code.

1 that procedure uses another macro, named RangetoHTML, which must be included in the module. refer to the web page, rdb.nl, you copied the code from.
2 it uses selected cells as mail body. so change that bit of the code or select desired cells.
3 as it is easy to see these, i recommend you study the basics of macro and vba programming first, to be able to adopt and modify the existing macros available on the web sites, mostly help forums.

cheers