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,665 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tags from access to excel 3 27
Search Form with Run Time Error 3075 1 22
Dropdown Not In List - not working correctly 11 35
VBA code won't run Delete Query 5 0
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now