Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 921
Default GetSaveAsFilename alwyas overwrites existing file

I am exporting a text file from within my macro, but it ALWAYS overwrites an
existing file without asking me if I want to replace it?

Anybody know what I did wrong?

FName = Application.GetSaveAsFilename("Rename_me.gft", filefilter:="GIFT
files _
(*.gft), *.gft,Text files, *.txt,All Files, *.*", _
Title:="Please pick a file name for your GIFT file")
If FName = False Then
myMessage = "You didn't select a file." & Chr(10) & Chr(10) & "Export
has been ABORTED!"
GoTo MyError:
End If

THanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default GetSaveAsFilename alwyas overwrites existing file

GetSaveAsFilename only askes the user for a filename: it's up to you to
check if it exists and if so then ask the user what they want to do.

You don't show the code which does the save: post that and I'm sure you'll
get a suggestion on how to handle it.

Tim


"jeff" wrote in message
...
I am exporting a text file from within my macro, but it ALWAYS overwrites
an
existing file without asking me if I want to replace it?

Anybody know what I did wrong?

FName = Application.GetSaveAsFilename("Rename_me.gft", filefilter:="GIFT
files _
(*.gft), *.gft,Text files, *.txt,All Files, *.*", _
Title:="Please pick a file name for your GIFT file")
If FName = False Then
myMessage = "You didn't select a file." & Chr(10) & Chr(10) & "Export
has been ABORTED!"
GoTo MyError:
End If

THanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 921
Default GetSaveAsFilename alwyas overwrites existing file

Oops... forgot the save part of the macro!!!

OKay, thanks Tim... so I guess my question has now changed to "How do I
check to see if the file already exists?" Here's the whole chunk of code I
already have. There are no problems with it all, as far as the actual export
goes, it's just that it overwrites the existing file if it already exists.

'---Pick a filename and location to save---
FName = Application.GetSaveAsFilename("Rename_me.gft", _
filefilter:="GIFT files (*.gft), *.gft,Text files, *.txt,All Files,
*.*", _
Title:="Please pick a file name for your GIFT file")
If FName = False Then
myMessage = "You didn't select a file." & Chr(10) & _
Chr(10) & "Export has been ABORTED!"
GoTo MyError:
End If
'***Need code HERE to check if the file (FName) already exists!!!!***
'---Export To Text File---
Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile
Open FName For Output Access Write As #FNum
For q = 0 To 100
If qGIFT(q) < "" Then Print #FNum, qGIFT(q)
Next q
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum



"Tim Williams" wrote:

GetSaveAsFilename only askes the user for a filename: it's up to you to
check if it exists and if so then ask the user what they want to do.

You don't show the code which does the save: post that and I'm sure you'll
get a suggestion on how to handle it.

Tim


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default GetSaveAsFilename alwyas overwrites existing file

'---Pick a filename and location to save---
FName = Application.GetSaveAsFilename("Rename_me.gft", _
filefilter:="GIFT files (*.gft), *.gft,Text files, *.txt,All Files,
*.*", _
Title:="Please pick a file name for your GIFT file")
If FName = False Then
myMessage = "You didn't select a file." & Chr(10) & _
Chr(10) & "Export has been ABORTED!"
GoTo MyError:
End If
if dir(fName) < "" then
ans = Msgbox("File exists, overwrite?", vbYesNo)
if ans = vbNo then exit sub
end if
Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile
Open FName For Output Access Write As #FNum
For q = 0 To 100
If qGIFT(q) < "" Then Print #FNum, qGIFT(q)
Next q
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum

--
Regards,
Tom Ogilvy


"jeff" wrote in message
...
Oops... forgot the save part of the macro!!!

OKay, thanks Tim... so I guess my question has now changed to "How do I
check to see if the file already exists?" Here's the whole chunk of code
I
already have. There are no problems with it all, as far as the actual
export
goes, it's just that it overwrites the existing file if it already exists.

'---Pick a filename and location to save---
FName = Application.GetSaveAsFilename("Rename_me.gft", _
filefilter:="GIFT files (*.gft), *.gft,Text files, *.txt,All Files,
*.*", _
Title:="Please pick a file name for your GIFT file")
If FName = False Then
myMessage = "You didn't select a file." & Chr(10) & _
Chr(10) & "Export has been ABORTED!"
GoTo MyError:
End If
'***Need code HERE to check if the file (FName) already exists!!!!***
'---Export To Text File---
Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile
Open FName For Output Access Write As #FNum
For q = 0 To 100
If qGIFT(q) < "" Then Print #FNum, qGIFT(q)
Next q
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum



"Tim Williams" wrote:

GetSaveAsFilename only askes the user for a filename: it's up to you to
check if it exists and if so then ask the user what they want to do.

You don't show the code which does the save: post that and I'm sure
you'll
get a suggestion on how to handle it.

Tim




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 921
Default GetSaveAsFilename alwyas overwrites existing file

Thanks Tom!

It's a very simple solution, isn't it!!!!

if dir(FileName) < "" then
ans = Msgbox("File exists, overwrite?", vbYesNo)
if ans = vbNo then exit sub
end if

I also found another example it the following post (almost the same question
asked at almost the same time)!!!

Subject: SaveFileName in Excel
3/10/2007 9:05 PM PST
By: TedT
In: microsoft.public.excel.programming

Thanks again!


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
User replaces existing file with blank during file open sjs Excel Discussion (Misc queries) 3 August 18th 08 06:16 PM
Macro to combine worksheets data - overwrites existing [email protected] Excel Discussion (Misc queries) 5 May 1st 08 08:54 PM
Worksheets.add in VBScript overwrites existing worksheet donparkerjr Excel Worksheet Functions 3 November 9th 06 07:09 PM
Importing text file, only option to edit existing file smokey99 Excel Discussion (Misc queries) 8 April 26th 06 09:08 PM
File save location with Application.GetSaveAsFilename Brad Patterson Excel Programming 1 July 21st 03 04:01 AM


All times are GMT +1. The time now is 02:50 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"