VBA - Save PDF with custom filename to specific location

JC83UK

New Member
Joined
Oct 15, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hoping someone can help me. I have most of the coding worked out but i cannot get it to agree to save with the custom file name.

I have two different pages which display data slightly differently depending on the user need. The result should be that they pick which report they want to generate as a PDF and then the save option comes up with the filename generated and the correct folder selected. Currently the only part that does not happen is the filename. I have attached the code below:

Sub PDF_EOD()

Dim Result As Integer
Dim Opendialog As Variant
Result = MsgBox("Was the Function Bar banking included in todays figures?", vbQuestion + vbYesNo)
If Result = vbYes Then
'''''
Sheets("EOD Banking Sheet 1").Select

Opendialog = Application.GetSaveAsFilename(Sheet1.[A10].Value, "PDF (*.pdf), *.pdf")

If Not Opendialog <> False Then MsgBox "File not saved.", vbCritical: Exit Sub

Dim fName As String
fName = "EOD" & Format(Date, "yyyymmdd")

ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Z:\EOD Reports" & fName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

MsgBox "Saved.", vbInformation

Sheets("Main").Select

Else

Sheets("EOD Banking Sheet 2").Select

Opendialog = Application.GetSaveAsFilename(Sheet1.[A10].Value, "PDF (*.pdf), *.pdf")

If Not Opendialog <> False Then MsgBox "File not saved.", vbCritical: Exit Sub

ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Z:\EOD Reports" & fName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

MsgBox "Saved.", vbInformation

Sheets("Main").Select

End If

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I am not 100% sure I know what you are trying to do.
See if this is heading in the right direction or not:-
I am assuming that the 2nd file name was supposed to come from sheet2 A10

VBA Code:
Sub PDF_EOD()

    Dim Result As Integer
    Dim Opendialog As Variant
    Dim fName As String
    
    Result = MsgBox("Was the Function Bar banking included in todays figures?", vbQuestion + vbYesNo)
    If Result = vbYes Then
    '''''
    Sheets("EOD Banking Sheet 1").Select
    
    'Opendialog = Application.GetSaveAsFilename("C:\Users\ablak\Documents\Alex\Software\Excel\Test\TestCreatePDF\" & Sheet1.[A10].Value, "PDF (*.pdf), *.pdf")
    Opendialog = Application.GetSaveAsFilename("Z:\EOD Reports\" & Sheet1.[A10].Value, "PDF (*.pdf), *.pdf")
   
    If Not Opendialog <> False Then MsgBox "File not saved.", vbCritical: Exit Sub
    
    fName = Replace(Opendialog, ".pdf", " " & Format(Date, "yyyymmdd") & ".pdf")
    
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    fName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    
    MsgBox "Saved.", vbInformation
    
    Sheets("Main").Select
    
    Else
    
    Sheets("EOD Banking Sheet 2").Select
    
    Opendialog = Application.GetSaveAsFilename("Z:\EOD Reports\" & Sheet2.[A10].Value, "PDF (*.pdf), *.pdf")
    
    If Not Opendialog <> False Then MsgBox "File not saved.", vbCritical: Exit Sub
    
    fName = Replace(Opendialog, ".pdf", " " & Format(Date, "yyyymmdd") & ".pdf")
    
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    fName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    
    MsgBox "Saved.", vbInformation
    
    Sheets("Main").Select
    
    End If

End Sub
 
Upvote 0
Hi Alex. Thanks for the reply. You are indeed on the correct track with what i am trying to achieve. The end result i am looking for is i want the user to run the macro and after selecting either yes or no be forced to save the file in a specified location with a filename that is date specific (EOD20211121.pdf which to the user would mean that the data in that pdf is for 21st November 2021 (Sheet1.[A10] is the location of the text EOD)).
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
Members
452,616
Latest member
intern444

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