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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

735 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