save workbook to pdf

Kaps_mr2

Well-known Member
Joined
Jul 5, 2008
Messages
1,589
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to save a workbook (output_workbook) as a pdf file to a users desktop. Although microsoft appears to be doing something no file is created. can any one help ? thanks

kind regards
Kaps

VBA Code:
Sub SaveActiveWorkbookAsPDF()

'Create and assign variables
Dim saveLocation As String
saveLocation = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "Myfile.pdf"

'Save active workbook as PDF
output_workbook.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=saveLocation

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
When I test your code on my laptop to the value of the "savelocation" variable is:
C:\Users\forrest\DesktopMyfile.pdf

The CreateObject function is not appending a needed "\"

modify that line of code to:
VBA Code:
saveLocation = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & Myfile.pdf"

I wrote the above so that if your file name changes or is coming from a Sheet the backslash ("\") will still be where it needs to be

Also the next line of code needs to be either
VBA Code:
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF,  Filename:=saveLocation


or your need to define output_workbook

VBA Code:
Dim output_workbook As Workbook
Set output_workbook = ThisWorkbook 'or the workbook you intend to save as a PDF
output_workbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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