Solved

Marco to create an email from Excel

Posted on 2012-03-14
3
442 Views
Last Modified: 2012-03-14
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
0
Comment
Question by:DavidH7470
3 Comments
 
LVL 13

Accepted Solution

by:
Shanan212 earned 500 total points
ID: 37720286
try

.Attachments.Add FiletoSend
0
 
LVL 28

Expert Comment

by:omgang
ID: 37720298
Instead of InsertFile try
.Attachments.Add(FiletoSend)

OM Gang
0
 

Author Closing Comment

by:DavidH7470
ID: 37720600
Perfect.  Thank you.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now