+ Reply to Thread
Results 1 to 5 of 5

Marco To Create PDF Using Specific Sheets & Attach To Outlook Email

Hybrid View

  1. #1
    Forum Contributor Gtrtim112's Avatar
    Join Date
    04-12-2013
    Location
    Alabama, USA
    MS-Off Ver
    Excel 2016
    Posts
    335

    Marco To Create PDF Using Specific Sheets & Attach To Outlook Email

    Can anyone tell me why my macro isn't working properly?

    The following code has been working flawlessly for quite some time but now it won't do as I need it to.

    I want the macro to take specific sheets, create a PDF with them and attach the PDF file to an Outlook email. In this case, it's sheets 2, 3, 4 and 8. However, it will only attach the active sheet. I can't figure out why.

    Sub SendWorkSheetToPDF()
    'Update 20131209
    Dim Wb As Workbook
    Dim FileName As String
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    On Error Resume Next
    Set Wb = Application.ActiveWorkbook
    FileName = Wb.FullName
    xIndex = VBA.InStrRev(FileName, ".")
    If xIndex > 1 Then FileName = VBA.Left(FileName, xIndex - 1)
    FileName = FileName & "_" + ActiveSheet.Name & ".pdf"
    Sheets(Array(2, 3, 4, 8)).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FileName
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
    Addresses = Sheets("TerminalWorkload-Total").Range("aa10").Value
    With OutlookMail
    .To = Addresses
    .CC = ""
    .BCC = ""
    .Subject = "Terminal Workload Calculator - Mobile/109"
    .Body = " "
    .Body = "Terminal Workload Calculator - Mobile/109 - Fail to plan... Plan to fail"
    .Attachments.Add FileName
    .Display
    End With
    Kill FileName
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    End Sub
    Thanks!

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,446

    Re: Marco To Create PDF Using Specific Sheets & Attach To Outlook Email

    .
    Option Explicit
    
    Sub SendWorkSheetToPDF()
    
    Dim Wb As Workbook
    Dim FileName As String
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Dim xIndex As Integer
    Dim Addresses As String
    On Error Resume Next
    Set Wb = Application.ActiveWorkbook
    
        FileName = Wb.FullName
        
        xIndex = VBA.InStrRev(FileName, ".")
        
            If xIndex > 1 Then FileName = VBA.Left(FileName, xIndex - 1)
            
            FileName = FileName & "_" + ActiveSheet.Name & ".pdf"
            ThisWorkbook.Sheets(Array("Sheet2", "Sheet3", "Sheet4", "Sheet8")).Select
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FileName
            Set OutlookApp = CreateObject("Outlook.Application")
            Set OutlookMail = OutlookApp.CreateItem(0)
            Addresses = Sheets("Sheet1").Range("A1").Value
            
                With OutlookMail
                    .To = Addresses
                    .CC = ""
                    .BCC = ""
                    .Subject = "Terminal Workload Calculator - Mobile/109"
                    .Body = " "
                    .Body = "Terminal Workload Calculator - Mobile/109 - Fail to plan... Plan to fail"
                    .Attachments.Add FileName
                    .Display
                End With
                
        Kill FileName
        Set OutlookMail = Nothing
        Set OutlookApp = Nothing
        
    Sheets("Sheet1").Activate
    Sheets("Sheet1").Range("A1").Select
    End Sub
    Attached Files Attached Files

  3. #3
    Forum Contributor Gtrtim112's Avatar
    Join Date
    04-12-2013
    Location
    Alabama, USA
    MS-Off Ver
    Excel 2016
    Posts
    335
    Actually, I just noticed something...

    It doesn’t appear to have the code that would tell it where the email addresses are listed on one of the sheets. Look at my previous code and you’ll see they are listed in AA10 on a particular sheet.

    Can you add that command into the code you offered?

    Quote Originally Posted by Logit View Post
    .
    Option Explicit
    
    Sub SendWorkSheetToPDF()
    
    Dim Wb As Workbook
    Dim FileName As String
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Dim xIndex As Integer
    Dim Addresses As String
    On Error Resume Next
    Set Wb = Application.ActiveWorkbook
    
        FileName = Wb.FullName
        
        xIndex = VBA.InStrRev(FileName, ".")
        
            If xIndex > 1 Then FileName = VBA.Left(FileName, xIndex - 1)
            
            FileName = FileName & "_" + ActiveSheet.Name & ".pdf"
            ThisWorkbook.Sheets(Array("Sheet2", "Sheet3", "Sheet4", "Sheet8")).Select
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FileName
            Set OutlookApp = CreateObject("Outlook.Application")
            Set OutlookMail = OutlookApp.CreateItem(0)
            Addresses = Sheets("Sheet1").Range("A1").Value
            
                With OutlookMail
                    .To = Addresses
                    .CC = ""
                    .BCC = ""
                    .Subject = "Terminal Workload Calculator - Mobile/109"
                    .Body = " "
                    .Body = "Terminal Workload Calculator - Mobile/109 - Fail to plan... Plan to fail"
                    .Attachments.Add FileName
                    .Display
                End With
                
        Kill FileName
        Set OutlookMail = Nothing
        Set OutlookApp = Nothing
        
    Sheets("Sheet1").Activate
    Sheets("Sheet1").Range("A1").Select
    End Sub

  4. #4
    Forum Contributor Gtrtim112's Avatar
    Join Date
    04-12-2013
    Location
    Alabama, USA
    MS-Off Ver
    Excel 2016
    Posts
    335

    Re: Marco To Create PDF Using Specific Sheets & Attach To Outlook Email

    Thanks, Logit! I will give this a whirl in the morning and let you know if it works for me.

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,446

    Re: Marco To Create PDF Using Specific Sheets & Attach To Outlook Email

    .
    Replace this line in the macro of the workbook I attached :
    Addresses = Sheets("Sheet1").Range("A1").Value
    With this line :
    Addresses = Sheets("TerminalWorkload-Total").Range("aa10").Value

    Change the sheet names in these two lines to the sheet name where you have the command button to run the macro:

    Sheets("Sheet1").Activate
    Sheets("Sheet1").Range("A1").Select
    If the Command Button is located on Sheet 1 .... don't change it.

+ 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. VBA to create PDF from worksheet and attach to Outlook Email
    By aglawrence in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-20-2017, 04:02 PM
  2. [SOLVED] PDF - open outlook email and attach PDF
    By passman86 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-02-2017, 10:00 AM
  3. attach file in an outlook email from specific folder
    By 9599lorenzo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2017, 07:07 AM
  4. [SOLVED] Macro to attach an additional file to and outlook email
    By Groovicles in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-18-2014, 01:27 PM
  5. [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
  6. Marco to send email via MAC (Outlook is working fine)
    By angela1979 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2013, 07:10 PM
  7. [SOLVED] Macro to create a pdf and attach to an email (Outlook)
    By jsedore in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-01-2013, 03:16 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