Page 1 of 1

ComObjActive with several open Word files

Posted: 06 Apr 2024, 03:45
by songdg
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.

Re: ComObjActive with several open Word files

Posted: 06 Apr 2024, 06:44
by mikeyww

Re: ComObjActive with several open Word files  Topic is solved

Posted: 06 Apr 2024, 12:45
by flyingDman
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 )

Re: ComObjActive with several open Word files

Posted: 07 Apr 2024, 04:02
by songdg
Thanks, I did searched the forum before I asking this question, but I got the keyword wrong.

Re: ComObjActive with several open Word files

Posted: 07 Apr 2024, 04:07
by songdg
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.