How to get handle to specific excel file among multiple opened excel files?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Sanjay
Posts: 17
Joined: 30 Oct 2016, 04:03

How to get handle to specific excel file among multiple opened excel files?

10 Feb 2019, 10:36

I have multiple excel files opened at the same time. For example A.xlsx, B.xlsx, C.xlsx, D.xlsx.
I want to get handle to only one specific excel file (A.xlsx) and then add data into or get data from it.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: How to get handle to specific excel file among multiple opened excel files?

10 Feb 2019, 12:17

try this

Code: Select all

path := "c:\a.xlsx"
WB := ComObjGet(path)
xl := WB.Application
Sanjay
Posts: 17
Joined: 30 Oct 2016, 04:03

Re: How to get handle to specific excel file among multiple opened excel files?

10 Feb 2019, 13:43

Thank you for your reply. But it doesn't work for me.

I am testing it on the following code.

Code: Select all

path := "C:\one.xlsx"
WB := ComObjGet(path)
XL := WB.Application
XL.range("A5").value := "HEllo4"
Above code doesnt add value to one.xlsx but instead it adds value to the excel file which was last maximized or currently maximized.
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to get handle to specific excel file among multiple opened excel files?

10 Feb 2019, 15:32

You don't need to get Application object in this case, you should get sheet object:

Code: Select all

XLBook := ComObjGet("C:\one.xlsx")
XLSht := XLBook.ActiveSheet
XLSht.Range("A5").Value := "Hello"
Sanjay
Posts: 17
Joined: 30 Oct 2016, 04:03

Re: How to get handle to specific excel file among multiple opened excel files?

10 Feb 2019, 17:17

teadrinker wrote:
10 Feb 2019, 15:32
You don't need to get Application object in this case, you should get sheet object:

Code: Select all

XLBook := ComObjGet("C:\one.xlsx")
XLSht := XLBook.ActiveSheet
XLSht.Range("A5").Value := "Hello"
Thank You For Your Reply. Your code is working fine. Thanks a lot.

I am using your code like below. And it is working fine. Guide me is there is any issue.

Code: Select all

try XL := ComObjActive("Excel.Application") ;handle to running application
Catch {
	XL := ComObjCreate("Excel.Application")
	Xl.Workbooks.Open("C:\one.xlsx")
	XL.Visible := 1 ;1=Visible/Default 0=hidden
}
XL.Visible := 1 ;1=Visible/Default 0=hidden

if !WinExist("one.xlsx - Excel")
{
	Xl.Workbooks.Open("C:\one.xlsx")
}

XLBook := ComObjGet("C:\one.xlsx")
XLSht := XLBook.ActiveSheet
XLSht.Range("A5").Value := "Hello"
Thanks once again
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to get handle to specific excel file among multiple opened excel files?

10 Feb 2019, 19:09

I'd change your code a bit to make it more correct:

Code: Select all

filePath := "C:\one.xlsx"

try XL := ComObjActive("Excel.Application")
catch {
   XL := ComObjCreate("Excel.Application")
   XL.Visible := 1
}
found := ""
for XLBook in Xl.Workbooks {
   if (XLBook.Path = filePath && found := true)
      break
}
if !found
   XLBook := Xl.Workbooks.Open(filePath)

XLSht := XLBook.ActiveSheet
XLSht.Range("A5").Value := "Hello"
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to get handle to specific excel file among multiple opened excel files?

10 Feb 2019, 19:35

Sanjay wrote:
10 Feb 2019, 10:36
I have multiple excel files opened at the same time. For example A.xlsx, B.xlsx, C.xlsx, D.xlsx.
I want to get handle to only one specific excel file (A.xlsx) and then add data into or get data from it.

A function I wrote for this purpose and more:

Code: Select all

