- Undocumented Matlab - https://undocumentedmatlab.com -

COM/ActiveX tips & tricks

Posted By Yair Altman On July 28, 2010 | 6 Comments

Matlab’s COM [1]/ActiveX [2] interface has been supported and well-documented [3] for many releases. However, there are still some aspects that are either not detailed, or that escape the casual documentation reader.

Accessing collection items

COM collections is a COM interface for sets of similar objects such as the worksheets in an Excel file or the images in a PowerPoint document. The items in a collection can be accessed using a numeric index (starting at 1) or the item’s string name.
The “normal” way to access collection items is using the Item() method that accepts a numeric index or the item’s name (a string).
Since collections are so common, Microsoft devised a short-cut of passing the parameter directly to the collection. For example, in our Excel VB code, instead of using Worksheets.Item(2) or Worksheets.Item(‘Sheet2’), we could write Worksheets(2) or Worksheets(‘Sheet2’). This shortcut is so common that the “normal” way of using Item() is rarely seen.
Unfortunately, Matlab’s implementation of the COM interface does not recognize this shortcut. Instead, we must use the more verbose way of using the Item():

% Invalid - shortcut is not recognized by Matlab
>> hSheet = hWorkbook.Worksheets(2);
??? Index exceeds matrix dimensions
% Valid
>> hSheet = hWorkbook.Worksheets.Item(2);
>> hSheet = hWorkbook.Worksheets.Item('Sheet2')
hSheet =
	Interface.Interface.Microsoft_Excel_11.0_Object_Library._Worksheet

Note that the dot-notation used above only works on recent Matlab 7 releases. Earlier releases (for example, Matlab 6.0 R12) have bugs that prevent it from functioning properly. The workaround is to use the following even-more-verbose way, which work on all Matlab releases:

hSheet = invoke(get(hWorkbook,'Worksheets'),'Item',2);

Matlab’s documentation actually has a short section [4] describing the valid way to access COM collection. IMHO, a special warning about the invalid but widely-used short-cut would have been useful. In any case, the issue of accessing collection items has often appeared on CSSM (for example, here [5] and here [6]), so I guess many programmers have overlooked this.

Using enumerated values

When setting COM property values, Matlab supports some enumerated (constant) values but not all (read here [7]). In practice, this can be very frustrating since the VB code and documentation almost always refers to the enumerated values only. Without the ability to set enumeration values, some properties become unusable in Matlab.
Well, not really. There’s a workaround: Since every enumerated COM value hides a numeric value, we can pass the numeric values rather than the enumerated (string) value when setting such properties. The numeric values are seldom documented, but can often be easily found online (use Google!). Quite often, they appear in C header-files that #define the enumerated values as pre-processor items with the required numeric value. For example, the numeric value for xlXYScatterLines is easily found [8] to be 74.
Some of the enumerated constants are harder to find in this manner. You can use one of the following resources to search for your requested constant: Excel [9], PowerPoint [10] (or here [11]), OLE/Office [12] (includes Word, Access and Internet Explorer), and an even larger list [13].
Again, old Matlab versions have problems understanding string enumerations, but the numeric values are always accepted and so are backward-compatible. If your application needs to support old Matlab versions, always use the numeric values (add a comment with the enumerated name, for maintainability).

Office 2010

Microsoft Office 2010 has apparently changed its COM interface, so accessing it from Matlab cannot easily be done. Luckily, Samuel Foucher has posted a workaround to this problem on CSSM yesterday [14]. The trick is basically to have both an older Office and Office 2010 installed at the same time. As Samuel notes, “This is far from an optimal solution but it seems to work so far“.

Categories: Low risk of breaking in future versions, Stock Matlab function


6 Comments (Open | Close)

6 Comments To "COM/ActiveX tips & tricks"

#1 Comment By Jason On July 28, 2010 @ 11:40

Hi Yair,

