Testing for Open Workbook

dcanham

Active Member
Joined
Jun 7, 2006
Messages
306
Hi All,
I'm trying to test for an existing Excel instance with a specific workbook. Now I may have to deal with several instances of Excel open at the same time on the workstation. I would like a way in VBA to look through what excel instances are open see if the desired workbook has been opened in them. This isn't to be confused with having multiple workbooks open in the same instance. I have tried several different ways in vain, some of them iterated below. I would appreciate any work code examples explaining what an dumkoff I am.

...this never detects the open workbook

Code:
Function OpenBook(sBookName As String)
    Dim k As Integer
    
    'Checks the names of all open  books
    For k = 1 To Workbooks.Count
        If Workbooks(k).Name = sBookName Then
            ' activate already open workbook
            Workbooks(k).Activate
            Exit Function
        End If
    Next k
     
    ' open workbook
    RetVal = Shell(("EXCEL.EXE " + Chr(34) + sFileBasePath + sBookName + Chr(34)), 1)
'    Workbooks.Open (sFileBasePath + sBookName)
End Function

...this doesn't work either
Code:
Sub TryThis()
    Dim WIsOpen As Workbook
    
    On Error Resume Next
    Set WIsOpen = Workbooks("c:\sheets\Planners.xls")
    If WIsOpen Is Nothing Then
        MsgBox "I'm not Open"
    Else
        MsgBox "I'm already Open"
    End If
    On Error GoTo 0
End Sub
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Instead of trying to look through other instances of Excel ( which I don't think is possible by the way ), why not test to see if Excel has allowed you write access or read-only access? Or are you testing a shared book?
 
Upvote 0
I would have to start-up another instance of Excel to test that....and I'm not sure of a way to have a function return that status as a parameter? The idea behind what I'm trying to do is test if an excel instance is open with the workbook I want. If "yes" than maximize it, if "no" then start a new instance with that workbook. Right now, it is just getting the test to work correctly that is giving me grief.
 
Upvote 0
I would have to start-up another instance of Excel to test that....and I'm not sure of a way to have a function return that status as a parameter? The idea behind what I'm trying to do is test if an excel instance is open with the workbook I want. If "yes" than maximize it, if "no" then start a new instance with that workbook. Right now, it is just getting the test to work correctly that is giving me grief.

Can you explain where you'll be doing this testing? You are talking as if you are doing your testing outside of Excel.
 
Upvote 0
I was going to make a little user form with launch buttons for the different sheets used. Much time is spent going between different applications and different excel spreadsheets. When the button on the user form is pushed, an excel instance is checked for that is running the desired sheet, if found to be running it is maximixed, otherwise a new instance of excel is started with the desired workbook.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,520
Members
448,968
Latest member
Ajax40

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top