VBA - Name PDF File, Should Be Easy

kgallego

Board Regular
Joined
Jul 26, 2011
Messages
82
Office Version
  1. 365
Hi all,

I have what should be an easy question. I have code that prints a PDF from a spreadsheet. I want to change it so that the file name of the printed PDF references a cell plus some text.

For example, in cell Sheet1.cells(1,1).value I have "AC/DC". I want the below code to name the PDF "AC/DC IS AWESOME.pdf". Any help would be greatly appreciated!

Sub MySheetsIntoPdf99()


Dim wsA As Worksheet
Set wsA = ActiveSheet


Call ExportSheetsAsPdf99(ThisWorkbook.Path & "/IS AWESOME.pdf", wsA)


End Sub
Sub ExportSheetsAsPdf99(FileName As String, ParamArray exportedSheets() As Variant)


Dim WB As Workbook
Dim Sheet As Object
Dim i As Long


Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set WB = Workbooks.Add
Set Sheet = exportedSheets(LBound(exportedSheets))
Sheet.Copy After:=WB.Sheets(1)
WB.Sheets(1).Delete

For i = LBound(exportedSheets) + 1 To UBound(exportedSheets)
exportedSheets(i).Copy After:=WB.Sheets(WB.Sheets.Count)
Next i

WB.ExportAsFixedFormat XlFixedFormatType.xlTypePDF, FileName, _
Quality:=xlQualityStandard, OpenAfterPublish:=False

WB.Close False

Application.DisplayAlerts = True
Application.ScreenUpdating = True

MsgBox "PDF file has been created"
Set WB = Nothing
Set Sheet = Nothing
End Sub


Thanks,
Kelsey
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I want the below code to name the PDF "AC/DC IS AWESOME.pdf".
You cannot use "/" in file names.
 
Upvote 0
Try,

Untested

Code:
[COLOR=#574123][FONT=Tahoma]CallExportSheetsAsPdf99(ThisWorkbook.Path & Range("A1").value & " IS AWESOME.pdf", wsA)[/FONT][/COLOR]

Regards,

BigDawg15
 
Last edited:
Upvote 0
or try

Tested

Code:
Call ExportSheetsAsPdf99("C:\Users\yourid here\Desktop\" & Range("A1").Value & " IS AWESOME.pdf", wsA)

Will save file to your desktop or you can change the path to wherever you would like.

BigDawg15
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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