Print to PDF and open Save As dialog box...

CarlStephens

Board Regular
Joined
Sep 25, 2020
Messages
128
Office Version
  1. 2016
Platform
  1. Windows
Hello Team,

I currently have the below code that prints a sheet to pdf and saves it to the user's desktop and takes the file name from a cell and names the file, but I want to change it so that it does not save to the desktop automatically and simply opens the Save As dialog for the user to choose where to save the file. Can I kindly take a moment of someone's time to enlighten me on what change is required? Thank you in advance.

Sub PrintVisa()
Dim s As String, p As String

s = Range("N1").Value 'file name
p = Environ("USERPROFILE") & "\Desktop\"
If Right(p, 1) <> "\" Then p = p & "\"
If Dir(p, vbDirectory) = "" Then
MsgBox "The path does not exist"
Exit Sub
End If
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=p & s, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

MsgBox "Success - The file is now on your desktop, please move this file in the Visa Letters folder on One Drive by clicking cell P1. ", vbInformation
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try the following . . .

VBA Code:
Sub PrintVisa()

    Dim saveAsFilename As Variant
    saveAsFilename = Application.GetSaveAsFilename( _
        InitialFileName:=Application.DefaultFilePath & "\" & Range("N1").Value, _
        FileFilter:="PDF File (*.pdf), *.pdf", _
        Title:="Save")
        
    If saveAsFilename = False Then Exit Sub

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveAsFilename, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    
    MsgBox "Success - The file is now located at '" & saveAsFilename & "', please move this file in the Visa Letters folder on One Drive by clicking cell P1.", vbInformation
    
End Sub

Note that you can change the default file path for InitialFilename as desired.

Hope this helps!
 
Upvote 0
Solution
Try the following . . .

VBA Code:
Sub PrintVisa()

    Dim saveAsFilename As Variant
    saveAsFilename = Application.GetSaveAsFilename( _
        InitialFileName:=Application.DefaultFilePath & "\" & Range("N1").Value, _
        FileFilter:="PDF File (*.pdf), *.pdf", _
        Title:="Save")
       
    If saveAsFilename = False Then Exit Sub

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveAsFilename, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
   
    MsgBox "Success - The file is now located at '" & saveAsFilename & "', please move this file in the Visa Letters folder on One Drive by clicking cell P1.", vbInformation
   
End Sub

Note that you can change the default file path for InitialFilename as desired.

Hope this helps!
Worked perfectly, thank you so much.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
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