+ Reply to Thread
Results 1 to 4 of 4

Macro to attach excel pdf to email not working

Hybrid View

  1. #1
    Registered User
    Join Date
    01-08-2014
    Location
    wigan
    MS-Off Ver
    2007
    Posts
    78

    Macro to attach excel pdf to email not working

    Hi,

    Please can someone tell me when the following code will not attach a pdf of my workbook to an email:

    Private Sub ExpAndSendPdf()
    Dim sFileName As String
    Dim sTo As String
    Dim sSubject As String
    Dim sMeas As String
    Dim OutApp As Object
    Dim OutMail As Object
    Dim Send As Boolean
    
    'Create file name
    sFileName = Range("client").Value & " - Managment accounts - " & _
        Format(Range("manp").Value, "dd mmm yyyy" & ".pdf") 'Without .pdf
    'Check if file exists
    
    If Dir(sFileName & ".pdf") <> "" Then
        sFileName = sFileName & Format(Now(), "_hh-mm" & ".pdf")
    End If
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        FileName:=sFileName & ".pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    
    'Send pdf
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        'create reciepient string
        sTo = ""
        'Mail subject
        sSubject = "For review - Management accounts for " & Sheets("input sheet").Range("client")
        'create measage body
        sMeas = "Hello." & vbNewLine & vbNewLine & _
                "Please find attached management accounts for review." & vbNewLine & vbNewLine & _
                "Thank you."
        
        On Error Resume Next
        With OutMail
            .To = sTo
            .CC = ""
            .BCC = ""
            .Subject = sSubject
            .Body = sMeas
            .Attachments.Add (sFileName & ".pdf")
            'Change Send = False if you don't want it to send.
            Send = False
            If Send = True Then
                .Send
            Else
                .Display
            End If
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End Sub
    It appears to do everything correct apart from attach the file.

    Also - is it possible to have the pdf output to a temporary file location on the users computer and then clear down when the email has been sent? At the minute this is generating to the location the workbook is saved.

    thanks in advance.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,121

    Re: Macro to attach excel pdf to email not working

    Looks like you are adding .pdf twice.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-08-2014
    Location
    wigan
    MS-Off Ver
    2007
    Posts
    78

    Re: Macro to attach excel pdf to email not working

    hi - even with this removed it still doesn't work.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to attach excel pdf to email not working

    Hi Dan

    You need to fully qualify the FileName
     '*********************************
        ' This will be the Title of the PDF
        
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                myPath & "What Ever You Wish " & Format(Date, "dd_mm_yyyy") & ".pdf" _
                , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
                :=False, OpenAfterPublish:=False
        Filename = myPath & "What Ever You Wish " & Format(Date, "dd_mm_yyyy") & ".pdf"
       '*********************************
    Then you can use this line of Code to do this
    is it possible to have the pdf output to a temporary file location on the users computer and then clear down when the email has been sent
    Kill Filename
    Here's the Complete Code (from rondebruin) that I use
    Option Explicit
    Dim myPath As String
    
    Sub Create_PDF()
        Dim strSubject As String
        Dim strBody As String
        Dim strTo As String
        Dim strCC As String
        Dim Filename As String
    
        myPath = ThisWorkbook.Path & "\"
        strSubject = "Enter Your Subject Here"  '<----------------
        strBody = "Enter Your Body Message Here"  '<----------------
        strTo = ""
        strCC = ""
    
        Sheets("Sheet1").Select
        
        '*********************************
        ' This will be the Title of the PDF
        
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                myPath & "What Ever You Wish " & Format(Date, "dd_mm_yyyy") & ".pdf" _
                , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
                :=False, OpenAfterPublish:=False
        Filename = myPath & "What Ever You Wish " & Format(Date, "dd_mm_yyyy") & ".pdf"
       '*********************************
    
        If Filename <> "" Then
            RDB_Mail_PDF_Outlook Filename, strTo, strCC, strSubject, strBody, False
        Else
            MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
                    "Microsoft Add-in is not installed" & vbNewLine & _
                    "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
                    "The path to Save the file in arg 2 is not correct" & vbNewLine & _
                    "You didn't want to overwrite the existing PDF if it exist"
        End If
        Kill Filename
    End Sub
    
    
    Function RDB_Mail_PDF_Outlook(FileNamePDF As String, strTo As String, strCC As String, _
            strSubject As String, strBody As String, Send As Boolean)
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .To = strTo
            .CC = strCC
            .BCC = ""
            .Subject = strSubject
            .Body = strBody
            .Attachments.Add FileNamePDF
            If Send = True Then
                .Send
            Else
                .Display
            End If
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Function
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Email Macro to attach a non active worksheet to outlook email
    By mickgibbons1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-21-2013, 08:38 PM
  2. Excel Email Macro - HELP! - Need to be able to attach two different files
    By benwahchang in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-29-2013, 08:41 AM
  3. SaveAs PDF and attach to email macro won't attach?!
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2012, 05:28 PM
  4. Attach Long email signature to existing excel macro
    By Gamegeared in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-08-2011, 01:43 PM
  5. Automatically attach Excel spreadsheet to Outlook Email with Macro?
    By nbaj2k in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-01-2006, 11:45 AM

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