Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Marco to create an email from Excel

Posted on 2012-03-14
3
451 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

840 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