I have this code :
Newdoc.SaveAs TextBox1 & ".doc"
It works except when the file "TextBox1.doc" already exist.
Is there a way to say : if the file "TextBox1.doc" exist then SaveAs "TextBox12.doc". ?
Printable View
I have this code :
Newdoc.SaveAs TextBox1 & ".doc"
It works except when the file "TextBox1.doc" already exist.
Is there a way to say : if the file "TextBox1.doc" exist then SaveAs "TextBox12.doc". ?
'does file exist using fso
Dim sFile$
sFile = "Name And Path Of Your File"
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.fileexists(sFile$) = True Then
MsgBox "File Exists...Your code here!"
Else
MsgBox "Does Not Exist...Your code here!"
End If
Set fs = Nothing
Thanks HeSaidJoe.
But what if I want a code that automatically save the document with a +1 at the end.
I mean if document1.doc exist then saveas document2.doc, if document2.doc exist then saveas document3.doc, and so on.
Do you have any idea ?
Here's a function (I used code from HeSaidJoe)
Click on Project|References and add "Microsoft Scripting Runtime"
I haven't tested it, but it should work.Code:' This will return True if saved, and False if not
Public Function SaveNextInstance(ByVal f as String) as Boolean
Dim fs As Scripting.FileSystemObject
Dim docnum as Integer
Set fs = New FileSystemObject
If fs.FileExists(f) then
On Error Goto ErrorHandler
docnum = Int(Val(Mid(f, Len(f) - 7, 3)
docnum = docnum + 1
f = Mid(f, 1, Len(f) - 7) & Format(docnum, "0##") & ".doc"
Else
On Error Goto ErrorHandler
f = f & "000.doc"
End if
NewDoc.SaveAs f
SaveNextInstance = True
Exit Function
ErrorHandler:
SaveNextInstance = False
End Function
Thanks for all this code, it'll help me a lot.