VBA save as PDF overwrite warning code

drefiek2

Board Regular
Joined
Apr 23, 2023
Messages
59
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi,
I have the following code which saves the sheet as a PDF in a particular folder. It works as desired. I need some code to add to this which will check the folder to see if the PDF already exists. It should produce an overwrite warning if it does and ask the user if they would like to continue or not.

VBA Code:
 Select Case MsgBox("Is the date and shift type correct?", vbYesNo Or vbQuestion, Application.Name)
    Case vbNo
        Debug.Print "User exit"
        Exit Sub
    End Select
    Dim SharePointPath As String
    Dim PdfFileName As String
    Dim msg As String

    On Error GoTo SaveError

    SharePointPath = "FOLDER"

    PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("J6").Value
    
    If Worksheets("Mechanics").Range("B15").Value = True Then
    Call DarkMode
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SharePointPath & PdfFileName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Call DarkMode
    Else
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SharePointPath & PdfFileName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End If

    msg = "Handover saved. You can now close the spreadsheet."
    MsgBox msg, vbInformation, "Upload Successful"
    Exit Sub

SaveError:
    msg = "Handover not saved. Please contact X on e-mail and use the backup document for today." & vbCr & vbCr & Err.Number & " - " & Err.Description
    MsgBox msg, vbCritical, "Upload Failure"
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
After this line:
VBA Code:
PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("J6").Value

Put this:
VBA Code:
  If Dir(SharePointPath & PdfFileName & ".pdf") <> "" Then
    If MsgBox("PDF already exists. Continue", vbExclamation + vbYesNo) = vbNo Then
      Exit Sub
    End If
  End If
 
Upvote 0
After this line:
VBA Code:
PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("J6").Value

Put this:
VBA Code:
  If Dir(SharePointPath & PdfFileName & ".pdf") <> "" Then
    If MsgBox("PDF already exists. Continue", vbExclamation + vbYesNo) = vbNo Then
      Exit Sub
    End If
  End If
Hi, thanks for your reply.
I receive error 52 - bad file name or number. I expect the issue will be my SharePointPath which is a http SharePoint folder (mapped to everyone's local C: OneDrives). I did temporarily change the SharePointPath to my C:Downloads folder to test it though and the code works.
My question now I suppose is what can we do to swap out the https:// path in favour of a local C: OneDrive path. Bearing in mind 5-6 people will be using this spreadsheet and therefore I cannot put my own C:Drive user on there. The code will need to adapt to whoever it using it. Everyone has the same OneDrive path to the folder e.g. C:\Users\USER\OneDrive - COMPANY\Folder
 
Upvote 0
C:\Users\USER\OneDrive - COMPANY\Folder
So in your original code, replace this:
VBA Code:
  SharePointPath = "FOLDER"
  PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("J6").Value

For this
VBA Code:
  SharePointPath = Environ("USERPROFILE") & "\OneDrive - COMPANY\Folder\"
  PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("J6").Value
 
  If Dir(SharePointPath & PdfFileName & ".pdf") <> "" Then
    If MsgBox("PDF already exists. Continue", vbExclamation + vbYesNo) = vbNo Then
      Exit Sub
    End If
  End If
Note: Beware the path separator is now "\"
I don't know what you have in cell "/" or "\"

Replace(Range("D6").Value, "/", "")
 
Upvote 0
Solution
So in your original code, replace this:
VBA Code:
  SharePointPath = "FOLDER"
  PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("J6").Value

For this
VBA Code:
  SharePointPath = Environ("USERPROFILE") & "\OneDrive - COMPANY\Folder\"
  PdfFileName = Replace(Range("D6").Value, "/", "") & ActiveSheet.Range("J6").Value
 
  If Dir(SharePointPath & PdfFileName & ".pdf") <> "" Then
    If MsgBox("PDF already exists. Continue", vbExclamation + vbYesNo) = vbNo Then
      Exit Sub
    End If
  End If
Note: Beware the path separator is now "\"
I don't know what you have in cell "/" or "\"

Replace(Range("D6").Value, "/", "")
Works perfectly! D6 is a date cell with / separators which are illegal save characters so I wanted to remove them out of the file name. Thanks for your help!
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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