+ Reply to Thread
Results 1 to 2 of 2

adding multiple attachments to a macro in excel for a email using outlook

  1. #1
    Registered User
    Join Date
    03-09-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    8

    adding multiple attachments to a macro in excel for a email using outlook

    hi there

    I am having a few problems attaching multiple documents using a macro here is the data i am currently using to produce an email and attach a pdf doc and then send off to clients, i am just wondering if anyone can help with adding multiple pdf attachments using this macro (if that makes sense)

    Sub sendemail()
    Dim Answer As Variant
    Dim FileToAttach As Variant
    Dim Msg As String
    Dim MyApp As Boolean
    Dim olApp As Object
    Dim olEmail As Object
    Dim SendTo As String
    Dim Subj As String
    Dim InsertSig As String
    'Outlook constants aren't available using late binding
    Const olByValue = 1
    Const olMailItem = 0

    'Setup email variables using worksheet cells F8, K8, B40 AND B41
    SendTo = Cells(8, "F").Text 'F8
    Subj = Cells(8, "K").Text 'K8
    Msg = "Dear," & Cells(41, "B").Text



    'Select a file to attach to the email
    FileFilter = "All Files, *.*,CSV Files, *.csv,Excel Files, *.xls;*.xlt;*.xla"
    FileToAttach = Application.GetOpenFilename(FileFilter:=FileFilter, Title:="C:\Documents and Settings\David\Desktop\Copy of ccb quote calculators\calculator\telecomsproposal.pdf")
    'Was Cancel selected?
    If FileToAttach = False Then
    Answer = MsgBox("Do you still want to send an email?", vbYesNo + vbQuestion)
    If Answer = vbNo Then Exit Sub
    End If

    'Open or Start Outlook
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If Err = 429 Then
    MyApp = True
    Set olApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0

    olApp.Session.Logon

    'Create an Outlook Mail Item
    Set olEmail = olApp.CreateItem(olMailItem)

    On Error Resume Next
    With olEmail
    .To = SendTo
    .Subject = Subj
    .Body = Msg
    .Attachments.Add FileToAttach, olByValue
    .Send
    End With

    If Err <> 0 Then
    MsgBox "The following error occurred while sending the email..." & vbCrLf _
    & "(" & Err.Number & ") " & Err.Description
    End If

    olApp.Session.logoff
    If MyApp Then olApp.Quit

    'Release Objects
    Set olApp = Nothing
    Set olEmail = Nothing

    Can anyone help please

    regards

    David

  2. #2
    Registered User
    Join Date
    03-09-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: adding multiple attachments to a macro in excel for a email using outlook

    for anyone who wants to know how this was done see below code
    Dim Answer As Variant
    Dim FileToAttach As Variant
    Dim Msg As String
    Dim MyApp As Boolean
    Dim olapp As Object
    Dim olEmail As Object
    Dim SendTo As String
    Dim Subj As String
    Dim InsertSig As String
    'Outlook constants aren't available using late binding
    Const olByValue = 1
    Const olMailItem = 0

    'Setup email variables using worksheet cells F8, K8, B40 AND B41
    SendTo = Cells(8, "F").Text 'F8
    Subj = Cells(9, "K").Text 'K8
    Msg = "Dear," & Cells(41, "B").Text & vbCrLf & vbCrLf & Cells(42, "B").Text & vbCrLf & vbCrLf & "Regards" & vbCrLf & vbCrLf & "David Curcic" & vbCrLf & "Tel: 0845 094 2517 (Local Rate)" & vbCrLf & "Fax: 0845 527 2610" & vbCrLf & "Email: [email protected]" & vbCrLf & "Web: www.energyadviceline.org.uk" & vbCrLf & vbCrLf & Cells(78, "B").Text



    'Select a file to attach to the email
    FileToAttach1 = Application.GetOpenFilename
    FileToAttach2 = Application.GetOpenFilename
    FileToAttach3 = Application.GetOpenFilename
    'Was Cancel selected?
    If FileToAttach = False Then
    Answer = MsgBox("Do you still want to send an email?", vbYesNo + vbQuestion)
    If Answer = vbNo Then Exit Sub
    End If
    'Open or Start Outlook
    On Error Resume Next
    Set olapp = GetObject(, "Outlook.Application")
    If Err = 429 Then
    MyApp = True
    Set olapp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0

    olapp.Session.Logon

    'Create an Outlook Mail Item
    Set olEmail = olapp.CreateItem(olMailItem)

    On Error Resume Next
    With olEmail
    .To = SendTo
    .Subject = Subj
    .Body = Msg
    .Attachments.Add FileToAttach1, olByValue
    .Attachments.Add FileToAttach2, olByValue
    .Attachments.Add FileToAttach3, olByValue
    .Send
    End With

    If Err <> 0 Then
    MsgBox "The following error occurred while sending the email..." & vbCrLf _
    & "(" & Err.Number & ") " & Err.Description
    End If

    olapp.Session.logoff
    If MyApp Then olapp.Quit

    'Release Objects
    Set olapp = Nothing
    Set olEmail = Nothing


    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1