Save to PDF - issue with file name

Lana_B

New Member
Joined
Feb 16, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I am using the below code in a button to Open and Auto save a copy of excel file to PDF. All works fine EXCEPT; the file name the PDF file is saving as adds the name of the Folder it saves in to the beginning of it

I want the file name to be Fee Calculation - (C1) but it is coming through as Folder Name - Fee Calculation - (C1)

I know it's to do with the line of code in bold and I've tried heaps of suggestions from forums but can't get anything to work. .. any ideas?

Sub PrintSelectionToPDF()
Dim invoiceRng As Range
Dim strfile As String
'Setting range to be printed
Set invoiceRng = Range("A1:D40")
'setting file name with a time stamp.
strfile = Range("C1") & " - " & Format(Now(), "ddmmyy") & ".pdf"
'setting the fulli qualified name. The resultent pdf will be saved where the main file exists.
strfile = ThisWorkbook.Path & "Fee Calculation - " & strfile

invoiceRng.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strfile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi and welcome to the forum. Try this or if that doesnt work try a \ instead of the /:
strfile = ThisWorkbook.Path & "/Fee Calculation - " & strfile
 
Upvote 0
Solution
Give this a try:
VBA Code:
Sub PrintSelectionToPDF()
    Dim invoiceRng As Range
    Dim strfile As String
    'Setting range to be printed
    Set invoiceRng = Range("A1:D40")
    'setting file name with a time stamp.
    strfile = Range("C1") & " - " & Format(Now(), "ddmmyy") & ".pdf"
    'setting the fulli qualified name. The resultent pdf will be saved where the main file exists.
    strfile = ThisWorkbook.Path & "\" & "Fee Calculation - " & strfile '<= Added "\"

    invoiceRng.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strfile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End Sub
 
Upvote 0
Hi and welcome to the forum. Try this or if that doesnt work try a \ instead of the /:
strfile = ThisWorkbook.Path & "/Fee Calculation - " & strfile

Thank you, you're a legend, worked perfectly!!
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,520
Members
448,968
Latest member
Ajax40

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