Excel Macro Export to PDF

bdenn

New Member
Joined
Feb 3, 2017
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hello,
I am using the following macro to save the current selected sheet as a PDF. When I go and edit the path I want it to save to it fails the macro and I think it has something to do with: ("userprofile") But if I create a folder on the desktop: \Holding\ it works fine.

I would like to be able to change this to the following path: C:\North\Transmission\Transmission Systems Logging\PDF Export

Also, I was wondering is there a way to adjust the macro to save all sheets to there own PDF files still using the sheet name and adding the current date at the end? This would make it so I would not need to click each sheet and run the macro, There is about 15 Sheets.

Thank You,
Bdenn

VBA Code:
Sub Export_PDF()
    ' Saves active sheet as PDF file.


    Dim FName As String
    
    FName = Environ("userprofile") & "\Desktop\Holding\" & ActiveSheet.Name & " " & _
        Format(Now(), "mm-dd-yyyy") & ".pdf"
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FName, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False
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.
To save all the worksheets to separate files, use this method

VBA Code:
Sub Export_PDF()
    ' Saves active sheet as PDF file.

    Dim FName As String, ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        
        FName = Environ("userprofile") & "\Desktop\Holding\" & ws.Name & " " & Format(Now(), "mm-dd-yyyy") & ".pdf"

        ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FName
    Next ws
End Sub
 
Upvote 0
I am using the following macro to save the current selected sheet as a PDF. When I go and edit the path I want it to save to it fails the macro and I think it has something to do with: ("userprofile") But if I create a folder on the desktop: \Holding\ it works fine.

I would like to be able to change this to the following path: C:\North\Transmission\Transmission Systems Logging\PDF Export

Are you saying that if folder does not already exist then save to the other path ?
- here is a method to do that, using a function to test the existence of the folder

VBA Code:
Sub Export_PDF()

    Dim FldrName As String, FName As String, ws As Worksheet
    Const DefaultFldr = "C:\North\Transmission\Transmission Systems Logging\PDF Export"
    FldrName = Environ("userprofile") & "\Desktop\Holding"
'if folder does not exist then revert to default folder
    If FldrExists(FldrName) = False Then FldrName = DefaultFldr
   
    For Each ws In ThisWorkbook.Worksheets   
         FName = FldrName & "\" & ws.Name & " " & Format(Now(), "mm-dd-yyyy") & ".pdf"
        ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FName
    Next ws
   
End Sub

Code:
Function FldrExists(ByVal fldr As String) As Boolean
    With CreateObject("Scripting.FileSystemObject")
        FldrExists = .FolderExists(fldr)
    End With
End Function
 
Upvote 0
If, on the other hand, you want to create the folder if it does not already exist
VBA Code:
Sub Export_PDF()

    Dim FldrName As String, FName As String, ws As Worksheet
    FldrName = Environ("userprofile") & "\Desktop\Holding"

'if folder does not exist then create it
    If FldrExists(FldrName) = False Then MkDir FldrName

'etc   

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,700
Messages
6,126,301
Members
449,308
Latest member
VerifiedBleachersAttendee

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