Export to pdf in user-selected folder

dtnce

New Member
Joined
Feb 15, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
So I've got this macro to export a worksheet to a pdf and save it in a folder selected by the user:

VBA Code:
Sub pdf()

    If Range("A1").Value = "" Then GoTo err
    Dim folder As FileDialog
    Dim Fldr As String
    Set folder = Application.FileDialog(msoFileDialogFolderPicker)
    With folder
        .AllowMultiSelect = False
        If .Show <> -1 Then GoTo err
        Fldr = .SelectedItems.Item(1)
    End With
    Application.DisplayAlerts = False
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("A1").Value, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
    Exit Sub
err:
    MsgBox "FILE NOT SAVED - 'OK' to continue"
    Exit Sub
End Sub

My problem is, when it runs, it saves to the previous folder in the hierarchy than the one selected. If user selects the folder U:\Files\pdf\test the file is saved in U:\Files\pdf

Any thoughts are greatly appreciated
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,723
Welcome to MrExcel forums.

You aren't including the selected folder in the file name.
VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Fldr & "\" & Range("A1").Value, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
 
Solution

dtnce

New Member
Joined
Feb 15, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Welcome to MrExcel forums.

You aren't including the selected folder in the file name.
VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Fldr & "\" & Range("A1").Value, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
🤦‍♂️ Stare at something long enough and you forget to look at the obvious. Thanks for the help!
 

Watch MrExcel Video

Forum statistics

Threads
1,128,167
Messages
5,629,079
Members
416,364
Latest member
maatpsr

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
Top