Qt ActiveX QAxObject format Excel cell comment

1.9k views Asked by At

I would like to format an Microsoft Excel 2010 cell comment (e.g. change font, boldness, ..) using Qt 5.

I can add an comment to a cell using the following code:

QAxObject* cellRange = m_activeWorksheet->querySubObject("Cells(int, int)", row, col);
cellRange->dynamicCall("AddComment(const QVariant&)", comment);

I am also able to set the AutoSize property for the cell comment:

QAxObject* axComment = cellRange->querySubObject("Comment");
QAxObject* shape = axComment->querySubObject("Shape");
shape->querySubObject("TextFrame")->setProperty("AutoSize", autosize);

But I am not able to change "deeper" comment properties, e.g. TextFrame.Characters.Font.Bold.

After setting the cell comment, the command

shape->querySubObject("TextFrame") 

returns a non-zero pointer, but

shape->querySubObject("TextFrame")->querySubObject("Characters")

returns NULL.

How do I format the cell comments using QAxObject ? Is there a description of the properties/subObjects for the different QAxObjects accessible by QAxObject?

The following code does not have any effect:

shape->setProperty("AutoShapeType", 5);
2

There are 2 answers

3
Lol4t0 On BEST ANSWER
  1. Probably the problem is that TextFrame does not have property Characters. Instead it has method Characters, but it full signature is

    Characters(Start, Length)
    

    Qt docs says that you should specify full signature, so you should probably query value with

    shape->querySubObject("TextFrame")->querySubObject("Characters(Start,Length)")
    
  2. You cannot set AutoShapeType to 5. AutoShapeType is of type MsoAutoShapeType and aonly specified values are allowed.

0
Michael Hilbert On

After browsing through the Qt docs, the QAxBase dynamicCAll section showed the the way how to set the shape of an Excel cell comment using a dynamic call:

QString comment("My comment");
QAxObject* cellRange = m_activeWorksheet->querySubObject("Cells(int, int)", cellRow, cellColumn);
cellRange->dynamicCall("AddComment(const QVariant&)", comment);
QAxObject* axComment = cellRange->querySubObject("Comment");
QAxObject* shape = axComment->querySubObject("Shape");
shape->dynamicCall("AutoShapeType", 5);

The value can be found from Lol4t0's link: MsoAutoShapeType Enumeration. Here 5 is used to get a rounded rectangle (msoShapeRoundedRectangle). Here comes the remaining code to change the text comment format:

QAxObject* textFrame = shape->querySubObject("TextFrame");
QAxObject* chars = textFrame->querySubObject("Characters(int, int)", 1, comment.size());
QAxObject* font = chars->querySubObject("Font");
font->setProperty("Bold", false);
shape->querySubObject("TextFrame")->querySubObject("Characters(2, 3)")->querySubObject("Font")->setProperty("Size", 24);