<?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="http://www.northatlantawebdesign.com/index.php/category/microsoft-office-2007/microsoft-word-2007/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.northatlantawebdesign.com</link>
	<description>Programming Examples, Samples, and Tutorials</description>
	<lastBuildDate>Mon, 15 Aug 2011 20:18:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Getting the COM object from HWND using AccessibleObjectFromWindow in Microsoft Word 2007</title>
		<link>http://www.northatlantawebdesign.com/index.php/2009/07/14/getting-the-com-object-from-hwnd-using-accessibleobjectfromwindow-in-microsoft-word-2007/</link>
		<comments>http://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>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 [...]]]></description>
			<content:encoded><![CDATA[<!-- sphereit start --><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>
<!-- sphereit end --><span style="margin-bottom:40px; border-bottom:none;"><a class="iconsphere" title="Sphere: Related Content" onclick="return Sphere.Widget.search('http://www.northatlantawebdesign.com/index.php/2009/07/14/getting-the-com-object-from-hwnd-using-accessibleobjectfromwindow-in-microsoft-word-2007/')" href="http://www.sphere.com/search?q=sphereit:http://www.northatlantawebdesign.com/index.php/2009/07/14/getting-the-com-object-from-hwnd-using-accessibleobjectfromwindow-in-microsoft-word-2007/">Sphere: Related Content</a></span><br/><br/>]]></content:encoded>
			<wfw:commentRss>http://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>
		<item>
		<title>A trip into the depths of Microsoft Active Accessibility (MSAA) through C++</title>
		<link>http://www.northatlantawebdesign.com/index.php/2009/07/10/a-trip-into-the-depths-of-microsoft-accessibility-using-c/</link>
		<comments>http://www.northatlantawebdesign.com/index.php/2009/07/10/a-trip-into-the-depths-of-microsoft-accessibility-using-c/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 15:05:04 +0000</pubDate>
		<dc:creator>Jeff Gibeau</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[Microsoft Access 2003]]></category>
		<category><![CDATA[Microsoft Access 2007]]></category>
		<category><![CDATA[Microsoft Excel 2007]]></category>
		<category><![CDATA[Microsoft Office 2003]]></category>
		<category><![CDATA[Microsoft Office 2007]]></category>
		<category><![CDATA[Microsoft Word 2007]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Cplusplus]]></category>
		<category><![CDATA[IAccessible]]></category>
		<category><![CDATA[Microsoft Access]]></category>
		<category><![CDATA[Microsoft Active Accessibility]]></category>
		<category><![CDATA[Microsoft Excel]]></category>
		<category><![CDATA[Microsoft Word]]></category>
		<category><![CDATA[MSAA]]></category>

		<guid isPermaLink="false">http://www.northatlantawebdesign.com/?p=16</guid>
		<description><![CDATA[Over the next few months, I will be interacting with Microsoft Access, Word, and Excel 2007 through Microsoft Active Accessibility (MSAA) in C++. As my journey progresses, I will be posting the neat little tricks and painful quirks that I run into along the way. To start, here are a few resources you can use [...]]]></description>
			<content:encoded><![CDATA[<!-- sphereit start --><p>Over the next few months, I will be interacting with Microsoft Access, Word, and Excel 2007 through Microsoft Active Accessibility (MSAA) in C++.  As my journey progresses, I will be posting the neat little tricks and painful quirks that I run into along the way.  To start, here are a few resources you can use to read up on Microsoft Active Accessibility.</p>
<p><span id="more-16"></span></p>
<ul>
<li><a title="Microsoft IAccessible Interface C++" href="http://msdn.microsoft.com/en-us/library/dd318466(VS.85).aspx">Microsoft&#8217;s documentation on IAccessible interface</a></li>
<li><a title="Sara Ford's Weblog - Microsoft Active Accessibility" href="http://blogs.msdn.com/saraford/archive/category/2507.aspx?p=2">Sara Ford&#8217;s Weblog on Accessibility</a></li>
<li><a title="User Interface Element References for Controls" href="http://msdn.microsoft.com/en-us/library/dd373675(VS.85).aspx">User Interface Element References for Control in Accessibility</a></li>
</ul>
<p>Over the past 6 months, I used MSAA to develop against <a type="amzn">Microsoft Access 2003</a>.  Although it was a painful process to get working, the end result gives quite a bit of flexibility in automating Access.  That being said, there are still a lot of hurdles to overcome, and Accessibility has definitely left many things to be desired.  In the next few weeks I will be posting about my first impressions of Accessibility and it&#8217;s inconsistencies.</p>
<!-- sphereit end --><span style="margin-bottom:40px; border-bottom:none;"><a class="iconsphere" title="Sphere: Related Content" onclick="return Sphere.Widget.search('http://www.northatlantawebdesign.com/index.php/2009/07/10/a-trip-into-the-depths-of-microsoft-accessibility-using-c/')" href="http://www.sphere.com/search?q=sphereit:http://www.northatlantawebdesign.com/index.php/2009/07/10/a-trip-into-the-depths-of-microsoft-accessibility-using-c/">Sphere: Related Content</a></span><br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.northatlantawebdesign.com/index.php/2009/07/10/a-trip-into-the-depths-of-microsoft-accessibility-using-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

