VBA exported excel to pdf but when saving need to check if file exists

lesleyfayburton

New Member
Joined
Dec 4, 2020
Messages
8
Office Version
  1. 2016
I tried to use the beginning of one script and add to it, but it's not working. When I am saving the file I want it to check to see if it already exists and if so the user can overwrite or save the new version (which will add a number from the cell range). Then close it out if it can't be saved. Any help you can offer I would appreciate it.

'Test to see if file name already exists
If Len(Dir(saveLocation & "\" & fName)) > 0 Then
If MsgBox(MsgBox("This ticket already exists, if you want to overwrite click Yes, if not, click No and a new version will be saved?", vbQuestion + vbYesNo)_
If answer = vbYes Then
rng.ExportAsFixedFormat Type:=xlTypePDF, FileName:=SaveAsStr, Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=False
MsgBox "Yes" & SaveAsStr = saveLocation & "\" & fName
Else
MsgBox "No" & SaveAsStr = SaveAsStr = saveLocation & "\" & fName & Sheets("Trade Ticket").Range("Z1")
rng.ExportAsFixedFormat Type:=xlTypePDF, FileName:=SaveAsStr, Quality:=xlQualityStandard, In
VBA Code:
cludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=False
End If

'Error Handler
NotSavedYet:
MsgBox "This file has not been initially saved. " & _
"Cannot save a new version!", vbCritical, "Not Saved To Computer"
End If
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,214,923
Messages
6,122,283
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