Getting the COM object from HWND using AccessibleObjectFromWindow in Microsoft Word 2007

July 14, 2009 by Jeff Gibeau Leave a reply »

After spending some time on this over the past few days, and sorting through the spotty documentation provided by Microsoft on Accessibility, I was able to get a handle on the COM object in Microsoft Word 2007.  It turns out to be pretty simple, and is actually in the documentation they provide here, but they don’t make it clear as day.  I intend to do that here.

First we obtain the hwnd of the Microsoft Word Process:

//The main window in Microsoft Word has a class name of OpusApp
HWND wordWindow = FindWindow(L"OpusApp", NULL);

We can then traverse the child windows until we find the one with classname _WwG:

//Use the EnumChildWindows function to iterate through all child windows until we find _WwG
EnumChildWindows(wordWindow, (WNDENUMPROC) EnumChildProc, (LPARAM)1);
static BOOL EnumChildProc(HWND hwnd, LPARAM)
{
     WCHAR szClassName[64];
     if(GetClassNameW(hwnd, szClassName, 64))
     {
          if(_wcsicmp(szClassName, L"_WwG") == 0)
          {
               //Get AccessibleObject
               Word::Window* pWindow = NULL;
               HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Word::Window), (void**)&pWindow);
               if(hr == S_OK)
               {
                    //Word object is now in pWindow pointer, from this you can obtain the document or application
                    Word::_Document* pDoc = NULL;
                    pWindow->get_Document(&pDoc);
               }
               return false;     // Stops enumerating through children
          }
     }
	 return true;
}

Next we obtain our Word::Window object through AccessibleObjectFromWindow:

Word::Window* pWindow = NULL;
HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Word::Window), (void**)&pWindow);

That’s it!  We now have our Word::Window COM Object.  From this object we can obtain the Word::Application and Word::Document objects, allowing us full interaction with the active document.  So with just a few lines of code, we now have access to automate a word 2007 document.

Full source code:

#include <windows.h>
#include <oleacc.h>
#include "msword.h"

static BOOL EnumChildProc(HWND hwnd, LPARAM)
{
	WCHAR szClassName[64];
	if(GetClassNameW(hwnd, szClassName, 64))
	{
		if(_wcsicmp(szClassName, L"_WwG") == 0)
		{
			//Get AccessibleObject
			Word::Window* pWindow = NULL;
			HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Word::Window), (void**)&pWindow);
			if(hr == S_OK)
			{
				//Word object is now in pWindow pointer, from this you can obtain the document or application
				Word::_Document* pDoc = NULL;
				pWindow->get_Document(&pDoc);
			}
			return false;     // Stops enumerating through children
		}
	}
	return true;
}
int main( int argc, CHAR* argv[])
{
	//The main window in Microsoft Word has a class name of OpusApp
	HWND wordWindow = FindWindow(L"OpusApp", NULL);

	//Use the EnumChildWindows function to iterate through all child windows until we find _WwG
	EnumChildWindows(wordWindow, (WNDENUMPROC) EnumChildProc, (LPARAM)1);

	return 0;
}

The logic to find the correct window can be changed for multiple instances of Word.  Also, you must have the Word type library imported to use the Word COM API.  We’ll leave those two for another topic on another date. Check back later in the week for a follow up post on how to access Microsoft Excel 2007 in the same manner.

Useful Links for this post:

Key phrases: Accessing Word 2007 COM object using AccessibleObjectFromWindow, Automating Word 2007 with Microsoft Active Accessibility MSAA

Advertisement

One Response

  1. Paul Kalnitz says:

    I’m not very experienced with Windows programming, but I want to automate MS Word and retrieve the cursor location in order to retrieve text just before the cursor, and then to insert new text after the cursor. Also I’d like to retrieve attributes such as font, size, etc. I think all this can be accomplished through the IDispatch interface and your example here is the closest I’ve come to a solutions. Would it be possible to point me in the direction of the 2 missing pieces you refer to – (mainly) how to import the type library, and (secondarily) how to handle multiple instances of Word. Thanks very much.

Leave a Reply