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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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