Connecting to the correct COM Object in MS word is a little problematic. We wrote this script to make it easier
How to connect to the correct COM object in MS Office Programs
Updated on 2024/04/14
Learn how to use COM objects to control Office programs and other applications with Autohotkey.
- 00:00 Get the right COM object for Office programs and adapt it to other programs.
- We will show you how to get the correct COM object for Office programs and how to adapt it to other programs.
- 01:23 Connecting to the wrong instance of Word can cause a COM object active issue.
- When two instances of Word are open, connecting to the wrong one can occur randomly, causing a problem with the COM object active.
- 02:59 Get a handle to a control to get a COM object from the Acc Library.
- We are getting a handle to a specific control to get a COM object from the Acc library.
- Using Windows Spy information, you can get the window class and control to target a Microsoft Windows document, and then use the handle to get a COM object from the Acc Library.
- 04:52 Found a list of hexadecimal values for the second parameter I was passing.
- I looked up the object identifier for the second parameter I was passing and found a list of values in hexadecimal numbers.
- 06:38 Added a variable to access Word’s own com object, ensuring code won’t fail.
- I found the -16 object id native om needed to access Word’s own com object and added it as a variable.
- Running the code will ensure that the com object has a document element, preventing code failure.
- 08:41 Use the active window’s ID to target specific documents instead of hard-coding.
- You can target a specific document by specifying a different window title for each one.
- We can get the active window’s ID and use it instead of the hard-coded ID to get the correct document.
- 10:40 Pass a class and title to a function to control a window.
- Pass a class and window title to a function to return an object and control the window you want to target.
- 12:25 Level up your Autohotkey coding skills with consultations or tutoring, and download the script from the first link!
- We have a course to teach working with objects and classes, and offer consultation for those who need help with bugs.
- Get expert help to level up your Autohotkey coding skills with consultations or tutoring, and download the script from the first link.


