VBScript - Activating an Excel Window Already Opened?

MEUserII

Board Regular
Joined
Oct 27, 2017
Messages
91
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
I am trying to write a VBScript (.vbs file) that will allow me to activate the current Excel window opened which is just the default "Book1" window after clicking "Blank Workbook" after opening Excel 2016 running on my Windows 10 PC, and activate that Window by bringing it on top (like as if I clicked on that window).

So, far I have only the following VBScript code:
Code:
CreateObject("Wscript.Shell").AppActivate GetObject(, "Excel.Application").Name

Which I was able to create from the Microsoft Support Documentation here: https://support.microsoft.com/en-us...eobject-behavior-of-office-automation-servers

However, I believe my above code doesn't work because it just retrieves "Microsoft Excel" for the name of the window instead of "Book1" as shown below using MessageBox in VBScript to see the output.
Code:
Dim ABCD
WScript.Sleep (1*1000)
ABCD=MsgBox(GetObject(, "Excel.Application").Name, 1, "MACRO STATUS")

So, how would I be able to activate this instance of Excel by retrieving the name "Book1"?
I want to know how to retrieve the window name of the Excel window that is open (for any Excel window open) for using with AppActivate that way I can generalize this code to working with any given already open Excel windows instead of always manually entering the window name, like entering "Book1" in the VBScript example below:
Code:
CreateObject("Wscript.Shell").AppActivate("Book1")
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Will there be only one instance of excel opened at a time or more ?
 
Upvote 0
I have just tested your code and it worked fine for activating the current and only running excel instance with book1 in it.
Code:
[COLOR=#574123]CreateObject("Wscript.Shell").AppActivate GetObject(, "Excel.Application").Name[/COLOR]
 
Upvote 0
Hi,
Using .Caption instead of .Name is more correct for different language localizations.
In the below code maximizing of the Excel main window is also applied for the case it is in the minimized state.
Code:
Const xlMaximized = -4137
Dim XL
Set XL = GetObject(, "Excel.Application")
XL.Windows(1).WindowState = xlMaximized
CreateObject("Wscript.Shell").AppActivate XL.Caption
 
Last edited:
Upvote 0
@ ZVI

Hi vladimir,

Does that work for you when the excel application is minimized to the taskbar ?

Regards.
 
Upvote 0
Hi Jaafar,

Yes it works regardles Excel (2016 64bit, Win10) application is minimized or not.
May be excluding of .Windows(1) is more correct: XL.WindowState = xlMaximized

Vlad
 
Last edited:
Upvote 0
Hi Jaafar,

Yes it works regardles Excel (2016 64bit, Win10) application is minimized or not.
May be excluding of .Windows(1) is more correct: XL.WindowState = xlMaximized

Vlad

Thank you .

XL.WindowState=xlMaximized worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,435
Members
452,326
Latest member
johnshaji

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