+ Reply to Thread
Results 1 to 6 of 6

Macro to send a pdf attachment via outlook from excel

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-06-2004
    Posts
    121

    Macro to send a pdf attachment via outlook from excel

    I've done a search on this which has resulted in a lot of complex code. I have a very simple code I use to create a PDF of a page and then switch back to my normal printer which is as follows:

    Application.ScreenUpdating = False
        Application.ActivePrinter = "Adobe PDF on Ne04:"
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
            IgnorePrintAreas:=False
        Application.ActivePrinter = "HP Officejet Pro 8500 A910 on Ne02:"
    All I need is an equally simple code (if possible) to add that will attach the PDF that is created from the code above to an outlook e-mail and automatically send it (same person every time, same subject line and body copy every time).

    What's the simplest way to do this?
    Last edited by Josiah; 08-26-2013 at 05:18 PM.

  2. #2
    Forum Contributor
    Join Date
    01-06-2004
    Posts
    121

    Re: Macro to send a pdf attachment via outlook from excel

    ...Anyone?

  3. #3
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    522

    Re: Macro to send a pdf attachment via outlook from excel

    Hello Josiah,
    As i am a VBA learner cant help you with a code. In almost similar situation i found the link below very helpful.
    HTML Code: 
    Hope this helps you too.
    Best Regards/VKS

  4. #4
    Forum Contributor
    Join Date
    01-06-2004
    Posts
    121

    Re: Macro to send a pdf attachment via outlook from excel

    Quote Originally Posted by VKS View Post
    Hello Josiah,
    As i am a VBA learner cant help you with a code. In almost similar situation i found the link below very helpful.
    HTML Code: 
    Hope this helps you too.
    Best Regards/VKS

    Thanks for everyone's help but this is EXACTLY what I needed. Perfect!! Thanks VKS!

  5. #5
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Macro to send a pdf attachment via outlook from excel

    I think this was taken from the internet someplace. Its the code behind out help tickets at work.

    Private Declare Function GetTempPath Lib "kernel32" Alias _
    "GetTempPathA" (ByVal nBufferLength As Long, ByVal _
    lpBuffer As String) As Long
     '
    Const MAX_PATH = 260
     '
     ' This function uses Windows API GetTempPath to get the temporary folder
    Sub Get_Temporary_Folder()
        sTempFolder = GetTmpPath
    End Sub
     '
     '
    Function GetTmpPath()
        Dim sFolder As String ' Name of the folder
        Dim lRet As Long ' Return Value
        sFolder = String(MAX_PATH, 0)
        lRet = GetTempPath(MAX_PATH, sFolder)
        If lRet <> 0 Then
            GetTmpPath = Left(sFolder, InStr(sFolder, _
            Chr(0)) - 1)
        Else
            GetTmpPath = vbNullString
        End If
    End Function
     '
     '
     '
     '
     'Main Functions
    Sub Workbook_To_PDF()
        Randomize
        Dim filename As String
        Dim File As String
        Dim Send_To As String
        Dim Subject_Line As String
        Dim Body_Text As String
        Dim Send_Email_Automatically As Boolean
        Dim Show_Saved_File As Boolean
         
        GetTmpPath
         
        File = GetTmpPath & "YourPdfFile " & Right(Rnd(), 5) & ".pdf"
        Show_Saved_File = False 'False = No
         
        filename = Create_PDF(ActiveWorkbook, File, True, Show_Saved_File)
         
         
        Send_To = "" '''Enter to email address here
        Subject_Line = ""  '''Enter subject here
        Body_Text = ""   '''Enter email body here
        Send_Email_Automatically = True 'False = No
         
        Mail_Me = Mail_PDF_Outlook(File, Send_To, Subject_Line, Body_Text, Send_Email_Automatically)
         
    End Sub
     '
     '
    Function Create_PDF(Myvar As Object, FixedFilePathName As String, _
        OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
        Dim FileFormatstr As String
        Dim Fname As Variant
         'Test to see if the Microsoft Create/Send add-in is installed.
        If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
        & Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
            If FixedFilePathName = "" Then
                 'Open the GetSaveAsFilename dialog to enter a file name for the PDF file.
                FileFormatstr = "PDF Files (*.pdf), *.pdf"
                Fname = Application.GetSaveAsFilename("", filefilter:=FileFormatstr, _
                Title:="Create PDF")
                 'If you cancel this dialog, exit the function.
                If Fname = False Then Exit Function
            Else
                Fname = FixedFilePathName
            End If
             'If OverwriteIfFileExist = False then test to see if the PDF
             'already exists in the folder and exit the function if it does.
            If OverwriteIfFileExist = False Then
                If Dir(Fname) <> "" Then Exit Function
            End If
             'Now export the PDF file.
            On Error Resume Next
            Myvar.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            filename:=Fname, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=OpenPDFAfterPublish
            On Error GoTo 0
             'If the export is successful, return the file name.
            If Dir(Fname) <> "" Then Create_PDF = Fname
        End If
    End Function
     '
     '
    Function Mail_PDF_Outlook(FileNamePDF As String, StrTo 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 = ""
            .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
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  6. #6
    Forum Contributor
    Join Date
    03-14-2012
    Location
    location
    MS-Off Ver
    Excel 2007
    Posts
    170

    Re: Macro to send a pdf attachment via outlook from excel

    or
    Sub Button37201_Click()
    On Error Resume Next
    ' Sheet3.Range("b2:i20")
    
        ActiveSheet.ExportAsFixedFormat From:=1, Type:=xlTypePDF, Filename:= _
        "C:\Users\blue\Desktop\emp1.pdf", Quality:=xlQualityStandard _
        , IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    'Working in Office 2000-2010
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
        Dim EmailAttachment As String
        
        If ActiveWorkbook.Path <> "" Then
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)
    
            strbody = "<font size=""2"" face=""Tahoma"">" & _
                      "Hi,<br><br>" & _
                      "Please find attached today's xxxxxxxx.<br><br>" & _
                      "Regards," & _
                      "<br><br>blue</font>"
    
            EmailAttachment = "C:\Users\blue\Desktop\emp1.pdf"
            
    
            
            With OutMail
                .To = [d15] 'change for yr needs
                .CC = [d16]
                .BCC = ""
                .Subject = "XXXXXXXXXXXX"
                .Attachments.Add EmailAttachment
                .HTMLBody = strbody
                '.Send   'or use
                .Display
            End With
            On Error GoTo 0
    
            Set OutMail = Nothing
            Set OutApp = Nothing
        Else
            MsgBox "Error"
        End If
     Dim MyFile As String    'This line of code is optional
        On Error Resume Next  'On hitting errors, code resumes next code
        MyFile = "C:\Users\blue\Desktop\emp1.pdf"
        Kill MyFile
        
    End Sub
    blue

+ 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] VBA Macro to print to Pdf format and place as attachment in Outlook (Excel & Outlook 2007)
    By Webman1012 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-29-2013, 01:25 PM
  2. Excel to send email via Outlook (attachment)
    By uncleslinky in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2012, 07:01 AM
  3. Send worksheet as an attachment to specified recipients via outlook
    By SunOffice in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-19-2011, 05:38 PM
  4. Send sheet as Outlook attachment
    By Jessy01 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-04-2011, 01:28 PM
  5. Send Excel attachment, but wait till Outlook closes
    By thepython in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-17-2006, 05:30 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