Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 238
Default 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
________________________________
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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
________________________________

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 238
Default 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)

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to prevent overwriting of file ? [email protected] Excel Programming 4 March 19th 08 01:09 PM
Rename and save without overwriting the exisiting file Thulasiram[_2_] Excel Programming 5 September 7th 07 06:09 PM
Prevent user from overwriting file peter.thompson[_45_] Excel Programming 5 January 14th 06 12:31 PM
overwriting a file Jordan Shoderu Excel Programming 4 August 6th 04 11:46 AM
Overwriting a file automatically using saveas in a macro Sam Excel Programming 5 May 5th 04 04:01 AM


All times are GMT +1. The time now is 04:59 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"