<?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 Excel 2007</title>
	<atom:link href="http://www.northatlantawebdesign.com/index.php/tag/microsoft-excel-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>Access Microsoft Excel 2007 COM API through Microsoft Active Accessibility</title>
		<link>http://www.northatlantawebdesign.com/index.php/2009/07/15/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility/</link>
		<comments>http://www.northatlantawebdesign.com/index.php/2009/07/15/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 00:47:49 +0000</pubDate>
		<dc:creator>Jeff Gibeau</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[Microsoft Excel 2007]]></category>
		<category><![CDATA[Microsoft Office 2007]]></category>
		<category><![CDATA[AccessibleObjectFromWindow]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Microsoft Active Accessibility]]></category>

		<guid isPermaLink="false">http://www.northatlantawebdesign.com/?p=63</guid>
		<description><![CDATA[A couple days ago I showed you how to access the Microsoft Word 2007 COM API through Microsoft Active Accessibility (MSAA). Today, I&#8217;m going to switch it up just a little bit, and use the same method to access the Microsoft Excel 2007 COM API. Again, we&#8217;ll be using AccesssibleObjectFromWindow to get at the main [...]]]></description>
			<content:encoded><![CDATA[<!-- sphereit start --><p>A couple days ago I showed you how to access the <a type="amzn">Microsoft Word 2007</a> COM API through Microsoft Active Accessibility (MSAA).  Today, I&#8217;m going to switch it up just a little bit, and use the same method to access the <a type="amzn">Microsoft Excel 2007</a> COM API.  Again, we&#8217;ll be using AccesssibleObjectFromWindow to get at the main Excel::Window object.  From that object, you can obtain the Excel Application, and Excel Workbook objects.  Some documentation from Microsoft can be <a href="http://msdn.microsoft.com/en-us/library/dd317978(VS.85).aspx">found here</a>.</p>
<p><span id="more-63"></span></p>
<p>First we obtain the hwnd of the Microsoft Excel Process:</p>
<pre class="brush: cpp; title: ; notranslate">
//The main window in Microsoft Excel has a class name of XLMAIN
HWND excelWindow = FindWindow(L&quot;XLMAIN&quot;, NULL);
</pre>
<p>We can then traverse the child windows until we find the one with classname EXCEL7:</p>
<pre class="brush: cpp; title: ; notranslate">
//Use the EnumChildWindows function to iterate through all child windows until we find EXCEL7
EnumChildWindows(excelWindow, (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;EXCEL7&quot;) == 0)
          {
               //Get AccessibleObject
               Excel::Window* pWindow = NULL;
               HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&amp;pWindow);
               if(hr == S_OK)
               {
                    //Excel object is now in pWindow pointer, from this you can obtain the document or application
                    Excel::_Application* pApp = NULL;
                    pWindow-&gt;get_Application(&amp;pApp);
                    pWindow-&gt;Release();
               }
               return false;     // Stops enumerating through children
          }
     }
     return true;
}
</pre>
<p>Next we obtain our Excel::Window object through AccessibleObjectFromWindow:</p>
<pre class="brush: cpp; title: ; notranslate">
//Get AccessibleObject
Excel::Window* pWindow = NULL;
HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&amp;pWindow);
</pre>
<p>That&#8217;s it!  We now have our Excel::Window COM Object.  From this object we can obtain the Excel::Application and Excel::Workbook objects, allowing us full interaction with the active document.  So with just a few lines of code, we now have access to automate an Excel 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;msexcel.h&quot;

static BOOL EnumChildProc(HWND hwnd, LPARAM)
{
     WCHAR szClassName[64];
     if(GetClassNameW(hwnd, szClassName, 64))
     {
          if(_wcsicmp(szClassName, L&quot;EXCEL7&quot;) == 0)
          {
               //Get AccessibleObject
               Excel::Window* pWindow = NULL;
               HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&amp;pWindow);
               if(hr == S_OK)
               {
                    //Excel object is now in pWindow pointer, from this you can obtain the document or application
                    Excel::_Application* pApp = NULL;
                    pWindow-&gt;get_Application(&amp;pApp);
                    pWindow-&gt;Release();
               }
               return false;     // Stops enumerating through children
          }
     }
	 return true;
}
int main( int argc, CHAR* argv[])
{
     //The main window in Microsoft Excel has a class name of XLMAIN
     HWND excelWindow = FindWindow(L&quot;XLMAIN&quot;, NULL);

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

     return 0;
}
</pre>
<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 Excel 2007 COM object using AccessibleObjectFromWindow, Automating Excel 2007 with Microsoft Active Accessibility MSAA<br />
<em><strong>Update:</strong> I&#8217;ve updated the full source to utilize get_Application.  get_Document was in place previously, which was leftover from my Word source code.</em></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/15/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility/')" href="http://www.sphere.com/search?q=sphereit:http://www.northatlantawebdesign.com/index.php/2009/07/15/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility/">Sphere: Related Content</a></span><br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.northatlantawebdesign.com/index.php/2009/07/15/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

