2

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);
Michael Hilbert
  • 246
  • 1
  • 4
  • 11

2 Answers2

2
  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.

Lol4t0
  • 12,444
  • 4
  • 29
  • 65
  • According to http://msdn.microsoft.com/en-us/library/office/ff862770%28v=office.15%29.aspx the value for msoShapeRoundedRectangle should be 5 (Rounded rectangle), therefore I do not understand why shape->setProperty("AutoShapeType", 5); should not work. Can you give an complete code example how to set the shape to a rounded rectangle. – Michael Hilbert Oct 28 '14 at 14:14
  • Ok, It looks weird, but have you tried `shape->setProperty("AutoShapeType", "msoShapeSnipRoundRectangle");` like in [docs](http://qt-project.org/doc/qt-4.8/qaxbase.html#querySubObject) example. Or else it looks like `shape->setProperty("AutoShapeType", "5");` (with qoutes) should also work. If it does, I'll write an explanation. – Lol4t0 Oct 28 '14 at 17:25
  • Even with quotes the shape does not change. – Michael Hilbert Oct 28 '14 at 22:28
1

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);
Michael Hilbert
  • 246
  • 1
  • 4
  • 11