VBA Check if SharePoint PDF file already exists before saving to SharePoint folder

drefiek2

Board Regular
Joined
Apr 23, 2023
Messages
59
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Greetings everyone,
I have searched around for this but I am struggling to find help for this particular matter. I have a module button which saves a sheet as a PDF in a particular SharePoint folder with the file name from cell D6 (including removing illegal characters) and D7 - it works perfectly. If it saves successfully I get a message box, if it doesn't I get a message box - both work perfectly (see full code below). Note I have made the SharePoint details random for posting here.

VBA Code:
Sub SaveAsPDF()
'
' SaveAsPDF Macro
'
' Keyboard Shortcut: Ctrl+Shift+S
'
    Dim SharePointPath As String
    Dim PdfFileName As String
    Dim msg As String

    On Error GoTo SaveError

    SharePointPath = "https://company.sharepoint.com/folder/"

    PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("D7").Value

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SharePointPath & PdfFileName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    msg = "Handover successfully delivered to SharePoint."
    MsgBox msg, vbInformation, "Save Successful"
    Exit Sub

SaveError:
    msg = "Handover was not delivered to SharePoint:" & vbCr & vbCr & Err.Number & " - " & Err.Description
    MsgBox msg, vbCritical, "Save Failure"
End Sub


What I am now keen for it to do, before the save is executed, is check whether the PDF already exists in the above SharePoint folder, if it does exist I want it to give a warning message box about overwriting (the user can still go ahead and save if they wish), if it doesn't exist then it should just save as normal. I tried the following but it did not work - it gave error 52 - bad file name or number. Can anyone assist with a corrected code or alternative code for doing this?

VBA Code:
Sub SaveAsPDF()
'
' SaveAsPDF Macro
'
' Keyboard Shortcut: Ctrl+Shift+S
'
    Dim SharePointPath As String
    Dim PdfFileName As String
    Dim msg As String

    On Error GoTo SaveError

    SharePointPath = "https://company.sharepoint.com/folder/"

    PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("D7").Value

    If Dir(SharePointPath & PdfFileName) <> "" Then
        MsgBox "File already exists"
    Else
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SharePointPath & PdfFileName, _
         Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

        msg = "Handover successfully delivered to SharePoint."
        MsgBox msg, vbInformation, "Save Successful"
    End If
    Exit Sub

SaveError:
    msg = "Handover was not delivered to SharePoint:" & vbCr & vbCr & Err.Number & " - " & Err.Description
    MsgBox msg, vbCritical, "Save Failure"
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,224,814
Messages
6,181,123
Members
453,021
Latest member
Justyna P

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