; [Function] Excel_GetWB
; Fanatic Guru
; 2019 01 28
;
; Get Excel Workbook COM object
;	start Excel and open WB if needed
;
;{-----------------------------------------------
;
; Excel_GetWB(Path, Sheet, Visible)
;
; Parameters:
;	Path		path or file name of Excel workbook
;	Sheet	sheet name or index number to activate (Default: ActiveSheet)
;	Visible	visible state (Default: true)
;					"" results in current visible state with no change
;
; Example:
;	xlWB := Excel_GetWB(A_Desktop "\Test\specificExcelFile.xlsx", "Sheet4")
;	
;}
Excel_GetWB(Path, Sheet := "ActiveSheet", Visible := true)
{
	SplitPath, Path, FileName
	try
	{
		xlApp := ComObjActive("Excel.Application")
		for xlWB in xlApp.Workbooks
			if (xlWB.Name = FileName)
			{
				Found := true
				break
			}
	}
	catch
		xlApp := ComObjCreate("Excel.Application")
	if !Found
		xlWB := xlApp.Workbooks.Open(Path)
	xlWB.Activate
	if (Sheet != "ActiveSheet")
		try
			xlWB.Sheets(Sheet).Activate
	if (Visible != "")
		xlApp.Visible := Visible
	return xlWB
}
It will start Excel if needed and open the file if needed. Can also activate a specific sheet.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Sanjay
Posts: 17
Joined: 30 Oct 2016, 04:03

Re: How to get handle to specific excel file among multiple opened excel files?

11 Feb 2019, 03:25

teadrinker wrote:
10 Feb 2019, 19:09
I'd change your code a bit to make it more correct:

Code: Select all

filePath := "C:\one.xlsx"

try XL := ComObjActive("Excel.Application")
catch {
   XL := ComObjCreate("Excel.Application")
   XL.Visible := 1
}
found := ""
for XLBook in Xl.Workbooks {
   if (XLBook.Path = filePath && found := true)
      break
}
if !found
   XLBook := Xl.Workbooks.Open(filePath)

XLSht := XLBook.ActiveSheet
XLSht.Range("A5").Value := "Hello"
Thank You. Your code is working perfectly fine for me.
Sanjay
Posts: 17
Joined: 30 Oct 2016, 04:03

Re: How to get handle to specific excel file among multiple opened excel files?

11 Feb 2019, 03:37

FanaticGuru wrote:
10 Feb 2019, 19:35
Sanjay wrote:
10 Feb 2019, 10:36
I have multiple excel files opened at the same time. For example A.xlsx, B.xlsx, C.xlsx, D.xlsx.
I want to get handle to only one specific excel file (A.xlsx) and then add data into or get data from it.

A function I wrote for this purpose and more:

Code: Select all

; [Function] Excel_GetWB
; Fanatic Guru
; 2019 01 28
;
; Get Excel Workbook COM object
;	start Excel and open WB if needed
;
;{-----------------------------------------------
;
; Excel_GetWB(Path, Sheet, Visible)
;
; Parameters:
;	Path		path or file name of Excel workbook
;	Sheet	sheet name or index number to activate (Default: ActiveSheet)
;	Visible	visible state (Default: true)
;					"" results in current visible state with no change
;
; Example:
;	xlWB := Excel_GetWB(A_Desktop "\Test\specificExcelFile.xlsx", "Sheet4")
;	
;}
Excel_GetWB(Path, Sheet := "ActiveSheet", Visible := true)
{
	SplitPath, Path, FileName
	try
	{
		xlApp := ComObjActive("Excel.Application")
		for xlWB in xlApp.Workbooks
			if (xlWB.Name = FileName)
			{
				Found := true
				break
			}
	}
	catch
		xlApp := ComObjCreate("Excel.Application")
	if !Found
		xlWB := xlApp.Workbooks.Open(Path)
	xlWB.Activate
	if (Sheet != "ActiveSheet")
		try
			xlWB.Sheets(Sheet).Activate
	if (Visible != "")
		xlApp.Visible := Visible
	return xlWB
}
It will start Excel if needed and open the file if needed. Can also activate a specific sheet.

FG
Thank you for reply. Your function seems to be very useful. I just want to know the correct way to use it.
When I use your function like below. Then it throws error
Error: 0x80020006 - Unknown name.
Specifically: Range

Code: Select all

XL:= Excel_GetWB("C:\one.xlsx", "Sheet2")
XL.Range("A15").Value := "Hello"

But when I use your function like below then it is working fine.

Code: Select all

XL:= Excel_GetWB("C:\one.xlsx", "Sheet2")
XL.ActiveSheet.Range("A15").Value := "Hello"
I just want to know that above method is right method to use your function or not.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to get handle to specific excel file among multiple opened excel files?

