krispykitchen
New Member
- Joined
- Jul 27, 2023
- Messages
- 2
- Office Version
- 365
- Platform
- 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.
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:
I'm stumped! Got any ideas?
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?