{"id":63,"date":"2009-07-15T20:47:49","date_gmt":"2009-07-16T00:47:49","guid":{"rendered":"http:\/\/www.northatlantawebdesign.com\/?p=63"},"modified":"2010-07-09T09:08:06","modified_gmt":"2010-07-09T13:08:06","slug":"access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility","status":"publish","type":"post","link":"https:\/\/www.northatlantawebdesign.com\/index.php\/2009\/07\/15\/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility\/","title":{"rendered":"Access Microsoft Excel 2007 COM API through Microsoft Active Accessibility"},"content":{"rendered":"<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>\n<p><!--more--><\/p>\n<p>First we obtain the hwnd of the Microsoft Excel Process:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/The main window in Microsoft Excel has a class name of XLMAIN\r\nHWND excelWindow = FindWindow(L&quot;XLMAIN&quot;, NULL);\r\n<\/pre>\n<p>We can then traverse the child windows until we find the one with classname EXCEL7:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/Use the EnumChildWindows function to iterate through all child windows until we find EXCEL7\r\nEnumChildWindows(excelWindow, (WNDENUMPROC) EnumChildProc, (LPARAM)1);\r\n<\/pre>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstatic BOOL EnumChildProc(HWND hwnd, LPARAM)\r\n{\r\n     WCHAR szClassName&#x5B;64];\r\n     if(GetClassNameW(hwnd, szClassName, 64))\r\n     {\r\n          if(_wcsicmp(szClassName, L&quot;EXCEL7&quot;) == 0)\r\n          {\r\n               \/\/Get AccessibleObject\r\n               Excel::Window* pWindow = NULL;\r\n               HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&amp;pWindow);\r\n               if(hr == S_OK)\r\n               {\r\n                    \/\/Excel object is now in pWindow pointer, from this you can obtain the document or application\r\n                    Excel::_Application* pApp = NULL;\r\n                    pWindow-&gt;get_Application(&amp;pApp);\r\n                    pWindow-&gt;Release();\r\n               }\r\n               return false;     \/\/ Stops enumerating through children\r\n          }\r\n     }\r\n     return true;\r\n}\r\n<\/pre>\n<p>Next we obtain our Excel::Window object through AccessibleObjectFromWindow:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/Get AccessibleObject\r\nExcel::Window* pWindow = NULL;\r\nHRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&amp;pWindow);\r\n<\/pre>\n<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>\n<p>Full source code:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;windows.h&gt;\r\n#include &lt;oleacc.h&gt;\r\n#include &quot;msexcel.h&quot;\r\n\r\nstatic BOOL EnumChildProc(HWND hwnd, LPARAM)\r\n{\r\n     WCHAR szClassName&#x5B;64];\r\n     if(GetClassNameW(hwnd, szClassName, 64))\r\n     {\r\n          if(_wcsicmp(szClassName, L&quot;EXCEL7&quot;) == 0)\r\n          {\r\n               \/\/Get AccessibleObject\r\n               Excel::Window* pWindow = NULL;\r\n               HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&amp;pWindow);\r\n               if(hr == S_OK)\r\n               {\r\n                    \/\/Excel object is now in pWindow pointer, from this you can obtain the document or application\r\n                    Excel::_Application* pApp = NULL;\r\n                    pWindow-&gt;get_Application(&amp;pApp);\r\n                    pWindow-&gt;Release();\r\n               }\r\n               return false;     \/\/ Stops enumerating through children\r\n          }\r\n     }\r\n\t return true;\r\n}\r\nint main( int argc, CHAR* argv&#x5B;])\r\n{\r\n     \/\/The main window in Microsoft Excel has a class name of XLMAIN\r\n     HWND excelWindow = FindWindow(L&quot;XLMAIN&quot;, NULL);\r\n\r\n     \/\/Use the EnumChildWindows function to iterate through all child windows until we find _WwG\r\n     EnumChildWindows(excelWindow, (WNDENUMPROC) EnumChildProc, (LPARAM)1);\r\n\r\n     return 0;\r\n}\r\n<\/pre>\n<p>Useful Links for this post:<\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd317978%28VS.85%29.aspx\">AccessibleObjectFromWindow Documentation<\/a><\/li>\n<li><a title=\"Using Visual C++ to Automate Office\" href=\"http:\/\/support.microsoft.com\/kb\/238972\">Using Visual C++ to Automate Office<\/a><\/li>\n<li><a title=\"EnumChildWindows Documentation\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms633494%28VS.85%29.aspx\">EnumChildWindows Documentation<\/a><\/li>\n<\/ul>\n<p>Key phrases: Accessing Excel 2007 COM object using AccessibleObjectFromWindow, Automating Excel 2007 with Microsoft Active Accessibility MSAA<br \/>\n<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>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,11,7],"tags":[70,22,74,21,15,73,71],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-accessibility","category-microsoft-excel-2007","category-microsoft-office-2007","tag-accessibility","tag-accessibleobjectfromwindow","tag-c","tag-com","tag-microsoft-active-accessibility","tag-microsoft-excel-2007","tag-microsoft-office-2007"],"_links":{"self":[{"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":9,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":197,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions\/197"}],"wp:attachment":[{"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}