Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for an easy way to test to see if excel spreadsheet exists 1

Status
Not open for further replies.

irethedo

Technical User
Feb 8, 2005
429
US
Is there an easy way to test to see if an excel spreadsheet exists to avoid the
error message that happens when trying to open a spreadsheet for export when it doesn't exists?

The idea is to create a new spreadsheet if one doesn't already exist

Thanks
 
How about somethine like
Code:
dim fso, spreadsheet
spreadsheet = "C:\users\x\documents\yyy.xlsx"
set fso = CreateObject("Scripting.FileSystemObject")
if (fso.FileExists(spreadsheet) Then
  WScript.Echo("File exists!")
else
  WScript.Echo("create spreadsheet")
  ...
end if
' Do something with the spreadsheet
 
Thanks xwb

I took what you suggested and applied the variables that I was already using but not sure if I messed something up
as I am getting a Run-time error '438' Object doesn't support this property or method error on the [highlight #FCE94F]highlighted line below[/highlight]

Here is what my variables and code looks like as my interpretation of what you are suggesting:

Code:
    Set objXL = CreateObject("Excel.Application")
    Set db = CurrentDb
    [highlight #FCE94F]If objXL.FileExists(Out_File) Then[/highlight]
            Set objWkb = .Workbooks.Open(Out_File)
         Else
            Set objWkb = .Workbooks.Add
            With objWkb
             .SaveAs Out_File, 51
            End With
         End If

What sis I do wrong?

thanks
 
FileExists is not an Excel VBA object. It is a File System method, as previously pointed out by xwb

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top