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 a file exists 1

Status
Not open for further replies.

skierm

Programmer
Jun 9, 2003
39
US
How can i check to see if a file exists before saving and then if it does to ask to change the name of the file your are saving.
 
Check if a file exists:

If Dir("filepath and name") = "file name" Then
' File Exists
End If

So:

If Dir("C:\Documents And Settings\Word\Test.doc") = "Test.doc" Then
' File Exists
End If

As to changing the name, that will ultimatley depending on how you are currently working your setup....
Please post you sub or rotuine that is doing the work of saving the file so we can determine the appropriate place to put the name change.

****************************
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. (Albert Einstein)

Robert L. Johnson III
MCSA, CNA, MCP, Network+, A+
w: robert.l.johnson.iii@citigroup.com
h: wildmage@tampabay.rr.com
 
You can also use...

If dir(path) <>&quot;&quot; then
'file exists
endif

or
If dir(path)=&quot;&quot; then
' file does not exist
endif


 
Bulls,

You are correct. I always forget about just checking for <> &quot;&quot;....Thanks.

****************************
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. (Albert Einstein)

Robert L. Johnson III
MCSA, CNA, MCP, Network+, A+
w: robert.l.johnson.iii@citigroup.com
h: wildmage@tampabay.rr.com
 
basically im calling this line
excel_sheet.saveas &quot;C:\data\file.xls&quot;
and if that file exist i want it to prompt for a new file name.
thanks
 
OKay.....here's how I would do this...

Dim blnSave as Boolean
Dim strPath as String
strPath = &quot;C:\data\file.xls&quot;
blnSave = False
If Dir(strPath) <> &quot;&quot; Then
Do until blnSave = True
strPath = InputBox(&quot;File Exists. Please enter new path and name.&quot;)
If Dir(strPath) = &quot;&quot; Then blnSave = True
Loop
End If
excel_sheet.saveas strPath


****************************
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. (Albert Einstein)

Robert L. Johnson III
MCSA, CNA, MCP, Network+, A+
wildmage@tampabay.rr.com
 
i also have a question about msgbox

if im using yesno format how do i access the answers do i set something=msgbox..... or what thanks
 
see new thread you posted...

****************************
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. (Albert Einstein)

Robert L. Johnson III
MCSA, CNA, MCP, Network+, A+
wildmage@tampabay.rr.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top