4
Dim objXL, strMessage

On Error Resume Next

Set objXl = GetObject(, "Excel.Application")

If Not TypeName(objXL) = "Empty" then
    strMessage = "Excel Running"    
Else 
    strMessage = "Excel NOT Running"
End If

MsgBox strMessage, vbInformation, "Excel Status"

Hey thanks alot buddy. This really brings me close to what am looking for, moving much closer to the solution. Let me tell you my exact requirement/issue: Actually my issue is that, from Java I am trying to find Excel instance with a particular workbook name but am not returned an Excel instance even though it appears. In my case I have an Excel opened with 2 workbooks "Book1" and "Book2" in it. When am trying to find Excel with any of these workbook name, am given no result. To narrow down, this issue is observed only on one of my client machines. On rest machines this same java code working fine. This started happening after uninstalling Excel2010 and installing Excel2007.

So what I am trying to do is that, want to create one vbscript where I can give the workbookname as an input and it will return me whether there is such Excel instance running with given workbook name.

Hey please guide me further towards creating such script where I will give the workbook name and script will find whether such Excel instance is running or not. Not an issue even if workbook name is passed as an hardcoded input in script. I will alter as per my workbook name.

Thanks for your previous reply and awaiting for this one too.. :))

Pratik Sanghvi
  • 41
  • 1
  • 1
  • 4

3 Answers3

6

If you potentially have more than one instance open than to detect if a specific workbook is open you could use:

  1. This code to examine all open workbooks in all instances Can VBA Reach Across Instances of Excel?
  2. Detect if the file is already in use. See Sid's suggestion from Detect whether Excel workbook is already open
  3. Doug's suggestion to use GetObject to attach to a host instance where you know the workbook name. As per the Microsoft Support article you can use Set xlApp = GetObject("YourExcelName").Application to detect if "YourExcelName" is open in any instance

In the question that you initially asked, the code below uses GetObject to detect whether any instance is open, and if there is an ActiveWorkbook and what that name ie. From your edited question my three links above are more relevant than this code.

Dim objXL, WB, strMessage
On Error Resume Next
Set objXL = GetObject(, "Excel.Application")
Set WB = objXL.ActiveWorkbook
On Error GoTo 0
If Not TypeName(objXL) = "Empty" Then
    If Not TypeName(WB) = "Nothing" Then
    strMessage = "Excel Running - " & objXL.ActiveWorkbook.Name & " is active"
    Else
    strMessage = "Excel Running - no workbooks open"
    End If
Else
    strMessage = "Excel NOT Running"
End If
MsgBox strMessage, vbInformation, "Excel Status"""
Community
  • 1
  • 1
brettdj
  • 54,857
  • 16
  • 114
  • 177
  • 2
    +1 Sandwiching both of them between OERN and OEGT0 early in the code is the best way :) – Siddharth Rout Apr 10 '13 at 05:56
  • 1
    Thanks a lot man. :) You have almost made my task easy. Need some more help. Have updated my question with the entire requirement. Please help me doing that. Awaiting for your generous responses. – Pratik Sanghvi Apr 10 '13 at 10:17
  • My judgment is contrariwise to @SiddharthRout, -0.5 for wrapping `ActiveWorkbook` in OERN instead of using `Workbooks.Count`. – seeker Apr 10 '13 at 20:47
  • @seeker -0.25 to seeker. :) `Workbooks.Count` can return `1` for the personal workbook which acts as an addin - but is not available. Plus the initial question asked for the `ActiveWorkbook` - it has been edited – brettdj Apr 10 '13 at 21:46
  • @brettdj still not see the reason of OERN, see http://msdn.microsoft.com/en-us/library/office/aa174744(v=office.11).aspx – seeker Apr 10 '13 at 23:18
  • @seeker. Try running `MsgBox "The name of the active workbook is " & ActiveWorkbook.Name` inside Excel without *any* workbooks open. It errors out. – brettdj Apr 10 '13 at 23:45
  • @brettdj Yes, that's true, `Workbooks.Count` was bad idea, but what's wrong in `ActiveWorkbook Is Nothing` test? – seeker Apr 11 '13 at 00:01
2

I not use Excel, and I hope that the next code may give you a starting point. But if you have many Excel instances running then should investigate future more yourself.

Dim objXL, strName, bFound, strMsg

On Error Resume Next
Set objXl = GetObject(, "Excel.Application")
On Error GoTo 0
If Err Then
    MsgBox "Excel NOT Running", vbInformation, "Excel Status"
    WScript.Quit(-1)
End If

strName = InputBox("Enter Workbook Name:", "Required")
If Len(strWBName) = 0 Then WScript.Quit(-2)

bFound = False
If objXL.Workbooks.Count > 0 Then
    For Each wb In objXL.Workbooks
        If wb.Name = strName Then
            bFound = True
            Exit For
        End If
    Next
End If

strMsg = "Workbook " & UCase(strName) & " is "
If bFound Then
    MsgBox strMsg & "open", vbInformation, "Result"
Else
    MsgBox strMsg & "not open", vbInformation, "Result"
End If

P.S. After reading Brettdj updated answer looks like GetObject can help for multiple Excel instances, so if that works you can capsule your test in a function.

Function IsWBookOpen(strWBook)
    On Error Resume Next
    Dim wb: Set wb = GetObject(strWBook)
    IsWBookOpen = Not Err
End Function
Panayot Karabakalov
  • 3,109
  • 3
  • 19
  • 28
1

You can capture the active workbook name with the following command. objXL.ActiveWorkbook.Name

Dim objXL, strMessage

On Error Resume Next

Set objXL = GetObject(,"Excel.Application")

If Not TypeName(objXL) = "Empty" then
    strMessage = "Excel Running"    
    WScript.Echo "The active workbook name is " & objXL.ActiveWorkbook.Name
Else 
    strMessage = "Excel NOT Running"
End If

MsgBox strMessage, vbInformation, "Excel Status"
J P
  • 274
  • 1
  • 2
  • 7