Solved

Word/Access VBA - SaveAs2 Method: How do I save as a different file name if file already exists?

Posted on 2013-01-26
1
1,716 Views
Last Modified: 2013-01-26
Word 2010
Document.SaveAs2 method

RE: FileName Parameter

Microsoft MSDN Library states the following regarding the FileName parameter "The name for the document. The default is the current folder and file name. If the document has never been saved, the default name is used (for example, Doc1.doc). If a document with the specified file name already exists, the document is overwritten without prompting the user.
"

How do I save a file using a different name if that file name already exists?

I have an Access database that uses the Word.Application Object to generate multiple fax sheets from a table then saves each fax sheet into a folder. Sometimes the file names clash. If the file names clash then the previously saved fax sheet is overwritten. I would like to simulate the Windows Save As feature of appending a (1), (2), etc. to the file name if the file name already exists.

I generated VBA from Word using the macro recorder to save a document then pasted that vba into a button click event in Access. That's how I arrived at using the SaveAs2 method in the first place.

Thank you.
0
Comment
Question by:CafeTica
1 Comment
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 38822770
You can determine whether a file already exists with the Dir() function. Itwill return the name of the file (without path) if the file already exists.  You could then wrap that in a loop to test various names until a valid version is identified, something like:
Dim strFilename As String
Dim varVersion As Variant
strFilename = "E:\Access\Output\Test"
varVersion = Null

Do While Len(Dir(strFilename & "(" + varVersion + ")" & ".docx")) > 0
    varVersion = CStr(Val(Nz(varVersion, 0)) + 1)
Loop
strFileName = strFilename & "(" + varVersion + ")" & ".docx")

Open in new window

Because varVersion is initially set to NULL, the parenthesis in the above algorithm will not be included during the first pass, but after that, they will.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question