How to get the title of a book to run an excel instance using 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" 

Hi thank you very much. It really brings me closer to what I'm looking for, much closer to a solution. Let me tell you my exact requirement / question: Actually, my problem is that with Java I try to find an Excel instance with a specific workbook name, but I did not return the Excel instance, even if it appears. In my case, I have Excel open with two books, Book1 and Book2. When I try to find Excel with any of these book titles, I did not get any result. To narrow this problem, this problem is observed only on one of my client machines. On rest computers, the same Java code works fine. It started after uninstalling Excel2010 and installing Excel2007.

So what I'm trying to do is that you want to create one vbscript where I can specify the name of the workbook as input, and it will return to me if there is an Excel instance working with the given title of the book.

Hey, please direct me further to creating such a script where I will give the name of the book, and the script will find out if this Excel instance works or not. Not a problem, even if the name of the workbook is passed as hard input to the script. I will change according to the name of my book.

Thanks for your previous answer and expect this too .. :))

+4
source share
3 answers

If you potentially have more than one excel open than to determine if a particular workbook is open, you can use:

In the question you originally asked, the code below uses GetObject to determine if any instance is ActiveWorkbook and if there is an ActiveWorkbook and what that name means. 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""" 
+6
source

I do not use Excel, and I hope that the following code can give you a starting point. But if you have many instances of Excel, you should investigate the future more.

 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 

PS After reading Brettdj, the updated answer looks like this: GetObject can help for multiple instances of Excel, so if that works, you can encapsulate your test in a function.

 Function IsWBookOpen(strWBook) On Error Resume Next Dim wb: Set wb = GetObject(strWBook) IsWBookOpen = Not Err End Function 
+2
source

You can write the name of the active book 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" 
+1
source

All Articles