PDA

View Full Version : VBA code errors for exporting sheets as individual PDFs



Essami
04-10-2018, 02:10 PM
Hi,

I've never used VBA / Macros with Excel before so please bare with me.

I'm trying to do a VBA to export multiple sheets (over 50) from one workbook to individual PDF's with their respective sheet names as file names for the PDFs.

I've tried numerous VBA codes from online but they all end up in some errors and I can't get any of them to work. I'm on a Mac and my Excel is the 2016 version. Any help to explaining what is wrong with the code below (or if there is a better code!) would be much appreciated.



Sub SaveAsPDF()​
Dim CurWorksheet As Worksheet
For Each CurWorksheet In ActiveWorkbook.Worksheets
CurWorksheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Application.ActiveWorkbook.Path & “\” & CurWorksheet.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next CurWorksheet
End Sub

With this code I get the error "Compile error: Syntax error"

Paul_Hossler
04-10-2018, 02:22 PM
Using a Win10 PC, this works

Make sure that the "" uses the 'dumb' double quote (ASCII 34), and not the smart ones like in your sample above





Option Explicit

Sub SaveAsPDF()
Dim CurWorksheet As Worksheet
For Each CurWorksheet In ActiveWorkbook.Worksheets
CurWorksheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Application.ActiveWorkbook.Path & "\" & CurWorksheet.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next CurWorksheet
End Sub

Essami
04-10-2018, 02:26 PM
I changed those, but still the same error. :(

The first line of code is highlighted with yellow after I try to run it as well.

Essami
04-10-2018, 02:53 PM
OK, so I copy pasted your code and now I'm getting a different error.

It now starts to run and says Save Printing but then a "Printing Error" dialogue appears and when I click that away an "Run-Time Error 1004: Document not saved".

So we're getting ahead! But a new problem... Hmmm...