{"id":5990,"date":"2015-05-15T09:00:59","date_gmt":"2015-05-15T13:00:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5990"},"modified":"2015-05-13T20:09:00","modified_gmt":"2015-05-14T00:09:00","slug":"control-excel-using-activex","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/05\/15\/control-excel-using-activex\/","title":{"rendered":"Control Excel using ActiveX"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48551-the-x-collection\">The X Collection<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1003439\">Yvan Lengwiler<\/a>.\r\n   <\/p>\r\n   <p>My day job is a customer training engineer. I get to take people who have never used MATLAB or Simulink to the point where\r\n      they feel comfortable in applying the tools in their day-to-day work. It's certainly a rewarding role.\r\n   <\/p>\r\n   <p>Recently during my MATLAB Fundamentals training course, I received a few questions regarding how to exchange data between\r\n      MATLAB and Microsoft\u00ae Excel\u00ae. In the training, we talk about functions like <a href=\"\"><tt>xlsread<\/tt><\/a> and <a href=\"\"><tt>xlswrite<\/tt><\/a>. The person wanted the ability to control the formatting of cells, which <tt>xlswrite<\/tt> does not provide. Specifically, he wanted to change the width of a column and some of the cell colors. I pointed him to the\r\n      function <a href=\"\"><tt>actxserver<\/tt><\/a>, which provides a way to connect to Windows\u00ae applications using <a href=\"http:\/\/en.wikipedia.org\/wiki\/ActiveX\">ActiveX<\/a>. Once connected to Excel, he can use the various <a href=\"http:\/\/en.wikipedia.org\/wiki\/Application_programming_interface\">APIs<\/a> that are provided by Excel to customize. The reference page for <tt>actxserver<\/tt> has an example for connecting to Excel.\r\n   <\/p>\r\n   <p>I started browsing the File Exchange in hopes for finding additional examples that I could point people to for using <tt>actxserver<\/tt> with Excel. Sure enough, I found quite a few <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=activex+excel\">entries<\/a>, including <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28600-xlsappend\">this one<\/a> (and many others) by our very own <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/845693\">Brett Shoelson<\/a>. I chose to highlight Yvan's X Collection, because he provides a suite of functions for performing various simple tasks within\r\n      Excel, using ActiveX. He uses intuitive function names, such as <tt>XConnect<\/tt>, <tt>XOpenBook<\/tt>, and <tt>XAddSheet<\/tt>. Each of these functions take in various handles (Excel application, workbook, or worksheet) as inputs, so it's easy to understand\r\n      how the functions are related to each other. In fact, if you open the files in the MATLAB Editor, you can place the cursor\r\n      at one of the handle variables to see how they are used or created. The editor will highlight the same variables, so you can\r\n      easily pick out where they are used.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_xcollection\/xcollection_variable_highlight.gif\"> <\/p>\r\n   <p>One suggestion I have for improvement is to convert this into a class, instead of a suite of functions. Naturally, these functions\r\n      operate on handles that represent the Excel application, workbooks, and worksheets. Each of them can be a class, and each\r\n      class can contain methods that specifically operate on the instances of the class. This will make the functions inherently\r\n      invisible to everyone except to the class instances. You wouldn't have to catch errors resulting from invalid handle inputs.\r\n      Of course, you may still need to catch other errors that may occur. The other reason for creating a class in this case would\r\n      be to hide (encapsulate) the actual handles. The <tt>hExcel<\/tt> returned from <tt>XConnect<\/tt> is a <tt>COM.Excel_Application<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hExcel = XConnect()<\/pre><pre style=\"font-style:oblique\">hExcel =\r\n\tCOM.Excel_Application\r\n<\/pre><p>This handle exposes the user to various methods and properties provided by the Excel application.<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_xcollection\/xcollection_methods.png\"> <\/p>\r\n   <p>This may be good or bad, but perhaps the intent is for users to use the functions provided by this suite and not the ones\r\n      that are natively available. By creating a class that maintains the handles internally as a property, it protects the users\r\n      from misusing the handle.\r\n   <\/p>\r\n   <p>Nonetheless, this is a great suite of functions for connecting MATLAB with Excel. I'm not doing justice to Yvan's entry if\r\n      I don't mention the true motivation for him creating this. If you need to repeatedly read from or write to an Excel file,\r\n      <tt>xlsread<\/tt> or <tt>xlswrite<\/tt> would suffer a performance hit because of the repeated connect\/disconnect that it's doing with Excel. With Yvan's XCollection,\r\n      you connect to Excel once, do the necessary read\/write operations, and disconnect once.\r\n   <\/p>\r\n   <p><i>Note that Yvan's entry does not provide the functionality the my training customer was asking for, but I pointed him to this\r\n         entry as a starting point.<\/i><\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Yvan provides detailed documentation of how to use the functions. Give this a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5990#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48551-the-x-collection#comments\">comment<\/a> for Yvan.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0cda7b9037a24f8fa6f2046f1f243e67() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='0cda7b9037a24f8fa6f2046f1f243e67 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0cda7b9037a24f8fa6f2046f1f243e67';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = '';\r\n        copyright = 'Copyright 2015 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_0cda7b9037a24f8fa6f2046f1f243e67()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015a<br><\/p>\r\n<\/div>\r\n<!--\r\n0cda7b9037a24f8fa6f2046f1f243e67 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48551-the-x-collection The X\r\n% Collection> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1003439 Yvan\r\n% Lengwiler>.\r\n%\r\n% My day job is a customer training engineer. I get to take people who have\r\n% never used MATLAB or Simulink to the point where they feel comfortable in\r\n% applying the tools in their day-to-day work. It's certainly a rewarding\r\n% role.\r\n%\r\n% Recently during my MATLAB Fundamentals training course, I received a few\r\n% questions regarding how to exchange data between MATLAB and Microsoft\u00ae\r\n% Excel\u00ae. In the training, we talk about functions like\r\n% <\r\n% |xlsread|> and\r\n% <\r\n% |xlswrite|>. The person wanted the ability to control the formatting of\r\n% cells, which |xlswrite| does not provide. Specifically, he wanted to\r\n% change the width of a column and some of the cell colors. I pointed him\r\n% to the function\r\n% <\r\n% |actxserver|>, which provides a way to connect to Windows\u00ae applications\r\n% using <http:\/\/en.wikipedia.org\/wiki\/ActiveX ActiveX>. Once connected to\r\n% Excel, he can use the various\r\n% <http:\/\/en.wikipedia.org\/wiki\/Application_programming_interface APIs>\r\n% that are provided by Excel to customize. The reference page for\r\n% |actxserver| has an example for connecting to Excel.\r\n%\r\n% I started browsing the File Exchange in hopes for finding additional\r\n% examples that I could point people to for using |actxserver| with Excel.\r\n% Sure enough, I found quite a few\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=activex+excel\r\n% entries>, including\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28600-xlsappend this one> (and\r\n% many others) by our very own\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/845693 Brett\r\n% Shoelson>. I chose to highlight Yvan's X Collection, because he provides\r\n% a suite of functions for performing various simple tasks within Excel,\r\n% using ActiveX. He uses intuitive function names, such as |XConnect|,\r\n% |XOpenBook|, and |XAddSheet|. Each of these functions take in various\r\n% handles (Excel application, workbook, or worksheet) as inputs, so it's\r\n% easy to understand how the functions are related to each other. In fact,\r\n% if you open the files in the MATLAB Editor, you can place the cursor at\r\n% one of the handle variables to see how they are used or created. The\r\n% editor will highlight the same variables, so you can easily pick out\r\n% where they are used.\r\n%\r\n% <<xcollection_variable_highlight.gif>>\r\n%\r\n% One suggestion I have for improvement is to convert this into a class,\r\n% instead of a suite of functions. Naturally, these functions operate on\r\n% handles that represent the Excel application, workbooks, and worksheets.\r\n% Each of them can be a class, and each class can contain methods that\r\n% specifically operate on the instances of the class. This will make the\r\n% functions inherently invisible to everyone except to the class instances.\r\n% You wouldn't have to catch errors resulting from invalid handle inputs.\r\n% Of course, you may still need to catch other errors that may occur. The\r\n% other reason for creating a class in this case would be to hide\r\n% (encapsulate) the actual handles. The |hExcel| returned from |XConnect|\r\n% is a |COM.Excel_Application|.\r\n\r\nhExcel = XConnect()\r\n\r\n%%\r\n% This handle exposes the user to various methods and properties provided\r\n% by the Excel application.\r\n%\r\n% <<xcollection_methods.png>>\r\n%\r\n% This may be good or bad, but perhaps the intent is for users to use the\r\n% functions provided by this suite and not the ones that are natively\r\n% available. By creating a class that maintains the handles internally as a\r\n% property, it protects the users from misusing the handle.\r\n%\r\n% Nonetheless, this is a great suite of functions for connecting MATLAB\r\n% with Excel. I'm not doing justice to Yvan's entry if I don't mention\r\n% the true motivation for him creating this. If you need to repeatedly\r\n% read from or write to an Excel file, |xlsread| or |xlswrite| would suffer\r\n% a performance hit because of the repeated connect\/disconnect that it's\r\n% doing with Excel. With Yvan's XCollection, you connect to Excel once, do\r\n% the necessary read\/write operations, and disconnect once.\r\n% \r\n% _Note that Yvan's entry does not provide the functionality the my\r\n% training customer was asking for, but I pointed him to this entry as a\r\n% starting point._\r\n%\r\n% *Comments*\r\n%\r\n% Yvan provides detailed documentation of how to use the functions. Give\r\n% this a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5990#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48551-the-x-collection#comments\r\n% comment> for Yvan.\r\n\r\n##### SOURCE END ##### 0cda7b9037a24f8fa6f2046f1f243e67\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_xcollection\/xcollection_variable_highlight.gif\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   Jiro's pick this week is The X Collection by Yvan Lengwiler.\r\n   \r\n   My day job is a customer training engineer. I get to take people who have never used MATLAB or Simulink to the point where\r\n... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/05\/15\/control-excel-using-activex\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,16,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5990"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5990"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5990\/revisions"}],"predecessor-version":[{"id":5999,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5990\/revisions\/5999"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}