Avatar of DavidH7470
DavidH7470
Flag for United States of America asked on

Marco to create an email from Excel

Hello:

I use the following macro to insert cells from a sheet in a workbook and create an email.  I wish to add an attachment to the email.  I added .InsertFile which seems correct but it does not pick up the file.  I have tried using

.InsertFile = "C:\MyFile.pdf" as well creating a variable

Dim FiletoSend As String
FiletoSend = "C:\MyFile.pdf"

Which I realize is the same thing.  I tried dropping the .pdf and still no luck.

Below is the entire code

FYI I did not write this code some really smart person did.

Sub Mail_Selection_Range_Outlook_Body()

' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2007
    Dim rng As Range
    Dim rng1 As Range
    Dim rng2 As Range
    Dim FiletoSend As String
       
    Dim EAddress As String
   
    'EMAIL ADDRESS
    EAddress = Sheets("Sheet1").Cells(1, 1).Value
   
    'File Attachment
    FiletoSend = "C:\MyFile.pdf"
   
    Dim EMessage As String
    EMessage = "Test Email"
   
    Dim OutApp As Object
    Dim OutMail As Object
 
    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    Set rng = Sheets("Sheet1").Range("C4:K26")
   
       
    'Selection.SpecialCells (xlCellTypeVisible)
    'You can also use a range if you want
    '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")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
 
    On Error Resume Next
    With OutMail
        .To = EAddress
        .CC = ""
        .BCC = ""
        .Subject = EMessage
        .HTMLBody = RangetoHTML(rng)
        .InsertFile = FiletoSend
        .Send   'or use .Display
    End With
    On Error GoTo 0
 
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
 
    Set OutMail = Nothing
    Set OutApp = Nothing
   
       
End Sub
Microsoft ApplicationsMicrosoft OfficeMicrosoft Excel

Avatar of undefined
Last Comment
DavidH7470

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Shanan212

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
omgang

Instead of InsertFile try
.Attachments.Add(FiletoSend)

OM Gang
DavidH7470

ASKER
Perfect.  Thank you.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes