Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get workbook name of running excel instance using vbscript..?

Tags:

excel

vbscript

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.. :))

like image 749
Pratik Sanghvi Avatar asked Jan 14 '23 17:01

Pratik Sanghvi


1 Answers

If you potentially have more than one excel 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"""
like image 113
brettdj Avatar answered Mar 29 '23 23:03

brettdj