Consulting

Results 1 to 6 of 6

Thread: PDF file not saving in right area

  1. #1
    VBAX Regular
    Joined
    Feb 2021
    Posts
    33
    Location

    PDF file not saving in right area

    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
    Last edited by Aussiebear; 02-23-2025 at 08:12 PM. Reason: Changed code tags to Code

  2. #2
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    149
    Location
    I'm not sure if this is a typo, but this line should at least read:
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & fname
    However, you'll still have a format problem with the variable dt_bill because a file name can't use slashes.
    Difficult is not to know but to share what you know (Han Fei Tzu reworked)

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,379
    Location
    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
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Regular
    Joined
    Feb 2021
    Posts
    33
    Location
    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?

  5. #5
    VBAX Regular
    Joined
    Feb 2021
    Posts
    33
    Location
    Actually ignore the above. I've fixed the issue. Thank you for your help.

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,379
    Location
    And you fixed the issue how?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •