Excel VBA to email multiple worksheets as PDFs

Jemini Jimi

New Member
Joined
Jan 11, 2025
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have been trying to revise this code to send multiple worksheets as PDFs with no success.
The worksheets are "Sheet4", "Sheet6", and "Sheet7".
What do I need to change?
I got this code from here Author: Siluvia

VBA Code:
Sub Save_As_PDF_Send()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range

Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

If xFileDlg.Show = True Then
   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.Name + ".pdf"

'Check if file already exist
If Len(Dir(xFolder)) > 0 Then
    xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbYes Then
        Kill xFolder
    Else
        MsgBox "If existing PDF is not overwriten, this process will STOP." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
        Exit Sub
    End If
    If Err.Number <> 0 Then
        MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
        Exit Sub
    End If
End If

Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
    'Save as PDF file
    xSht.ExportAsFixedFormat Type:=xlTypePDF, FileName:=xFolder, Quality:=xlQualityStandard
    
    'Create Outlook email
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
    With xEmailObj
        .Display
        .To = ""
        .CC = ""
        .Subject = xSht.Name + ".pdf"
        .Attachments.Add xFolder
        If DisplayEmail = False Then
            '.Send
        End If
    End With
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi and welcome to MrExcel forum!

Try this:
VBA Code:
Sub Save_As_PDF_Send()
  Dim xFolder As String
  
  With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show <> -1 Then
       MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
       Exit Sub
    End If
    xFolder = .SelectedItems(1)
  End With
  
  'Save as PDF file
  xFolder = xFolder & "\" & "multiple worksheets.pdf"
  Sheets(Array("Sheet4", "Sheet6", "Sheet7")).Select
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard
  
  'Create Outlook email
  With CreateObject("Outlook.Application").CreateItem(0)
    .To = "example@gmail.com"
    .CC = ""
    .Subject = "multiple worksheets.pdf"
    .Attachments.Add xFolder
    .Display
  End With
  Sheets(1).Select
End Sub

🤗
 
Upvote 0
The code stops here
It is not enough to say "stop here".

What does the error message say?

Do sheets "Sheet4", "Sheet6" and "Sheet7" actually exist in your book?

Change on that line to the actual names of your sheets.
 
Upvote 0
Sorry about that. Error - Run-time error '9': Subscript out of range.
I can not change it to the "actual name" because the name will continually change.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
I have been trying to revise this code to send multiple worksheets as PDFs with no success.
The worksheets are "Sheet4", "Sheet6", and "Sheet7".
What do I need to change?
But in your original post you mentioned that they are sheets 4, 6 and 7.
You didn't mention that the names of the sheets are going to change.
It's impossible for us to guess if you don't explain it.
🧙‍♂️

I ask you to do the test with sheets 4, 6 and 7. Just so you can verify that 3 sheets are actually saved in the PDF.

After you confirm to me that the macro works, then if you require an update to the macro to consider changing the names of the sheets, you must explain what pattern to consider to take 3 sheets.

For example, if you want to save the first 3 sheets in PDF, regardless of the name, then change this line:
VBA Code:
Sheets(Array("Sheet4", "Sheet6", "Sheet7")).Select

For this:
VBA Code:
Sheets(Array(1, 2, 3)).Select

😇
 
Upvote 0
Hi RoryA , I am new here and did not know about the "Cross-posting" rule. Forgive me.

Hi Danta, Yes i did say that about the worksheet names because that is what the "codename" is and that will not change.
I am still getting the same error as before when I change the Array to 4, 6, 7.
Thank you for your help.
 
Upvote 0
Yes i did say that about the worksheet names because that is what the "codename" is and that will not change.
Where?

I am still getting the same error as before when I change the Array to 4, 6, 7.
I don't understand what you put in the code.

I asked you to put this:
VBA Code:
Sheets(Array("Sheet4", "Sheet6", "Sheet7")).Select

Or this:
VBA Code:
Sheets(Array(1, 2, 3)).Select

If you don't follow the instructions correctly, no macro will work.
 
Upvote 0

Forum statistics

Threads
1,225,605
Messages
6,185,949
Members
453,333
Latest member
BioCoder84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top