VBA Print to pdf

Qqqqq

New Member
Joined
Feb 6, 2014
Messages
48
I have a macro that prints a single worksheet to a pdf file, which I've used successfully many times. I moved this code into a new workbook, and it doesn't work. When I get to the ActiveSheet.ExportAsFixedFormat portion of the code, I get a Run-time error '5': Invalid procedure call or argument error.

VBA Code:
Sub Print_pdf

    Dim xWB As Workbook
    Dim xFSO As Object
    Dim xFileName As String

    Set xWB = ActiveWorkbook
    Set xFSO = CreateObject("Scripting.FileSystemObject")
    xFileName = xWB.Path & "\" & xFSO.GetBaseName(xWB.Name) & ".pdf"

    xWB.Sheets("Sheet1").Select

    ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=xFileName, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

End Sub

What can I do to make this code work in my new workbook?
 
If the OP has left out a portion of the macro code, it should be posted for review.

I stand by my first statement. The macro code as posted works here.
The point I was making is that it is not always the code that is the reason for the error. Sometimes it is the data you are working with.
The fact that they said it works once they changed the file paths seems to suggest that the error lines in the data, and not the code.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

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