{"id":119,"date":"2009-07-21T12:58:51","date_gmt":"2009-07-21T16:58:51","guid":{"rendered":"http:\/\/www.northatlantawebdesign.com\/?p=119"},"modified":"2009-07-27T10:04:02","modified_gmt":"2009-07-27T14:04:02","slug":"automating-excel-2007-in-c-by-importing-the-excel-2007-type-library","status":"publish","type":"post","link":"http:\/\/www.northatlantawebdesign.com\/index.php\/2009\/07\/21\/automating-excel-2007-in-c-by-importing-the-excel-2007-type-library\/","title":{"rendered":"Automating Excel 2007 in C++ by Importing the Excel 2007 Type Library"},"content":{"rendered":"<p>When I started trying to write automations for <a type=\"amzn\">Excel 2007<\/a> using <a type=\"amzn\">C++<\/a>, I ran into problems right up front.  I was trying to use #import to get to the type library for Excel 2007, and was importing what I thought was the correct file.  The following was written for a C++ application in <a type=\"amzn\">Visual Studio 2008<\/a> (VS2008), automating Excel 2007.<br \/>\n<!--more--><\/p>\n<ol>\n<li>The Excel Type Library is not contained in XL5EN32.OLB as you might expect, it is contained in excel.exe<\/li>\n<li>\nError #2: Even importing the correct file, errors were being raised.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/The Following Import gives you the error Error\t1\terror C2504: '_IMsoDispObj' : base class undefined\r\n#import &quot;C:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE&quot;\r\n<\/pre>\n<p>To correctly import the Excel type library, two more references are needed: MSO.DLL and VBE6EXT.OLB<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#import &quot;C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\mso.dll&quot; no_implementation raw_interfaces_only\r\n#import &quot;C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB&quot; no_implementation raw_interfaces_only\r\n#import &quot;C:\\Program Files\\Microsoft Office\\OFFICE12\\excel.exe&quot; no_implementation raw_interfaces_only\r\n<\/pre>\n<\/li>\n<li>While this solved the problem of the _IMsoDispObj, it raised a few more issues.\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nwarning C4003: not enough actual parameters for macro 'RGB'\r\nwarning C4003: not enough actual parameters for macro 'DialogBoxW'\r\n<\/pre>\n<p>Finally I had the solution, renaming the objects in question that seemed to be redefined elsewhere.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#import &quot;C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL&quot; no_implementation rename(&quot;RGB&quot;, &quot;ExclRGB&quot;) rename(&quot;DocumentProperties&quot;, &quot;ExclDocumentProperties&quot;) rename(&quot;SearchPath&quot;, &quot;ExclSearchPath&quot;)\r\n#import &quot;C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB&quot; no_implementation\r\n#import &quot;D:\\Program Files\\Microsoft Office\\OFFICE12\\EXCEL.EXE&quot; rename(&quot;DialogBox&quot;, &quot;ExclDialogBox&quot;) rename(&quot;RGB&quot;, &quot;ExclRGB&quot;) rename(&quot;CopyFile&quot;, &quot;ExclCopyFile&quot;) rename(&quot;ReplaceText&quot;, &quot;ExclReplaceText&quot;)\r\n<\/pre>\n<\/li>\n<\/ol>\n<p>So in conclusion, in order to import the Excel 2007 Type Library in C++ and use it in your automations, 3 files must be imported, MSO.DLL, VBE6EXT.OLB, and EXCEL.EXE.  On top of those three files, various objects must be renamed due to already being defined.  In my case it was the DialogBox and RGB, though it may differ on each project depending on what is defined.  Here is the final code needed to import the type library.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#import &quot;C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL&quot; no_implementation rename(&quot;RGB&quot;, &quot;ExclRGB&quot;) rename(&quot;DocumentProperties&quot;, &quot;ExclDocumentProperties&quot;) rename(&quot;SearchPath&quot;, &quot;ExclSearchPath&quot;)\r\n#import &quot;C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB&quot; no_implementation\r\n#import &quot;D:\\Program Files\\Microsoft Office\\OFFICE12\\EXCEL.EXE&quot; rename(&quot;DialogBox&quot;, &quot;ExclDialogBox&quot;) rename(&quot;RGB&quot;, &quot;ExclRGB&quot;) rename(&quot;CopyFile&quot;, &quot;ExclCopyFile&quot;) rename(&quot;ReplaceText&quot;, &quot;ExclReplaceText&quot;)\r\n<\/pre>\n<p>Useful Links:<\/p>\n<ul>\n<li><a href=\"http:\/\/groups.google.com\/group\/microsoft.public.vc.language\/msg\/d82ee4d7b85fbed1\">Reading a cell from an Excel Worksheet<\/a><\/li>\n<li><a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/vcgeneral\/thread\/1de6c74f-6cf0-4d91-a5a9-e85853b867f6\">Using Excel Type Library in VS 2005<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>When I started trying to write automations for Excel 2007 using C++, I ran into problems right up front. I was trying to use #import to get to the type library for Excel 2007, and was importing what I thought was the correct file. The following was written for a C++ application in Visual Studio [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,11,7,37],"tags":[40,74,21,39,41,80,38],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-c","category-microsoft-excel-2007","category-microsoft-office-2007","category-visual-studio-2008","tag-automating","tag-c","tag-com","tag-excel-2007","tag-type-library","tag-visual-studio-2008","tag-vs2008"],"_links":{"self":[{"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":9,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"predecessor-version":[{"id":130,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions\/130"}],"wp:attachment":[{"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}