Creating Unique PDF name using VBA in Excel

dan_sherbs

New Member
Joined
May 14, 2014
Messages
25
Hi,

I have a tool that produces a set of outputs in the form of a quote in a tab called "SalesQuotation".

On this worksheet, i have a button which when clicked runs the following VBA

Code:
Sheets("SalesQuotation").Range("A1:J70").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\" & Environ$("Username") & _
    "\Desktop\Customer_QUOTE.pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True

This works fine which opens up the quote. However, whilst this is open & i click the button again i get a run time error relating to Document not Saved.

How can i prevent this? Will the filename have to be unique or will it need to overwrite the existing one?

Many Thanks
Dan
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How can i prevent this? Will the filename have to be unique or will it need to overwrite the existing one?

Either would work. Which do you want: close and overwrite the previous file, or create a new file with a unique name?

Unique names base on date-time

Code:
Sheets("SalesQuotation").Range("A1:J70").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\" & Environ$("Username") & _
        "\Desktop\Customer_QUOTE[COLOR=#ff0000]" & Format(Now, " mmddyy hhmmss") & "[/COLOR].pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
 
Last edited:
Upvote 0

Forum statistics

Threads
1,222,562
Messages
6,166,804
Members
452,073
Latest member
akinch

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