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
 

Some videos you may like

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,056
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
Hi and welcome to the forum. Try this or if that doesnt work try a \ instead of the /:
strfile = ThisWorkbook.Path & "/Fee Calculation - " & strfile
 
Solution

kanadaaa

Active Member
Joined
Dec 29, 2019
Messages
297
Office Version
  1. 365
Platform
  1. Windows
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
 

Lana_B

New Member
Joined
Feb 16, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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!!
 

Watch MrExcel Video

Forum statistics

Threads
1,127,374
Messages
5,624,319
Members
416,020
Latest member
ANDREAC247

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
Top