VBA warning message when saving PDF over existing file

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
370
Hi, would it be possible to have a warning box pop up to prevent saving the PDF over an existing file? Also, can we have the box show the existing filename and be able to change the name and then save it? Thanks

Code:
Sub DETAIL_PDF()
    Dim response As String
    Dim PrintAreaString As String
    response = InputBox("Enter column letter for 2nd part of range", "Enter Data")
    If response = Cancel Then
    Exit Sub
    End If
      Application.ScreenUpdating = False
      With ActiveSheet
        PrintAreaString = "A3:$" & Trim(UCase(response)) & "$" & .Range("B1").Value
        If response <> "" Then
            .PageSetup.PrintArea = PrintAreaString
            .PageSetup.Orientation = xlLandscape
            .PageSetup.Zoom = False
            .PageSetup.FitToPagesWide = 1
            .PageSetup.FitToPagesTall = False
            .PageSetup.LeftMargin = 36
            .PageSetup.TopMargin = 72
            .PageSetup.RightMargin = 36
            .PageSetup.BottomMargin = 36
    End If
    End With
    fileSaveName = ActiveWorkbook.Path & "\" & "FRIEDLAND QUOTE " & [B6] & " " & "JOB " & [B7] & " " & [A15] & " " & [AB15] & " " & [AD15] & [B9] & " PCS" & " " & Format(Date, "mmddyy")
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        fileSaveName _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True '(change this to "False" to prevent the file from opening after saving)
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
spotted an error post deleteded - see below
 
Last edited:
Upvote 0
Insert immediately below line beginning fileSaveName = ActiveWorkbook.Path.....
Code:
Dim fpath As String [COLOR=#006400][I]'declare with other variables[/I][/COLOR]

TestExistence:
fpath = Left(fileSaveName, InStrRev(fileSaveName, "\"))
If Not Dir(fileSaveName & ".pdf", vbDirectory) = vbNullString Then
    fileSaveName = fpath & InputBox("Amend file name to continue!", "PDF File Exists!", Replace(fileSaveName, fpath, ""))
    GoTo TestExistence
End If
 
Last edited:
Upvote 0
Hi, pretty much works fine except if you hit "cancel" in the input box it returns a run time error 2147024773.
 
Upvote 0
minor modification to allow value of input box to be assessed

you did not say what should happen upon cancel
- here procedure exits without saving pdf
- amend to suit your own requirements

Code:
Dim fPath As String [I][COLOR=#006400]'declare with other variables[/COLOR][/I]
Dim fName As String

TestExistence:
    fPath = Left(fileSaveName, InStrRev(fileSaveName, "\"))
    fName = Replace(fileSaveName, fPath, "")
    If Not Dir(fileSaveName & ".pdf", vbDirectory) = vbNullString Then
        On Error Resume Next
        fName = InputBox("Amend file name to continue!", "PDF File Exists!", fName)
        If fName = "" Then
            MsgBox "Cancelled, not saved,...!"
            Exit Sub
        Else
            fileSaveName = fPath & fName
        End If
        GoTo TestExistence
    End If
 
Last edited:
Upvote 0
Another way, without the GoTo and On Error statements.

Code:
    Dim fileSaveName As String, filePath As String

    fileSaveName = "FRIEDLAND QUOTE " & [B6] & " " & "JOB " & [B7] & " " & [A15] & " " & [AB15] & " " & [AD15] & [B9] & " PCS" & " " & Format(Date, "mmddyy")
    
    filePath = ActiveWorkbook.Path & "\" & fileSaveName & ".pdf"
    While Dir(filePath) <> vbNullString And fileSaveName <> ""
        fileSaveName = InputBox("The PDF for " & fileSaveName & " already exists." & vbCrLf & vbCrLf & "Please enter a new file name:", "Save as PDF", fileSaveName)
        filePath = ActiveWorkbook.Path & "\" & fileSaveName & ".pdf"
    Wend
        
    If fileSaveName <> "" Then
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=filePath, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True '(change this to "False" to prevent the file from opening after saving)
    Else
        MsgBox "PDF not created"
    End If
 
Upvote 0
Hi, Thanks. I forget that the cancel part has to be coded. Next time I think I can do it myself!
 
Upvote 0
Hi, Would it be possible to revise the code so when the filename exists we could write over it when hitting OK. But it still needs to show the filename in the box with the option to edit it. Thanks
 
Upvote 0
Try this:
Code:
    Dim fileSaveName As String, filePath As String
    Dim reply As Variant
    
    fileSaveName = "FRIEDLAND QUOTE " & [B6] & " " & "JOB " & [B7] & " " & [A15] & " " & [AB15] & " " & [AD15] & [B9] & " PCS" & " " & Format(Date, "mmddyy")
    
    filePath = ActiveWorkbook.Path & "\" & fileSaveName & ".pdf"
    
    reply = vbNo
    While Dir(filePath) <> vbNullString And fileSaveName <> "" And reply = vbNo
        reply = MsgBox("The PDF for " & fileSaveName & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", vbYesNo, "Save as PDF")
        If reply = vbNo Then
            fileSaveName = InputBox("Please enter a new file name:", "Save as PDF", fileSaveName)
        End If
        filePath = ActiveWorkbook.Path & "\" & fileSaveName & ".pdf"
    Wend
        
    If fileSaveName <> "" Then
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=filePath, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True '(change this to "False" to prevent the file from opening after saving)
    Else
        MsgBox "PDF not created"
    End If
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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