Link to home
Start Free TrialLog in
Avatar of Euro5
Euro5Flag for United States of America

asked on

Visual basic check if file exists

This code works only if the file does not already exist.
Can I add something to overwrite?

Sub PrintSelectionToPDFEDS()
Dim invoiceRng As Range
Dim pdfile As String
Worksheets("Scorecard").Activate
'Setting range to be printed
Set invoiceRng = Range("A1:J101")
'setting file name with a time stamp.
strfile = "_EDS" & ".pdf"
'setting the fulli qualified name. The resultent pdf will be saved where the main file exists.
pdfile = ThisWorkbook.path & "/Operation Scorecard" & strfile
invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
filename:=pdfile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub

Open in new window

Avatar of Daniel Pineault
Daniel Pineault

Use something like

If Len(Dir(pdfile)) > 0 Then Kill pdfile

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Euro5

ASKER

I am getting error on line 12,
User generated image
While I try to figure out why, uncomment line 5 which I commented for my original testing.
Try changing to this
strFile = Format(Now, " mm-dd-ss") & "_EDS" & ".pdf"

Your previous code was generating a filename that contained forward slashes like this:

C:\aatmpI\Operation Scorecard 06/27/45_EDS.pdf

rberke (aka unclebob)