Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 216
Default File Exists Killing Workbook_Open()

I have a little code that saves the current file as a
user input name as soon as the file is opened. I am
trying to figure out how to keep the code from breaking
when the user chooses a filename that exists.

The following code was suggested:


fname = Application.InputBox( _
Prompt:="Please enter file name",
Title:="File name", Type:=2)
Do Until Dir(fname) = ""
If fname = "False" Then Exit Sub 'user clicked
Cancel
fname = Application.InputBox(Prompt:="FileName
Exists Please Try Again.", Title:="File Exists",
Type:=2)
Loop


ThisWorkbook.SaveAs Filename:=fname



I haven't been able to get this to work. I am not sure
if I am doing something wrong or not.

Any help with a solution is greatly appreciated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default File Exists Killing Workbook_Open()

Hi Mike
what is your exact problem with the code below. It should work. did you
get an error message?

--
Regards
Frank Kabel
Frankfurt, Germany

Mike wrote:
I have a little code that saves the current file as a
user input name as soon as the file is opened. I am
trying to figure out how to keep the code from breaking
when the user chooses a filename that exists.

The following code was suggested:


fname = Application.InputBox( _
Prompt:="Please enter file name",
Title:="File name", Type:=2)
Do Until Dir(fname) = ""
If fname = "False" Then Exit Sub 'user clicked
Cancel
fname = Application.InputBox(Prompt:="FileName
Exists Please Try Again.", Title:="File Exists",
Type:=2)
Loop


ThisWorkbook.SaveAs Filename:=fname



I haven't been able to get this to work. I am not sure
if I am doing something wrong or not.

Any help with a solution is greatly appreciated.


  #3   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default message unavailable

This what I see.

If anyone can explain this to me so I can make it quit
Please do. you can email me recs_4u@bellsouth. net

Thanks
-----Original Message-----
Message unavailable

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default File Exists Killing Workbook_Open()

Frank

Thanks for the tips....

Ok what it does it seems to skip by the Do condition.. Like it is always
true.

So even if the filename chosen exists it still goes the saveas. Then I get
the standard do you want to overwrite message from excel. Of courseif I
choose no the macro dies.


"Frank Kabel" wrote in message
...
Hi Mike
what is your exact problem with the code below. It should work. did you
get an error message?

--
Regards
Frank Kabel
Frankfurt, Germany

Mike wrote:
I have a little code that saves the current file as a
user input name as soon as the file is opened. I am
trying to figure out how to keep the code from breaking
when the user chooses a filename that exists.

The following code was suggested:


fname = Application.InputBox( _
Prompt:="Please enter file name",
Title:="File name", Type:=2)
Do Until Dir(fname) = ""
If fname = "False" Then Exit Sub 'user clicked
Cancel
fname = Application.InputBox(Prompt:="FileName
Exists Please Try Again.", Title:="File Exists",
Type:=2)
Loop


ThisWorkbook.SaveAs Filename:=fname



I haven't been able to get this to work. I am not sure
if I am doing something wrong or not.

Any help with a solution is greatly appreciated.






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default File Exists Killing Workbook_Open()

Does this happen when you *don't* enter an extension?

This is a bit more robust:


Const sPROMPT1 As String = "Please enter file name."
Const sPROMPT2 As String = "Filename exists. Please try again."
Dim bValidName As Boolean
Dim sFName As String

Do
sFName = Application.InputBox( _
Prompt:=IIf(Len(sFName) = 0, sPROMPT1, sPROMPT2), _
Title:="File Name", _
Type:=2)
If sFName = "False" Then Exit Sub 'user clicked cancel
If Right(sFName, 4) = ".xls" Then _
sFName = Left(sFName, Len(sFName) - 4)
If Len(sFName) 0 Then bValidName = (Dir(sFName & ".xls") = "")
Loop Until bValidName
ThisWorkbook.SaveAs Filename:=sFName & ".xls"


In article ,
"Michael Hardy" wrote:

Ok what it does it seems to skip by the Do condition.. Like it is always
true.

So even if the filename chosen exists it still goes the saveas. Then I get
the standard do you want to overwrite message from excel. Of courseif I
choose no the macro dies.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default File Exists Killing Workbook_Open()

Perfect.. Is there anyway to make it like vbokonly?
"JE McGimpsey" wrote in message
...
Does this happen when you *don't* enter an extension?

This is a bit more robust:


Const sPROMPT1 As String = "Please enter file name."
Const sPROMPT2 As String = "Filename exists. Please try again."
Dim bValidName As Boolean
Dim sFName As String

Do
sFName = Application.InputBox( _
Prompt:=IIf(Len(sFName) = 0, sPROMPT1, sPROMPT2), _
Title:="File Name", _
Type:=2)
If sFName = "False" Then Exit Sub 'user clicked cancel
If Right(sFName, 4) = ".xls" Then _
sFName = Left(sFName, Len(sFName) - 4)
If Len(sFName) 0 Then bValidName = (Dir(sFName & ".xls") = "")
Loop Until bValidName
ThisWorkbook.SaveAs Filename:=sFName & ".xls"


In article ,
"Michael Hardy" wrote:

Ok what it does it seems to skip by the Do condition.. Like it is always
true.

So even if the filename chosen exists it still goes the saveas. Then I

get
the standard do you want to overwrite message from excel. Of courseif I
choose no the macro dies.



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default File Exists Killing Workbook_Open()

Hmmm... perfect, but needs a tweak...

Can't do it using InputBox. Easily done with a Userform.


In article ,
"Michael" wrote:

Perfect.. Is there anyway to make it like vbokonly?

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default File Exists Killing Workbook_Open()

Well in relation to the question asked the answer was absolutely perfect.
It was the question that needed the tweak actually.


Thanks a ton for all of the help I have gotten.




"JE McGimpsey" wrote in message
...
Hmmm... perfect, but needs a tweak...

Can't do it using InputBox. Easily done with a Userform.


In article ,
"Michael" wrote:

Perfect.. Is there anyway to make it like vbokonly?



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.588 / Virus Database: 372 - Release Date: 2/13/2004


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
File Exists Formula El Bee Excel Worksheet Functions 4 November 9th 06 03:45 PM
Determine if a File Exists Connie Excel Discussion (Misc queries) 1 November 8th 06 09:11 AM
File Exists Mike McLellan Excel Discussion (Misc queries) 2 May 4th 06 09:20 AM
the file already exists - do you want to replace the existing file? Paul James[_3_] Excel Programming 4 December 12th 03 02:50 AM
File|Close w/Save fires Workbook_Open twice Mike Preston Excel Programming 6 August 28th 03 10:54 PM


All times are GMT +1. The time now is 07:19 PM.

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

About Us

"It's about Microsoft Excel"