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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

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