11 Feb 2019, 14:26

Sanjay wrote:
11 Feb 2019, 03:37
But when I use your function like below then it is working fine.

Code: Select all

XL:= Excel_GetWB("C:\one.xlsx", "Sheet2")
XL.ActiveSheet.Range("A15").Value := "Hello"
I just want to know that above method is right method to use your function or not.
This is the nuances of Excel COM structure.

There are three main objects in Excel COM: Application, Workbook, WorkSheet.

Range is a method of the WorkSheet object.
https://docs.microsoft.com/en-us/office/vba/api/excel.worksheet.range

So you have to have a Worksheet object in order to use Range. But there is a wrinkle to this, if you just use Application.Range the application object will default to the ActiveSheet.

Excel_GetWB returns a Workbook object. Range is not a method of a Workbook object and Workbook will not default to the ActiveSheet like an Application object will.

It is important to know what kind of object you have in your variable as you start using more and more of the available COM methods, as all of them generally only act on a certain type of object.

Below is how I would do some things.

Code: Select all

xlWB:= Excel_GetWB("C:\one.xlsx", "Sheet2")
xlWB.ActiveSheet.Range("A15").Value := "Hello"

Code: Select all

xlSheet:= Excel_GetWB("C:\one.xlsx", "Sheet2").ActiveSheet
xlSheet.Range("A15").Value := "Hello"

Code: Select all

; Get all three main Excel objects
xlWB:= Excel_GetWB("C:\one.xlsx", "Sheet2")
xlSheet := xlWB.ActiveSheet
xlApp := xlWB.Parent
; other stuff
This could all be done in the function but then there is the problem of how to pass back three objects. And many people frown on just having them be global variables with names defined by the function.

Code: Select all

xlWB:= Excel_GetWB("C:\one.xlsx", "Sheet2") ; open Workbook and make a sheet active
Data := xlWB.ActiveSheet.Range("A15").Value
xlApp := xlWB.Parent ; Get the application object
xlApp.Quit ; Quit the applicatin object, shutdown Excel
MsgBox % Data
I don't know if it would be worth creating a function like: Excel_GetAWS(xlApp, xlWB, xlSheet, "C:\one.xlsx", "Sheet2") where you supply the three main variable names you want to use and the function puts the three main objects into those variables. Opening and creating them as needed. It is all just about the logistics of creating a parameters scheme that is easy to use.

It might be useful. I am going to play around with this type function.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Sanjay
Posts: 17
Joined: 30 Oct 2016, 04:03

Re: How to get handle to specific excel file among multiple opened excel files?

12 Feb 2019, 09:11

FanaticGuru wrote:
11 Feb 2019, 14:26
Sanjay wrote:
11 Feb 2019, 03:37
But when I use your function like below then it is working fine.

Code: Select all

XL:= Excel_GetWB("C:\one.xlsx", "Sheet2")
XL.ActiveSheet.Range("A15").Value := "Hello"
I just want to know that above method is right method to use your function or not.
This is the nuances of Excel COM structure.

There are three main objects in Excel COM: Application, Workbook, WorkSheet.

Range is a method of the WorkSheet object.
https://docs.microsoft.com/en-us/office/vba/api/excel.worksheet.range

So you have to have a Worksheet object in order to use Range. But there is a wrinkle to this, if you just use Application.Range the application object will default to the ActiveSheet.

Excel_GetWB returns a Workbook object. Range is not a method of a Workbook object and Workbook will not default to the ActiveSheet like an Application object will.

It is important to know what kind of object you have in your variable as you start using more and more of the available COM methods, as all of them generally only act on a certain type of object.

Below is how I would do some things.

Code: Select all

xlWB:= Excel_GetWB("C:\one.xlsx", "Sheet2")
xlWB.ActiveSheet.Range("A15").Value := "Hello"

Code: Select all

xlSheet:= Excel_GetWB("C:\one.xlsx", "Sheet2").ActiveSheet
xlSheet.Range("A15").Value := "Hello"

Code: Select all

; Get all three main Excel objects
xlWB:= Excel_GetWB("C:\one.xlsx", "Sheet2")
xlSheet := xlWB.ActiveSheet
xlApp := xlWB.Parent
; other stuff
This could all be done in the function but then there is the problem of how to pass back three objects. And many people frown on just having them be global variables with names defined by the function.

