c++ms-wordoffice-automation

How to access the name of ActiveX controls in Word by OLE Automation in C++


How to access the name of ActiveX controls in Word by OLE Automation in C++

Visiting all ActiveX controls by OLE Automation and collecting their names is an easy task in C#. It just requires to add the COM references for Word and for Microsoft Forms and you can iterating the Shapes or InlineShapes. OLEFormat.Object gives you access to the ActiveX control and the member name contains its name in Word.

using Word = Microsoft.Office.Interop.Word;
using Forms = Microsoft.Vbe.Interop.Forms;

static void Main(string[] args)
{ 
    Word.Document doc = wordApp.Documents.Open("sample.docm");

    foreach (Word.Shape shape in doc.Shapes)
    {
        String name = shape.OLEFormat.Object.name;
        Console.WriteLine(name);
    }
}

The question is how to do this in c++. It is possible to use the typelib for Word and get the Shapes, access their OLEFormat and finally the Object. This Object seems not be represented in the Word or the Forms typelib at all. It seems to be an abstract class for all kind of ActiveX controls. The question is, how to access the 'name' from it. Also the ActiveX controls like ComboBox do not contain a property name.

So the question is how to solve this by C++.


Solution

  • The Object is not represented in the typelib for Word or for Forms, but IDispatch::Invoke can be used to query for name.