Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if file exist 1

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
NL
How do I check if a file exist before saving a file.
This to protect the file from overwrite without confirmation.

This is the code I'm using but it overwrites the file without asking if the file already exist;

Code:
If MsgBox("Save document as " & fname & "?", vbYesNo, "Confirmation save document") = vbYes Then
    ChangeFileOpenDirectory "R:\SEM-FIB\FIB Information Forms\"
    ActiveDocument.SaveAs FileName:=fname, FileFormat:=wdFormatDocument, _
        LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
        :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
        SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
        False
End If
 
Have a look at the Dir function.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You try my function, and modify as needed:
Function WorkBookExists(ByVal vPath As String, vBook As String, Optional msg As String = "Blank") As Boolean
'''''''''''''''''''''''''''''''
'Test to see if a Workbook exists
''''''''''''''''''''''''''''''''
If msg = "Blank" Then
msg = "Sorry, invalid name or path for workbook!" & vbCrLf & _
"Please reenter the name and path and try again!"
End If
Dim i As Integer
With Application.FileSearch
.LookIn = vPath
.Filename = vBook
If .Execute > 0 Then 'Workbook exists
WorkBookExists = True
Else 'There is Not a Workbook
MsgBox msg, vbCritical + vbOKOnly, "File Not Found!"
WorkBookExists = False
End If
End With
End Function

Enjoy
 

kosday, in your function [tt]msg[/tt] is an optional argument, which means you do not HAVE to provide it, that's why it is optional.

But if you decide to not provide it, you will get the message:
[tt]"Sorry, invalid name or path for workbook!" [/tt]
because you use its default value: "Blank"

So to bypass this, you HAVE to provide it, which is not Optional any more :) It is required.

Or am I missing something here......? :-(

Have fun.

---- Andy
 
Dim SaveIt As Boolean
SaveIt = True
If Len(Dir$(strPath2TargetFile)) Then
If MsgBox("The file exists do you want to overwrite it?", vbYesNo) = vbNo Then SaveIt = False
End If
If SaveIt Then
'code to save the file
End If
 


Ooops, sorry, never mind. I see the logic now :)

Have fun.

---- Andy
 
There is a big difference between using Dir and using Application.FileSearch.

Or there is now.

2007 versions of the Office applications do NOT have FileSearch exposed to VBA. You can not use it. In its wisdom Microsoft removed FileSearch from VBA starting in the 2007 applications.

Hugh, nice.

wmbb, unless you really need to, and in this case I do not see that you do, you have no need to ChangeFileOpenDirectory.

After all, that is a ChangeFileOpenDirectory value, and you are not opening files...you are saving them.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top