PDA

View Full Version : [SOLVED:] PDF file not saving in right area



SteveABC
02-23-2025, 03:59 PM
Hi.

I've written the below code, but the PDF is not saving in the below path and is saving the PDF into a different path. I can't see where it saves it to because the save box only appears for a sec. Can't see what I'm doing wrong. I'm simply saving the active sheet as a PDF. I've selected a particular range within the worksheet as a set printed area. Any ideas?



Sub Save_PDF()
Dim invno As Long
Dim custname As String
Dim Amt As Currency
Dim dt_issue As Date
Dim Path As String
Dim fname As String
Dim Nextrec As Range
Dim dt_bill As Date
invno = Range("c20")
custname = Range("c1")
Amt = Range("n48")
dt_issue = Range("d20")
dt_bill = Range("n20")
Path = "Q:\Finance\Accounts\Finance\"
fname = dt_bill & " " & custname & " " & invno
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF
End Sub

rollis13
02-23-2025, 05:22 PM
I'm not sure if this is a typo, but this line should at least read:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fnameHowever, you'll still have a format problem with the variable dt_bill because a file name can't use slashes.

Aussiebear
02-23-2025, 11:51 PM
Does this work for you?



Sub Save_PDF()
Dim invno As Long
Dim custname As String
Dim Amt As Currency
Dim dt_issue As Date
Dim Path As String
Dim fname As String
Dim dt_bill As Date
Dim formattedDate As String
' Get values from specified cells
invno = Range("c20").Value
custname = Range("c1").Value
Amt = Range("n48").Value
dt_issue = Range("d20").Value
dt_bill = Range("n20").Value
' Specify the path to save the PDF
Path = "Q:\Finance\Accounts\Finance\"
' Format the date for the file name
formattedDate = Format(dt_bill, "yyyy-mm-dd") ' Use a file-safe format
fname = formattedDate & " " & custname & " " & invno & ".pdf"
' Export the active sheet as a PDF to the specified path
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Path & fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
MsgBox "PDF saved as: " & vbCrLf & fname
' Optionally use Debug.Print to see the full path in the Immediate Window
Debug.Print "PDF saved successfully at: " & Path & fname
End Sub

SteveABC
02-24-2025, 03:57 AM
Brilliant. Thank you. I amended the formatted date to dd-mmm-yyyy. But the filename still pulls through an erroneous date. The date in range N20 is an xlookup result but still in the format dd-mmm-yyyy. Any ideas why the date doesn't show correctly?

SteveABC
02-24-2025, 03:59 AM
Actually ignore the above. I've fixed the issue. Thank you for your help.

Aussiebear
02-24-2025, 04:57 AM
And you fixed the issue how?