View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
JE McGimpsey JE McGimpsey is offline
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.