Save Excel File as PDF With Duplicate Name Check

tim220225

New Member
Joined
Jun 4, 2012
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have the code below to convert and save the active worksheet as a PDF in a specific file location. Works great for what I am doing. How can this be amended to prompt if the file name being saved already exists in the folder to overwrite the existing folder or cancel the operation?


Sub SaveAsPDF()

'Saves active worksheet as pdf using Range

'of AX2,AX4'

Dim fName As String

With ActiveSheet

fName = .Range("AX2").Value & "." & .Range("B14").Value & " " & .Range("AX4").Value

.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _

"C:\Users\Tim M\Dropbox\PCL\PCL Work Orders\Tractor Internal Work Orders\" & fName, Quality:=xlQualityStandard, _

IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

End With

End Sub
 
Last edited:

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)
Replace the ExportAsFixedFormat line with this code:
VBA Code:
    If Dir("C:\Users\Tim M\Dropbox\PCL\PCL Work Orders\Tractor Internal Work Orders\" & fname) <> vbNullString Then
        If MsgBox(fname & " exists.  Do you want to overwrite it?", vbYesNo) = vbYes Then
            'Put ExportAsFixedFormat line here
        End If
    End If
 
Upvote 0
Replace the ExportAsFixedFormat line with this code:
VBA Code:
    If Dir("C:\Users\Tim M\Dropbox\PCL\PCL Work Orders\Tractor Internal Work Orders\" & fname) <> vbNullString Then
        If MsgBox(fname & " exists.  Do you want to overwrite it?", vbYesNo) = vbYes Then
            'Put ExportAsFixedFormat line here
        End If
    End If
I believe I did as you suggested. When I run the code I get the Sub SaveAsPDF() line highlighted in yellow with the compile error: Expected End Sub Microsoft VBA message. What am I doing wrong here?


Sub SaveAsPDF()
'Saves active worksheet as pdf using Range
'of AX2,AX4'

Dim fName As String
With ActiveSheet
fName = .Range("AX2").Value & "." & .Range("B14").Value & " " & .Range("AX4").Value

If Dir("C:\Users\Tim M\Dropbox\PCL\PCL Work Orders\Tractor Internal Work Orders\" & fName) <> vbNullString Then
If MsgBox(fName & " exists. Do you want to overwrite it?", vbYesNo) = vbYes Then

.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\Tim McLeod\Dropbox\Port City Logistics\PCL Work Orders\Tractor Internal Work Orders\" & fName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End If
End If
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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