Solved

Marco to create an email from Excel

Posted on 2012-03-14
3
452 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
[X]
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
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

696 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