Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Checking for open workbook...

I want to add some code to a workbook_BeforeClose event.
For example sake, lets call this Workbook1. I want to
sort the contents of Workbook1!Sheet1 only if Workbook2
is not open.

How can I test to see if another Workbook is open?

Thanks
Mike
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Checking for open workbook...

Hi Mike
try the following

Dim WBook As Workbook
'...
On Error Resume Next
Set WBook = Workbooks("Workbook2")
On Error GoTo 0
If WBook Is Nothing Then
'your sorting code
End If
'...



--
Regards
Frank Kabel
Frankfurt, Germany

Squid wrote:
I want to add some code to a workbook_BeforeClose event.
For example sake, lets call this Workbook1. I want to
sort the contents of Workbook1!Sheet1 only if Workbook2
is not open.

How can I test to see if another Workbook is open?

Thanks
Mike

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Checking for open workbook...

Mike,

Here is a simple function to check

Function IsOpen(FileName As String) As Boolean
Dim oWB As Workbook
On Error Resume Next
Set oWB = Workbooks(FileName)
IsOpen = Not oWB Is Nothing
End Function

Use like this

Debug.Print IsOpen("SIP 2004.xls")

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Squid" wrote in message
...
I want to add some code to a workbook_BeforeClose event.
For example sake, lets call this Workbook1. I want to
sort the contents of Workbook1!Sheet1 only if Workbook2
is not open.

How can I test to see if another Workbook is open?

Thanks
Mike



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default Checking for open workbook...

Or even shorter ;-):

Function IsOpen(FileName As String) As Boolean
On Error Resume Next
IsOpen = CBool(Len(Workbooks(FileName).Name))
End Function

It even works without the CBool but I don't like ot use implicit coercion.

--

Vasant


"Bob Phillips" wrote in message
...
Mike,

Here is a simple function to check

Function IsOpen(FileName As String) As Boolean
Dim oWB As Workbook
On Error Resume Next
Set oWB = Workbooks(FileName)
IsOpen = Not oWB Is Nothing
End Function

Use like this

Debug.Print IsOpen("SIP 2004.xls")

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Squid" wrote in message
...
I want to add some code to a workbook_BeforeClose event.
For example sake, lets call this Workbook1. I want to
sort the contents of Workbook1!Sheet1 only if Workbook2
is not open.

How can I test to see if another Workbook is open?

Thanks
Mike





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Checking for open workbook...

Always got to go one better<vbg.

I think I'll steal that.

Bob

"Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message
...
Or even shorter ;-):

Function IsOpen(FileName As String) As Boolean
On Error Resume Next
IsOpen = CBool(Len(Workbooks(FileName).Name))
End Function

It even works without the CBool but I don't like ot use implicit coercion.

--

Vasant


"Bob Phillips" wrote in message
...
Mike,

Here is a simple function to check

Function IsOpen(FileName As String) As Boolean
Dim oWB As Workbook
On Error Resume Next
Set oWB = Workbooks(FileName)
IsOpen = Not oWB Is Nothing
End Function

Use like this

Debug.Print IsOpen("SIP 2004.xls")

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Squid" wrote in message
...
I want to add some code to a workbook_BeforeClose event.
For example sake, lets call this Workbook1. I want to
sort the contents of Workbook1!Sheet1 only if Workbook2
is not open.

How can I test to see if another Workbook is open?

Thanks
Mike









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default Checking for open workbook...

Hi Bob:

I'm sure I stole it from someone else ... probably Dana DeLouis <g.

Regards,

Vasant.


"Bob Phillips" wrote in message
...
Always got to go one better<vbg.

I think I'll steal that.

Bob

"Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message
...
Or even shorter ;-):

Function IsOpen(FileName As String) As Boolean
On Error Resume Next
IsOpen = CBool(Len(Workbooks(FileName).Name))
End Function

It even works without the CBool but I don't like ot use implicit

coercion.

--

Vasant


"Bob Phillips" wrote in message
...
Mike,

Here is a simple function to check

Function IsOpen(FileName As String) As Boolean
Dim oWB As Workbook
On Error Resume Next
Set oWB = Workbooks(FileName)
IsOpen = Not oWB Is Nothing
End Function

Use like this

Debug.Print IsOpen("SIP 2004.xls")

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Squid" wrote in message
...
I want to add some code to a workbook_BeforeClose event.
For example sake, lets call this Workbook1. I want to
sort the contents of Workbook1!Sheet1 only if Workbook2
is not open.

How can I test to see if another Workbook is open?

Thanks
Mike








  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default Checking for open workbook...

"Vasant Nanavati" wrote...
Or even shorter ;-):

Function IsOpen(FileName As String) As Boolean
On Error Resume Next
IsOpen = CBool(Len(Workbooks(FileName).Name))
End Function

...

If you want shorter, you might as well go for fewer function calls too.

Function IsFile(fn As String) As Boolean
On Error Resume Next
IsFile = (Workbooks(fn).Name < "")
End Function

--
To top-post is human, to bottom-post and snip is sublime.
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default Checking for open workbook...

Hi Harlan:

You had made a similar "enhancement" the last time I posted this solution,
but I couldn't remember what it was. Thanks!

Regards,

Vasant.

"Harlan Grove" wrote in message
...
"Vasant Nanavati" wrote...
Or even shorter ;-):

Function IsOpen(FileName As String) As Boolean
On Error Resume Next
IsOpen = CBool(Len(Workbooks(FileName).Name))
End Function

..

If you want shorter, you might as well go for fewer function calls too.

Function IsFile(fn As String) As Boolean
On Error Resume Next
IsFile = (Workbooks(fn).Name < "")
End Function

--
To top-post is human, to bottom-post and snip is sublime.



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
Checking to see if a Workbook is Open Todd Huttenstine[_3_] Excel Programming 4 December 25th 03 11:55 PM
Checking if workbook is open Jesse[_4_] Excel Programming 6 December 11th 03 11:17 PM
Checking if workbook open (where path is unknown) [email protected] Excel Programming 0 September 4th 03 11:13 PM
Checking for Open Workbook sbharbour Excel Programming 6 August 28th 03 11:42 PM
checking if workbook is open before accessing David Goodall Excel Programming 1 August 22nd 03 08:43 PM


All times are GMT +1. The time now is 02:28 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"