Code: Select all

xlWB:= Excel_GetWB("C:\one.xlsx", "Sheet2") ; open Workbook and make a sheet active
Data := xlWB.ActiveSheet.Range("A15").Value
xlApp := xlWB.Parent ; Get the application object
xlApp.Quit ; Quit the applicatin object, shutdown Excel
MsgBox % Data
I don't know if it would be worth creating a function like: Excel_GetAWS(xlApp, xlWB, xlSheet, "C:\one.xlsx", "Sheet2") where you supply the three main variable names you want to use and the function puts the three main objects into those variables. Opening and creating them as needed. It is all just about the logistics of creating a parameters scheme that is easy to use.

It might be useful. I am going to play around with this type function.

FG
Thank so much for the explanation. It helped a lot.
Keep up the good work.
Sanjay
Posts: 17
Joined: 30 Oct 2016, 04:03

Re: How to get handle to specific excel file among multiple opened excel files?

15 Feb 2019, 10:20

FanaticGuru wrote:
11 Feb 2019, 14:26
Sanjay wrote:
11 Feb 2019, 03:37
But when I use your function like below then it is working fine.

Code: Select all

XL:= Excel_GetWB("C:\one.xlsx", "Sheet2")
XL.ActiveSheet.Range("A15").Value := "Hello"
I just want to know that above method is right method to use your function or not.
This is the nuances of Excel COM structure.

There are three main objects in Excel COM: Application, Workbook, WorkSheet.

Range is a method of the WorkSheet object.
https://docs.microsoft.com/en-us/office/vba/api/excel.worksheet.range

So you have to have a Worksheet object in order to use Range. But there is a wrinkle to this, if you just use Application.Range the application object will default to the ActiveSheet.

Excel_GetWB returns a Workbook object. Range is not a method of a Workbook object and Workbook will not default to the ActiveSheet like an Application object will.

It is important to know what kind of object you have in your variable as you start using more and more of the available COM methods, as all of them generally only act on a certain type of object.

Below is how I would do some things.

Code: Select all

xlWB:= Excel_GetWB("C:\one.xlsx", "Sheet2")
xlWB.ActiveSheet.Range("A15").Value := "Hello"

Code: Select all

xlSheet:= Excel_GetWB("C:\one.xlsx", "Sheet2").ActiveSheet
xlSheet.Range("A15").Value := "Hello"

Code: Select all

; Get all three main Excel objects
xlWB:= Excel_GetWB("C:\one.xlsx", "Sheet2")
xlSheet := xlWB.ActiveSheet
xlApp := xlWB.Parent
; other stuff
This could all be done in the function but then there is the problem of how to pass back three objects. And many people frown on just having them be global variables with names defined by the function.

Code: Select all

xlWB:= Excel_GetWB("C:\one.xlsx", "Sheet2") ; open Workbook and make a sheet active
Data := xlWB.ActiveSheet.Range("A15").Value
xlApp := xlWB.Parent ; Get the application object
xlApp.Quit ; Quit the applicatin object, shutdown Excel
MsgBox % Data
I don't know if it would be worth creating a function like: Excel_GetAWS(xlApp, xlWB, xlSheet, "C:\one.xlsx", "Sheet2") where you supply the three main variable names you want to use and the function puts the three main objects into those variables. Opening and creating them as needed. It is all just about the logistics of creating a parameters scheme that is easy to use.

It might be useful. I am going to play around with this type function.

FG
The function Excel_GetWB() is not working as expected when excel is in edit mode. Is there any way to make it work as it works when excel is not in edit mode.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to get handle to specific excel file among multiple opened excel files?

15 Feb 2019, 14:44

Sanjay wrote:
15 Feb 2019, 10:20
The function Excel_GetWB() is not working as expected when excel is in edit mode. Is there any way to make it work as it works when excel is not in edit mode.
Yea, that is kind of an unrelated problem. Most things don't work when Excel is in Edit Mode.

The simpliest solution is to just make sure Excel is not in Edit Mode by doing ControlSend, , ^{Enter}, ahk_class XLMAIN.

