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,853 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

739 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