VBA Print to PDF Macro (with specific file name and type)

CHOPDOG

New Member
Joined
Feb 27, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, I've added a macro that creates a PDF based on specific cell references. It works as intended, but I would like to make it a little easier for users. I have two questions:
  • (1) Is it possible to have the file name already pre-populated to the SaveAsDialog box based on specific cell references (highlighted in yellow)? It currently defaults to blank.
  • (2) Is it possible to have the Save as type: pre-set to PDF (red arrow)? It currently defaults to Excel Workbook.xlsx.
The current code and reference image are below. Thanks in advance!

1709059780247.png


VBA Code:
Sub ToPDF()

Dim filesave As FileDialog
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range

Set filesave = Application.FileDialog(msoFileDialogSaveAs)
Set wb = ThisWorkbook
Set ws = wb.Worksheets("A3 Rebate Planner")
Set rng = ws.Range("A1:U37")

Sheets(Array("A3 Rebate Planner")).Select

With filesave

If .Show = -1 Then

rng.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=.SelectedItems(1), _
    OpenAfterPublish:=False, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=True, _
    Quality:=xlQualityStandard

End If
End With
End Sub
[CODE=vba]
[/CODE]
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try this - it reads the PDF file name (without the .pdf extension) from V1.

VBA Code:
Sub ToPDF()

    Dim filesave As FileDialog
    Dim rng As Range
    Dim PDFfileName As String
    Dim formatIndex As Long, i As Long
    
    Set filesave = Application.FileDialog(msoFileDialogSaveAs)
    
    With ThisWorkbook.Worksheets("A3 Rebate Planner")
        Set rng = .Range("A1:U37")
        PDFfileName = .Range("V1").Value
    End With
    
    With filesave
        .Title = "Save as PDF"
        .InitialFileName = PDFfileName
        formatIndex = 0
        For i = 1 To .Filters.Count
            If InStr(1, .Filters(i).Extensions, "pdf", vbTextCompare) > 0 Then formatIndex = i
        Next
        If formatIndex > 0 Then .FilterIndex = formatIndex
        If .Show Then
            rng.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=.SelectedItems(1), _
                OpenAfterPublish:=False, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=True, _
                Quality:=xlQualityStandard
        End If
    End With
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,813
Messages
6,181,111
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