testing for workbook already open

J

Janis

I have a macro that opens a form and workbook however I want to test to see
if the form is already opened by the user. This looks like it should work
but it doesn't like the line:
set wkb = GetObject.Openxxxxx

Public Sub openFile()


Const fName As String = "Book1.xls"
Const fPath As String = "Mac OS X:SSP Process:"

Dim wkb As Workbook

Set wkb = GetObject.Open(Filename:=fPath & fName)
If Err.Number <> 0 Then _
Set wkb = Workbooks.Open(Filename:=fPath & fName)
Err.Clear


End Sub


Thanks,
 
J

Jim Thomlinson

It should be something like this...

Public Sub openFile()

Const fName As String = "Book1.xls"
Const fPath As String = "Mac OS X:SSP Process:"

Dim wkb As Workbook

on error resume next
Set wkb = workbooks(fName)
on error goto 0
if wbk is nothing then _
Set wkb = Workbooks.Open(Filename:=fPath & fName)

End Sub
 
J

Jim Thomlinson

Sorry Typo wbk should be wkb...

Public Sub openFile()

Const fName As String = "Book1.xls"
Const fPath As String = "Mac OS X:SSP Process:"

Dim wkb As Workbook

on error resume next
Set wkb = workbooks(fName)
on error goto 0
if wkb is nothing then _
Set wkb = Workbooks.Open(Filename:=fPath & fName)

End Sub
 
J

Jim Thomlinson

That is the thing about computers. Simple is complicated and complicated is
simple. The mark of good spreadsheets and good code is that it looks simple...

As a complete aside you are really getting the hang of this. You are rapidly
making the leap from recorded code full of selects and other extraneous bits
to simple well strutured code dealing with the underlying object of Excel.
Keep up the good work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top