+ Reply to Thread
Results 1 to 9 of 9

VBA code exporting to PDF but not attaching the file to the email.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-02-2013
    Location
    abbots langley
    MS-Off Ver
    Excel 2010
    Posts
    325

    VBA code exporting to PDF but not attaching the file to the email.

    Hi,

    i am almost there with my code i am trying to PDF a range in excel and attach to an email. I am converting ok and opening up the email but i am missing the attachment and i am now stuck. I would really appreciate if someone could advise me how to adapt the code so it attaches just the range i have specified


    Sub Mail_Genius()
    
        Dim rng As Range
        Dim wb1 As Workbook
        Dim wb2 As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim FileExtStr As String
        Dim OutApp As Object
        Dim OutMail As Object
    
      
        ActiveSheet.ExportAsFixedFormat xlTypePDF, fn, OpenAfterPublish:=True
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        Set rng = ActiveSheet.Range("B8:J75")
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        fn = "H:\AA - XXX\XX\XX\XX" & Format(Now(), "DD-MM-YYYY") & ".pdf"
        On Error Resume Next
        With OutMail
            .To = ""
            .CC = ""
            .BCC = ""
            .Subject = Range("d6").Value & " , " & Range("C18").Value & " , " & Range("B2").Value
            .Attachments.Add ActiveSheet.Range("B8:j75")
           
            .Display
        End With
        On Error GoTo 0
      
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    Appreciate your help with this.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: VBA code exporting to PDF but not attaching the file to the email.

    If fn contains the path and filename to the PDF that was created try this.
            .Attachments.Add fn
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    07-02-2013
    Location
    abbots langley
    MS-Off Ver
    Excel 2010
    Posts
    325

    Re: VBA code exporting to PDF but not attaching the file to the email.

    Hi Norie


    That is it, works perfectly. i will digest your comments now so i undertsand.

    Thankyou so very much for looking at this and heping.

  4. #4
    Forum Contributor
    Join Date
    07-02-2013
    Location
    abbots langley
    MS-Off Ver
    Excel 2010
    Posts
    325

    Re: VBA code exporting to PDF but not attaching the file to the email.

    Hi

    Is it possible to add Range ("d6) to the fn?

    fn = "H:\XXX\XX\XX " & Format(Now(), "DD-MM-YYYY") & ".pdf"

    Sorry one more thing how can i specify the sheet name, just in case another file is open it cannot accidently email the wrong sheet?

  5. #5
    Forum Contributor
    Join Date
    07-02-2013
    Location
    abbots langley
    MS-Off Ver
    Excel 2010
    Posts
    325

    Re: VBA code exporting to PDF but not attaching the file to the email.

    Hi Norie,

    Thanks for your reply and that makes sense,. I have just noticed it is not saving the file, it is correctly opening up the range as a PDF along. i have checked the file name which is fine, must be something else in the code?

  6. #6
    Forum Contributor
    Join Date
    07-02-2013
    Location
    abbots langley
    MS-Off Ver
    Excel 2010
    Posts
    325

    Re: VBA code exporting to PDF but not attaching the file to the email.

    Hi Norie, when the pDF opens up the name is that of the file which is not what i want actually i want it to be the same as the email subject title.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: VBA code exporting to PDF but not attaching the file to the email.

    When the PDF opens does it actually have a name?

    Where is the value for fn coming from anyway?

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: VBA code exporting to PDF but not attaching the file to the email.

    You need to move this up above the code that's meant to create the PDF.
    fn = "H:\AA - XXX\XX\XX\XX" & Format(Now(), "DD-MM-YYYY") & ".pdf"
    Another couple of things you'll need to do is move this up to,
        Set rng = ActiveSheet.Range("B8:J75")
    and change the code that creates the PDF to this.
     rng.ExportAsFixedFormat xlTypePDF, fn, OpenAfterPublish:=False
    So after all those changes you should end up with this.
    Option Explicit
    
    Sub Mail_Genius()
    
    Dim rng As Range
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim OutApp As Object
    Dim OutMail As Object
    Dim fn As String
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        Set rng = ActiveSheet.Range("B8:J75")
    
        fn = "H:\AA - XXX\XX\XX\XX" & Format(Now(), "DD-MM-YYYY") & ".pdf"
    
        rng.ExportAsFixedFormat xlTypePDF, fn, OpenAfterPublish:=False
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        On Error Resume Next
        With OutMail
            .To = ""
            .CC = ""
            .BCC = ""
            .Subject = Range("d6").Value & " , " & Range("C18").Value & " , " & Range("B2").Value
            .Attachments.Add fn
            .Display
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End Sub

  9. #9
    Forum Contributor
    Join Date
    07-02-2013
    Location
    abbots langley
    MS-Off Ver
    Excel 2010
    Posts
    325

    Re: VBA code exporting to PDF but not attaching the file to the email.

    Hi i have worked out the filename :-)

    & Range("D6").Text & Format(Now(), "DD-MM-YYYY") & ".pdf"
    Thanks

+ 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. Email (outlook) recipients attaching specific file name.
    By monkeypants in forum Outlook Formatting & Functions
    Replies: 1
    Last Post: 05-13-2015, 07:18 PM
  2. Email (outlook) recipients attaching specific Excel file name.
    By monkeypants in forum Outlook Formatting & Functions
    Replies: 0
    Last Post: 05-13-2015, 07:14 PM
  3. Need VB Code to Extract email address from File name and email the file
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-30-2012, 11:18 AM
  4. Replies: 11
    Last Post: 07-10-2012, 10:35 AM
  5. Attaching One Worksheet of a File to Email
    By MLCall in forum Excel General
    Replies: 0
    Last Post: 01-17-2012, 12:51 PM
  6. Macro. compiling email, attaching 1x sheet + 1x powerpoint file.
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-28-2011, 02:24 PM
  7. Exporting to a .txt file - VBA Code
    By gavsta in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-17-2005, 05:05 PM

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