<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>North Atlanta Web Design &#187; Microsoft Word 2007</title>
	<atom:link href="https://www.northatlantawebdesign.com/index.php/tag/microsoft-word-2007/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.northatlantawebdesign.com</link>
	<description>Programming Examples, Samples, and Tutorials</description>
	<lastBuildDate>Mon, 15 Aug 2011 20:18:35 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.7.41</generator>
	<item>
		<title>Getting the COM object from HWND using AccessibleObjectFromWindow in Microsoft Word 2007</title>
		<link>https://www.northatlantawebdesign.com/index.php/2009/07/14/getting-the-com-object-from-hwnd-using-accessibleobjectfromwindow-in-microsoft-word-2007/</link>
		<comments>https://www.northatlantawebdesign.com/index.php/2009/07/14/getting-the-com-object-from-hwnd-using-accessibleobjectfromwindow-in-microsoft-word-2007/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 01:31:44 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Gibeau]]></dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[Microsoft Office 2007]]></category>
		<category><![CDATA[Microsoft Word 2007]]></category>
		<category><![CDATA[AccessibleObjectFromWindow]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Microsoft Active Accessibility]]></category>

		<guid isPermaLink="false">http://www.northatlantawebdesign.com/?p=30</guid>
		<description><![CDATA[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 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>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 <a type="amzn">COM</a> object in <a type="amzn">Microsoft Word 2007</a>.  It turns out to be pretty simple, and is actually in the documentation <a href="http://msdn.microsoft.com/en-us/library/dd317978(VS.85).aspx">they provide here</a>, but they don&#8217;t make it clear as day.  I intend to do that here.</p>
<p><span id="more-30"></span></p>
<p>First we obtain the hwnd of the Microsoft Word Process:</p>
<pre class="brush: cpp; title: ; notranslate">
//The main window in Microsoft Word has a class name of OpusApp
HWND wordWindow = FindWindow(L&quot;OpusApp&quot;, NULL);
</pre>
<p>We can then traverse the child windows until we find the one with classname _WwG:</p>
<pre class="brush: cpp; title: ; notranslate">
//Use the EnumChildWindows function to iterate through all child windows until we find _WwG
EnumChildWindows(wordWindow, (WNDENUMPROC) EnumChildProc, (LPARAM)1);
</pre>
<pre class="brush: cpp; title: ; notranslate">
static BOOL EnumChildProc(HWND hwnd, LPARAM)
{
     WCHAR szClassName[64];
     if(GetClassNameW(hwnd, szClassName, 64))
     {
          if(_wcsicmp(szClassName, L&quot;_WwG&quot;) == 0)
          {
               //Get AccessibleObject
               Word::Window* pWindow = NULL;
               HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Word::Window), (void**)&amp;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-&gt;get_Document(&amp;pDoc);
               }
               return false;     // Stops enumerating through children
          }
     }
	 return true;
}
</pre>
<p>Next we obtain our Word::Window object through AccessibleObjectFromWindow:</p>
<pre class="brush: cpp; title: ; notranslate">
Word::Window* pWindow = NULL;
HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Word::Window), (void**)&amp;pWindow);
</pre>
<p>That&#8217;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.</p>
<p>Full source code:</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;windows.h&gt;
#include &lt;oleacc.h&gt;
#include &quot;msword.h&quot;

static BOOL EnumChildProc(HWND hwnd, LPARAM)
{
	WCHAR szClassName[64];
	if(GetClassNameW(hwnd, szClassName, 64))
	{
		if(_wcsicmp(szClassName, L&quot;_WwG&quot;) == 0)
		{
			//Get AccessibleObject
			Word::Window* pWindow = NULL;
			HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Word::Window), (void**)&amp;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-&gt;get_Document(&amp;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&quot;OpusApp&quot;, NULL);

	//Use the EnumChildWindows function to iterate through all child windows until we find _WwG
	EnumChildWindows(wordWindow, (WNDENUMPROC) EnumChildProc, (LPARAM)1);

	return 0;
}
</pre>
<p>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&#8217;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.</p>
<p>Useful Links for this post:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/dd317978%28VS.85%29.aspx">AccessibleObjectFromWindow Documentation</a></li>
<li><a title="Using Visual C++ to Automate Office" href="http://support.microsoft.com/kb/238972">Using Visual C++ to Automate Office</a></li>
<li><a title="EnumChildWindows Documentation" href="http://msdn.microsoft.com/en-us/library/ms633494%28VS.85%29.aspx">EnumChildWindows Documentation</a></li>
</ul>
<p>Key phrases: Accessing Word 2007 COM object using AccessibleObjectFromWindow, Automating Word 2007 with Microsoft Active Accessibility MSAA</p>
]]></content:encoded>
			<wfw:commentRss>https://www.northatlantawebdesign.com/index.php/2009/07/14/getting-the-com-object-from-hwnd-using-accessibleobjectfromwindow-in-microsoft-word-2007/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
