LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
jai jai is offline
external usenet poster
 
Posts: 71
Default workbooks.open and error handling

Hi John,

Firstly the problem with your code.
Workbooks("Filename.xls") works only if the workbook is part of the
workbooks collection ie if it is already open. Since at the point of
invocation, it is not open, it will give an error. You may set a workbook
variable to it after opening the file without error.

What you need to do is to
1. Determine if the file is existing in the given path. Use the Dir
function
dim Fil as String
Fil = dir(path &"\" filename.xls") will return an empty string if
the file is not in the given path, else it will return the filename. You may
use wild cards on Windows - apparantly does not work on Macs.

2. Determine if the workbook is already open. To do so, you have to
search the workbooks collection

Dim wb as workbook, isOpen as Boolean
IsOpen = False
for each wb in workbooks
if wb.name = "xyz.xls" then
IsOpen = True
exit For
endif
end for
[use IsOpen logical variable at this point to check]



"John Keith" wrote:

Im getting an error when running this code on the Set. (I found this code as
a solution on another post, but it won't seem to work.)

thedir = CurDir()
On Error GoTo notOpen
Set xlTest = Workbooks("ThePlayingboard.xls") ***
GoTo itsOpenNow
notOpen:
Workbooks.Open Filename:=theDir & "\" & "ThePlayingboard.xls",
UpdateLinks:=0
itsOpenNow:
Workbooks("ThePlayingboard.xls").Worksheets("Comba t").Activate

At the *** marker, this is the line that gets a "runtime error 9, subscript
out of range" which I assume is the Workbooks(sub) its talking about. and
this Should happen because the file was not open. I was under the impression
that the On Error GoTo label would trap the error and force the execution
pointer to jump on past the error.

I'm just trying to make sure the file is open, if it is open then Activate,
else open it then activate. How do you do that?

--
Regards,
John

 
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
Error Handling - Check a file isn't already open before overwritin NDBC Excel Discussion (Misc queries) 4 August 13th 09 08:36 AM
Error handling with a handling routine ben Excel Programming 0 March 15th 05 03:01 PM
Excel 2003 Workbooks.Open with CorruptLoad=xlRepairFile fails on Excel 5.0/95 file due to Chart, with Error 1004 Method 'Open' of object 'Workbooks' failed Frank Jones Excel Programming 2 June 15th 04 03:21 AM
Error Handling Open Function or query for missing Files BigNate Excel Programming 3 June 11th 04 05:22 PM
error on oXL.Workbooks.Open arieribbens Excel Programming 3 April 26th 04 10:09 AM


All times are GMT +1. The time now is 04:04 AM.

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"