ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check before overwriting file? (https://www.excelbanter.com/excel-programming/420712-check-before-overwriting-file.html)

Fan924

Check before overwriting file?
 
I am using the following to select a file name and then open file for
writing. It writes to the filename even if it is already used. Is
there a way to have it check before overwriting file? Examples?
________________________________

SaveFileName = Application.GetSaveAsFilename("C:\My Documents
\temp.hex", _
"Hex File (*.hex), *.hex", , "Save File As:")

Open SaveFileName For Output As #1
________________________________

Chip Pearson

Check before overwriting file?
 
Use something like

If Dir(SaveFileName) < vbNullString Then
' file exists
Else
' file does not exist
End If

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Mon, 1 Dec 2008 12:29:11 -0800 (PST), Fan924
wrote:

I am using the following to select a file name and then open file for
writing. It writes to the filename even if it is already used. Is
there a way to have it check before overwriting file? Examples?
________________________________

SaveFileName = Application.GetSaveAsFilename("C:\My Documents
\temp.hex", _
"Hex File (*.hex), *.hex", , "Save File As:")

Open SaveFileName For Output As #1
________________________________


Fan924

Check before overwriting file?
 
Thanks Chip. I did a search using your example and found this. It
increments the file name until it finds a unique one. It was missing
an ELSE so it didn't work at first.


dim filename as string
dim try as long
try=1
another:
filename = "c:\test_" & try & ".txt"
if dir(filename)<vbnullstring then 'the fiel exists! try another
try=try+1
goto another
msgbox("unused filename is " & filename)



All times are GMT +1. The time now is 12:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com