I created a file for a client that has (among other things) macros to save copies of various sheets as PDF files. The PDFs are saved in a specified folder that is under the folder containing the Excel file.
Everything worked fine until they apparently moved the Excel file to a SharePoint folder, and are accessing it through OneDrive. Now when they try to save the PDF, they get a runtime error 52 (Bad file name or number).
The code checks to see whether the standard filename already exists, and if so, it adds a sequential "Ver00" to the filename. Curiously, I have an error handler in place should an error occur during the filesave operation, but the macro is NOT reaching that error handler. So I'm thinking it's tripping the error 52 when running the Dir command. But why would this crash when the file is on SharePoint, and not when it's on a local drive? I have no experience with SharePoint / OneDrive so I am clueless on how to help. Any suggestions would be appreciated!
By the way, neither the Subdirectory nor the RefText contain any prohibited characters.
The code is:
Sub CreateSubTrans_PDFFile()
' Generates a PDF file of the active SubTrans sheet
Dim CheckName As String
Dim counter As Integer
Dim FilePathAndName As String
Dim RefText As String
Dim Response As Integer
Dim Subdirectory As String
Subdirectory = [ProjectInfo_FileLocationTransmittals]
' Limit REF to 15 characters
RefText = Left([SubTrans_Reference], 15)
' Export the print area as a PDF file
FilePathAndName = ThisWorkbook.Path & Subdirectory & "\S-" & Format([SubTrans_TransmittalNumber], "00") & "-" & RefText & "-" & Format(Now(), "m-d-yy")
CheckName = FilePathAndName & ".pdf"
counter = 0
If Dir(CheckName) <> "" Then 'File already exists
counter = 2
CheckName = FilePathAndName & "-Ver" & Format(counter, "00") & ".pdf"
Do
If Dir(CheckName) = "" Then Exit Do 'Filename does not exist
counter = counter + 1
CheckName = FilePathAndName & "-Ver" & Format(counter, "00") & ".pdf"
Loop
End If
If counter > 0 Then FilePathAndName = FilePathAndName & "-Ver" & Format(counter, "00")
On Error GoTo FileSaveError
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FilePathAndName
On Error GoTo 0
GoTo ExitThisSub
FileSaveError:
Response = MsgBox("The file path or filename:" & vbCrLf & vbCrLf & FilePathAndName & vbCrLf & vbCrLf & "is INVALID! Check that:" _
& vbCrLf & "? Target subdirectory exists" & vbCrLf & "? Filename does not contain invalid characters (such as ?<>/: etc.)" _
& vbCrLf & "Edit the Reference entry in the source form to remove any invalid characters.", vbOKOnly, "File Save Error")
ExitThisSub:
End Sub
Bookmarks