VBA to save file with version number

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
I have a macro that is saving as PDF.

I use a line of code to generate the filepath

Code:
filepath = "\\serv\company\shipping\invoices\" & FileName & ".pdf"


However I would like to then check if this file already exists and rename it to (1) or (2) .. etc


Any suggestions
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Just check for the existance of "\\serv\company\shipping\invoices\" & FileName & ".pdf" BEFORE saving and, if it exists, add a (1) or (2) to 'Filename'.
 
Upvote 0
I know how to open the file but not how to just check its there - Seems a bit long winded to attempt to open the file.
 
Upvote 0
Maybe like this

Code:
i = 0
Do While Dir("\\serv\company\shipping\invoices\" & Filename & ".pdf") <> ""
    i = i + 1
    Filename = Filename & " (" & i & ")"
Loop
 
Upvote 0
A little off topic, but can you post or send the code you use to save the excel workbook as a PDF? Thanks
 
Upvote 0
pretty bad practice I'm afraid.

Until I get a few PC's upgraded we won't be running office 2007 / 2010 which has it built in,.

Currently use cutepdf as the pdf writer and use a sendkeys command to handle the cutepdf interface.

Code:
    Newhour = Hour(Now())
    newMinute = Minute(Now())
    newSecond = Second(Now()) + 5
    FileName = Sheets("Data").Range("A3").Value
    FileName = FileName & " - " & Sheets("Calcs").Range("B14").Value
    FileName = FileName & " - " & Sheets("Calcs").Range("B18").Value
    FileName = Replace$(FileName, Mid$("/", 1, 1), "-")
    
        
    n = 0
    Do While Dir("\\serv\company\shipping\invoices\" & FileName & ".pdf") <> ""
        n = n + 1
        FileName = FileName & " -" & n
    Loop
    filepath = "\\serv\company\shipping\invoices\" & FileName & ".pdf"
    Sheets("Invoice edit").PrintOut Copies:=1, ActivePrinter:= _
    "CutePDF Writer on CPW2:", Collate:=True
 
    waittime = TimeSerial(Newhour, newMinute, newSecond)
    Application.Wait waittime

    SendKeys filepath & "{ENTER}", False


The 5 second delay on the waittime isn't really necissary but we have some pretty slow PC's and this guarantees the dialog box is definatly up.



Can't claim any credit for this code - Found after a bit of digging on this forum.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

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