Save as PDF macro

pinoynl

New Member
Joined
Jan 28, 2023
Messages
5
Office Version
  1. 365
Hi there,

I'm just new on the forum and I would like to seek your help & advice how to make the macro work.

I have prepared a file with multiple sheets (90 to be exact). All these sheets need to be printed to PDF in separate files, each sheet a separate file. The PDF will have the name of the sheet as filename.

This is the code:
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
When I run it, it gives a compiling syntax error. It points to the Sub SaveAsPDF() line.

1674900337819.png


Would you might know what is causing the error and can you help me fix it? Thanks a lot!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
In addition, there is a warning banner in my Excel application saying: BLOCKED CONTENT
I reviewed the Trust Center settings and enabled macros.

Not sure if that is causing the compile error?
 
Upvote 0
Hi pinoyl,

an underscore at the end of a codeline says that this line is continued in the next line (contents belong together). If this line is blank the Debugger will raise an error-

VBA Code:
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

Macros in embedded Excel workbook are blocked from running may answer the additional quesiton.

Ciao,
Holger
 
Upvote 0
Thanks a lot for your help, Holger!

Now I receive the error "Runtime Error 11 Division by zero"

What does that mean?
 
Upvote 0
Hi pinoyl,

you try to divide a number either through 0 (Zero) or an empty cell - but that is not part of teh code you posted before.

Holger
 
Upvote 0
Hi Holger,

That was the error message when I pasted the code that you shared.

I'll try again.

Thanks!
 
Upvote 0
Hi pinoyl,

do you have any event-driven code in either ThisWorkbook or behind the sheet(s)? That could be the reason for the message.

Holger
 
Upvote 0
Hi Holger,

yes, I have an index sheet that contains the links to all other sheets to easily jump from the index sheet to sheet 'x' and back to the index sheet.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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