Not sure if this would be something appropriate for inside the function as this is a general issue. Most COM commands will not work when Excel is in Edit Mode and the body of the script should probably handle exiting edit mode if the script author believes that is something that needs to be addressed. There is a questions of when exiting Edit Mode whether to Accept or Cancel the changes. Sending ^{Enter} Accepts, Sending {Esc} Cancels. Not sure a function should be blindly making that decision.

It is worth noting that exiting Edit Mode has to be done outside of COM as COM is basically locked out when in Edit Mode. So I know of no way to exit Edit Mode through COM.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to get handle to specific excel file among multiple opened excel files?

15 Feb 2019, 15:19

At least, you can detect edit mode:

Code: Select all

try XL.Calculation := XL.Calculation
catch
   MsgBox, Edit Mode
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to get handle to specific excel file among multiple opened excel files?

15 Feb 2019, 15:48

teadrinker wrote:
15 Feb 2019, 15:19
At least, you can detect edit mode:

Code: Select all

try XL.Calculation := XL.Calculation
catch
   MsgBox, Edit Mode
Yea, you can put pretty much any command after the XL. For example, XL.Range("A1") and if it fails you can assume that it is in edit mode which is generally a pretty good assumption. There is nothing particually special about Calculation.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Sanjay
Posts: 17
Joined: 30 Oct 2016, 04:03

Re: How to get handle to specific excel file among multiple opened excel files?

18 Feb 2019, 16:11

Thank You @FanaticGuru and @teadrinker for your help. :)
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: How to get handle to specific excel file among multiple opened excel files?

12 Nov 2019, 12:53

Thanks for this.

By doing this method you've shown @FanaticGuru I'd suppose it would be possible to have more than one Excel Workbook openned and not encounter ID conflicts ?

Code: Select all

xlWB:= Excel_GetWB("S:\Folder\WorkHours.xlsm")
xlSheet := xlWB.ActiveSheet
xlApp := xlWB.Parent
MsgBox % xlWB.Sheets(1).Range("A4").value
MsgBox % xlSheet.Range("A4").value
MsgBox % xlApp.Range("A4").value
ExitApp
I mean, if there would be an existing Excel.exe process running in task manager,
And you would call this for individual workbooks when needed, they would not confuse with each other and this wouldn't create many Excel.exe instances, am I right ?
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to get handle to specific excel file among multiple opened excel files?

14 Nov 2019, 15:25

DRocks wrote:
12 Nov 2019, 12:53
Thanks for this.

By doing this method you've shown @FanaticGuru I'd suppose it would be possible to have more than one Excel Workbook openned and not encounter ID conflicts ?

Code: Select all

xlWB:= Excel_GetWB("S:\Folder\WorkHours.xlsm")
xlSheet := xlWB.ActiveSheet
xlApp := xlWB.Parent
MsgBox % xlWB.Sheets(1).Range("A4").value
MsgBox % xlSheet.Range("A4").value
MsgBox % xlApp.Range("A4").value
ExitApp
I mean, if there would be an existing Excel.exe process running in task manager,
And you would call this for individual workbooks when needed, they would not confuse with each other and this wouldn't create many Excel.exe instances, am I right ?

Yes, normally you want to avoid having multiply Excel applications running at the same time and this function attempts to avoid that by not creating another instance if one is already running.

This function will find the right workbook if the workbook is open in the 'first' Excel application found but it will not necessarily find the right workbook if multiple instances of Excel application are running. That is more complicated.

Here is some code having to do with sorting out multiple instances of Excel applications running.

Code: Select all

; Lexikos example to get all active objects
list := "All Active COM Objects`n"
for name, obj in GetActiveObjects()
    list .= name " -- " ComObjType(obj, "Name") "`n"
MsgBox %list%

; FanaticGuru example of Excel_Objects class method Xls
list := "All Excel Workbooks and Visible State`n"
Xls := Excel_Objects.Xls()
  for index, Xl in Xls
	for Workbook in Xl.Workbooks
		list .= Workbook.name "`t" Workbook.Windows(1).Visible "`n"
MsgBox %list%

; FanaticGuru example of Excel_Objects class method Wbs
list := "All Visible Excel Workbooks and Visible State`n"
Wbs := Excel_Objects.Wbs(,1) ; only visible workbooks
for index, Wb in Wbs
		list .= Wb.Name "`t" Wb.Windows(1).Visible "`n"