Two comments here. First, there actually is a way to use the VB syntax via the verbose get and set. To use the context of your example:

hSheet = get(hWorkbook,'Worksheets',2);

Secondly, I have found the Microsoft developer resources pretty useful for finding enumerated values. Most of the time, they will list the numeric value next to the enumerated name. It’s pretty well laid out, except for using links instead of a tree view. Here is the link to the one for Excel:

[21]

#2 Comment By Jaideep On January 17, 2017 @ 15:37

How do i stack an activeX control(Windows Media player) behind an axes?
I want my axes to appear on top of my activex control i.e. windows media player in my case? My guide however is not allowing me to do this inspite of selecting ‘Bring to front’ or ‘Send to Back’ option. The axes is always stuck behind my active x control.
Thank you in advance.

#3 Comment By Manju On January 22, 2018 @ 06:21

Hi,
How do we click on a excel user form using matlab actxserver? Using workbooks.Worksheet.Item(userformName) is not working…

#4 Comment By Yair Altman On January 22, 2018 @ 11:01

@Manju – you can start the Excel Macro Recorder, then do whatever you want in Excel, then stop the recording, view the resulting VB code (using Alt-F8 or the Macros menu), and then convert that VB code into corresponding Matlab code (a process called “Matlabization”).

#5 Comment By Gary Roth On February 6, 2018 @ 21:23

I tried to follow the “well documented” link ( [22]) and received the message “The page you were looking for does not exist. Use the search box or browse topics below to find the page you were looking for.”

#6 Comment By Yair Altman On February 7, 2018 @ 22:05

@Gary – MathWorks constantly change their website without bothering to redirect the old webpages to their new URLs. This is yet another example of this. I fixed the link that you mentioned to the new URL (hopefully it will not be changed again in the near future, but who know). Unfortunately, all the other links in the article that lead to MathWorks webpage are also broken, and I have not fixed them. If anyone has time to search for the new URLs and email me, then I will update them.


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/com-activex-tips

URLs in this post:

[1] COM: http://en.wikipedia.org/wiki/Component_Object_Model

[2] ActiveX: http://en.wikipedia.org/wiki/ActiveX

[3] well-documented: https://www.mathworks.com/help/matlab/ref/com.html

[4] short section: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f27433.html#f27442

[5] here: https://www.mathworks.com/matlabcentral/newsreader/view_thread/286930

[6] here: https://www.mathworks.com/matlabcentral/newsreader/view_thread/286988

[7] read here: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f89715.html#f107980

[8] easily found: http://www.google.com/search?q=%22define+xlXYScatterLines%22

[9] Excel: http://techsupt.winbatch.com/ts/T000001033005F9.html

[10] PowerPoint: http://www.wizardwrx.com/CommonTypelibs/O2000_MSPPT9.CSV

[11] here: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP/Constants+PPCONST.txt

[12] OLE/Office: http://techsupt.winbatch.com/ts/T000001033F49.html

[13] even larger list: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP//Constants

[14] on CSSM yesterday: https://www.mathworks.com/matlabcentral/newsreader/view_thread/287717#766303

[15] Some Matlab performance-tuning tips : https://undocumentedmatlab.com/articles/some-performance-tuning-tips

[16] Controlling plot data-tips : https://undocumentedmatlab.com/articles/controlling-plot-data-tips

[17] Tips for accelerating Matlab performance : https://undocumentedmatlab.com/articles/tips-for-accelerating-matlab-performance

[18] Draggable plot data-tips : https://undocumentedmatlab.com/articles/draggable-plot-data-tips

[19] A few parfor tips : https://undocumentedmatlab.com/articles/a-few-parfor-tips

[20] Running VB code in Matlab : https://undocumentedmatlab.com/articles/running-vb-code-in-matlab

[21] : http://msdn.microsoft.com/en-us/library/bb149081.aspx

[22] : https://www.mathworks.com/help/matlab/matlab_external/brd4at8.html

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.