ComObjActive with several open Word files Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
songdg
Posts: 609
Joined: 04 Oct 2017, 20:04

ComObjActive with several open Word files

06 Apr 2024, 03:45

I know if there're several open Word files, ComObjActive will connects the first open Word file, I wonder if it is possible in that case open a dialogue let the user to choose which file ComObjActive to connect.
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: ComObjActive with several open Word files  Topic is solved

06 Apr 2024, 12:45

ComObjActive does not connect to a file, it connects to the application (in this case MS Word). Having several files open in Word does not mean that there are several instances of the application running simultaneously. Several files can be open and running under the same instance. One of the open files is the activedocument. You can find the name of the activedocument using oWord.activedocument.Name. You can select the active document using doc.activate.
The following script enumerates the open files using a loop and list them in a menu. Clicking on the menu item will make that item the activedocument.

Code: Select all

oword := ComObjActive("Word.Application")
MyMenu := Menu()
loop oWord.documents.count
	MyMenu.Add(oWord.documents.item(a_index).name, MyMenuMenuHandler)
MyMenu.Check(oWord.activedocument.Name)							; puts a ✓ next to the active document
MyMenu.show

MyMenuMenuHandler(ItemName, ItemPos, MyMenu) 
	{ 
	doc := oWord.application.documents.item(ItemPos)
	doc.activate	
	msgbox oWord.activedocument.Name, "Active Doc is:"
	}
Note: the way doc.activate works under v2 is different than under v1. In v1 you can use oWord.documents(x).activate. For some reason that does not work in v2. Using oWord.application.documents.item(ItemPos).activate in above script (replacing line 9 and 10) triggers error 0x800A16E6 (which I encountered several times already and I still don't have a full understanding of the issue).

Excel does not have this quirky behavior:

For a v2 version of GetActiveObjects see here: viewtopic.php?t=80074#p529688 (thank you @Spitzi )
14.3 & 1.3.7
songdg
Posts: 609
Joined: 04 Oct 2017, 20:04

Re: ComObjActive with several open Word files

07 Apr 2024, 04:02

Thanks, I did searched the forum before I asking this question, but I got the keyword wrong.
songdg
Posts: 609
Joined: 04 Oct 2017, 20:04

Re: ComObjActive with several open Word files

07 Apr 2024, 04:07

flyingDman wrote:
06 Apr 2024, 12:45
ComObjActive does not connect to a file, it connects to the application (in this case MS Word). Having several files open in Word does not mean that there are several instances of the application running simultaneously. Several files can be open and running under the same instance. One of the open files is the activedocument. You can find the name of the activedocument using oWord.activedocument.Name. You can select the active document using doc.activate.
The following script enumerates the open files using a loop and list them in a menu. Clicking on the menu item will make that item the activedocument.

Code: Select all

oword := ComObjActive("Word.Application")
MyMenu := Menu()
loop oWord.documents.count
	MyMenu.Add(oWord.documents.item(a_index).name, MyMenuMenuHandler)
MyMenu.Check(oWord.activedocument.Name)							; puts a ✓ next to the active document
MyMenu.show

MyMenuMenuHandler(ItemName, ItemPos, MyMenu) 
	{ 
	doc := oWord.application.documents.item(ItemPos)
	doc.activate	
	msgbox oWord.activedocument.Name, "Active Doc is:"
	}
Note: the way doc.activate works under v2 is different than under v1. In v1 you can use oWord.documents(x).activate. For some reason that does not work in v2. Using oWord.application.documents.item(ItemPos).activate in above script (replacing line 9 and 10) triggers error 0x800A16E6 (which I encountered several times already and I still don't have a full understanding of the issue).

Excel does not have this quirky behavior:

For a v2 version of GetActiveObjects see here: viewtopic.php?t=80074#p529688 (thank you @Spitzi )
Wow, that's amazing :bravo: , your help is much appreciated.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada and 26 guests