MsgBox %list%

Esc::ExitApp

class Excel_Objects
{
	Xls(Needle := "")
	{
		Xls := {}
		for index, obj in GetActiveObjects()
		{
			try 
				Name := obj.Application.ActiveWorkbook.Name
			catch
				continue
			if (RegExMatch(index, "(^|\\)" Name  "$") and RegExMatch(Name, Needle))
				Xls.Push(obj.Application)
		}
		return Xls
	}
	Wbs(Needle := "", Visible := "")
	{
		Wbs := {}
		Xls := Excel_Objects.Xls()
		for index, Xl in Xls
			for Workbook in Xl.Workbooks
				if RegExMatch(Workbook.Name, Needle)
					if (Visible = 1 and Workbook.Windows(1).Visible)
						Wbs.Push(Workbook)
					else if  (Visible = 0 and not Workbook.Windows(1).Visible)
						Wbs.Push(Workbook)
					else if (Visible = "")
						Wbs.Push(Workbook)
		return Wbs
	}
}

; GetActiveObjects v1.0 by Lexikos
; http://ahkscript.org/boards/viewtopic.php?f=6&t=6494
GetActiveObjects(Prefix:="", CaseSensitive:=false) {
    objects := {}
    DllCall("ole32\CoGetMalloc", "uint", 1, "ptr*", malloc) ; malloc: IMalloc
    DllCall("ole32\CreateBindCtx", "uint", 0, "ptr*", bindCtx) ; bindCtx: IBindCtx
    DllCall(NumGet(NumGet(bindCtx+0)+8*A_PtrSize), "ptr", bindCtx, "ptr*", rot) ; rot: IRunningObjectTable
    DllCall(NumGet(NumGet(rot+0)+9*A_PtrSize), "ptr", rot, "ptr*", enum) ; enum: IEnumMoniker
    while DllCall(NumGet(NumGet(enum+0)+3*A_PtrSize), "ptr", enum, "uint", 1, "ptr*", mon, "ptr", 0) = 0 ; mon: IMoniker
    {
        DllCall(NumGet(NumGet(mon+0)+20*A_PtrSize), "ptr", mon, "ptr", bindCtx, "ptr", 0, "ptr*", pname) ; GetDisplayName
        name := StrGet(pname, "UTF-16")
		DllCall(NumGet(NumGet(malloc+0)+5*A_PtrSize), "ptr", malloc, "ptr", pname) ; Free
        if InStr(name, Prefix, CaseSensitive) = 1 {
            DllCall(NumGet(NumGet(rot+0)+6*A_PtrSize), "ptr", rot, "ptr", mon, "ptr*", punk) ; GetObject
            ; Wrap the pointer as IDispatch if available, otherwise as IUnknown.
            if (pdsp := ComObjQuery(punk, "{00020400-0000-0000-C000-000000000046}"))
                obj := ComObject(9, pdsp, 1), ObjRelease(punk)
            else
                obj := ComObject(13, punk, 1)
            ; Store it in the return array by suffix.
            objects[SubStr(name, StrLen(Prefix) + 1)] := obj
        }
        ObjRelease(mon)
    }
    ObjRelease(enum)
    ObjRelease(rot)
    ObjRelease(bindCtx)
    ObjRelease(malloc)
    return objects
}
You can have multiple Excel applications running at the same time. Each Excel application can have multiple workbooks open and each workbook can have multiple sheets.

Just through my work flow I usually avoid having multiple Excel applications running at the same time as there are few good reasons to have two applications running as one Excel application can handle everything that I would normally want to do.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: How to get handle to specific excel file among multiple opened excel files?

14 Nov 2019, 18:06

Thank you again. I had found your example in another forum post while I was documenting on the topic and tried it too.

I agree with you on all you said about the complexity of having multiple Excel apps openned and the intention to keep it down to a single instance.
I discovered my problem of multiple instances will arise when a Excel file is manually openned by me before. This can lead to the creation of a new instance of excel.exe.

After all I think I've come across a wrap around with all the examples you provided to me and what other posts have shown me : https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69841

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: skeerrt and 149 guests