SaveAs - prevent overwriting existing file?

F

fisch4bill

I've encountered a problem with VBA code that is performing the SaveAs
function: it doesn't seem to give me the same warning about the file already
existing as using SaveAs from the Word menu manually. I seem to remember that
there is a programmatic way of determining if a particular file name exists
in a target directory, I just don't remember what it is. I tried

If Exists(\\servername\directoryname\filename) Then . . .

both enclosed in quotes and not, and tried the path property, both with no
success. Can anyone help me with this? How can I check to see if a specific
file name exists in a directory using VBA?

TIA
Bill
 
F

Fumei2 via OfficeKB.com

Correct. If you use SaveAs on the ActiveDocument via VBA, VBA assumes you
know what you are doing, and it will do the SaveAs. No message.

You can use FileSystemObject (requires Microsoft Scripting Runtime as a
Reference!).

Dim fso As Scripting.FileSystemObject
Dim strToSaveAs As String

strToSaveAs = "c:\zzz\Suji\Test.doc"
Set fso = CreateObject("Scripting.Filesystemobject")

If fso.FileExists(strToSaveAs) Then
MsgBox "This file alrteady exists. Sorry, try again."
Exit Sub
Else
ActiveDocument.SaveAs Filename:=strToSaveAs
End If


Or some sort of cersion thereof.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top