VBA ExportAsFixedFormat - fails when there is no existing file and only succeeds when there is an existing file

krispykitchen

New Member
Joined
Jul 27, 2023
Messages
2
Office Version
  1. 365
Platform
  1. MacOS
Totally confounded by this error. Sub has been working perfectly for some time and today stopped working out of the blue.
Error code is 1004 - "Method "ExportAsFixedFormatType" of object _Worksheet failed."
The sub takes a worksheet as a parameter then exports it to a pdf in a specific folder and gives it a specific file name.
As I was debugging what I found stumps me - if a pdf with that name already existed in that folder then it would proceed without error. If there was NOT an existing pdf with the exact same filename, it would fail. Taking the example below - this snippet fails. However if I was to change the 4 to a 3 it would complete because there is already a file in that folder called "BEF Invoice3.pdf". If I were to go and create a file in that folder called "BEF Invoice4.pdf" then run it again, it would complete without failing.

VBA Code:
PathName = "/Users/kristen/Library/CloudStorage/OneDrive-Personal/Invoicing and Legals/Invoices/Drafts/"
SvAs = PathName & "BEF Invoice[SIZE=6][B][COLOR=rgb(226, 80, 65)]4[/COLOR][/B][/SIZE].pdf"

' Instruct user how to send
    'On Error GoTo RefLibError
        ChDir PathName
        ws.ExportAsFixedFormat Type:=xlTypePDF, fileName:=SvAs _
            , Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=False


Here's the full code - noting that I have a caller sub and a subordinate sub because there is also another caller sub which iterates through a list and calls the subordinate sub for each:

VBA Code:
Private Sub Save_PDF(ByVal ws As Worksheet)   ' Copies sheets into new PDF file for e-mailing
    Dim Thissheet As String, ThisFile As String, PathName As String
    Dim SvAs As String

    'get info about the invoice to derive the pdf name
    Dim invNum As Long, invDate As Date, invOurRef As String
    invNum = ws.Range("fldInvoiceNumber").Value
    invDate = ws.Range("fldInvoiceDate").Value
    invOurRef = Replace(ws.Range("fldInvoiceOurRef").Value, " ", "")
    Dim vFName As String
    vFName = "INV" & invNum & "_" & invOurRef & "_" & Format(invDate, "yyyyMMdd") & ".pdf"
    Debug.Print vFName

    PathName = "/Users/kristen/Library/CloudStorage/OneDrive-Personal/Invoicing and Legals/Invoices/Drafts/"
    SvAs = PathName & "BEF Invoice4.pdf". 'vFName NOTING HERE THAT IT WAS USING THE VARIABLE BUT WHEN DEBUGGING I FOUND THAT THE FILE NAME WAS CAUSING THE ERR SO I TESTED IT WITH A HARDCODED FILENAME WHICH IS HOW I CLOCKED THE STRANGE BEHAVIOUR
    
'Set Print Quality
    On Error Resume Next
    ws.PageSetup.PrintQuality = 600
    Err.Clear
    On Error GoTo 0

    'On Error GoTo RefLibError
        ChDir PathName
        ws.ExportAsFixedFormat Type:=xlTypePDF, fileName:=SvAs _
            , Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=False
    'On Error GoTo 0
    
SaveOnly:
    'MsgBox "A copy of this sheet has been successfully saved as a  pdf: " & vbCrLf & vbCrLf & SvAs
    
RefLibError:
    'MsgBox "Unable to save as PDF. Reference library not found."
End Sub

I'm stumped! Got any ideas?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
For anyone who stumbles on this later - it spontaneously started working again the following day so i can only assume it was some strange OneDrive connectivity issue.
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,571
Members
452,652
Latest member
eduedu

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