Search code examples
c++qtcomms-wordactiveqt

Center a picture after adding it to Word using Active Qt


I'm generating a Word document via Qt's Active Qt module.

I'm able to write to the document, specify the "style" for that writing (bold, italic, justified aligment, etc.) and query diverse parts of it.

Right now I'm trying to display a picture and position it right in the center of the page.

For inserting the picture:

QAxObject word( "Word.Application" );

QAxObject* activeDocument = word.querySubObject("ActiveDocument");
QAxObject* activeWindow = activeDocument->querySubObject( "ActiveWindow" );
QAxObject* selection = activeWindow->querySubObject( "Selection" );

selection->dynamicCall( "Collapse(int)", 0 );
const int pos = selection->dynamicCall( "End" ).toInt();

QAxObject* shapes = activeDocument->querySubObject( "Shapes" );
QAxObject* shape = shapes->dynamicCall( "AddPicture(QString,bool,bool,float,float,float,float)",
                     picPath,
                     true, true );

This inserts the picture on the left of the page. In Word, I can insert the picture, select it and specify its alignment (centered, in my case), but I'm not able to do this via code.

I tried to set the pic's anchor to the center, but it still appears at the left of the page:

shape->querySubObject( "Anchor" )->querySubObject( "ParagraphFormat" )->setProperty( "Alignment", 1);    // 1 == wdAlignParagraphCenter

and:

shape->querySubObject( "Anchor" )->dynamicCall("InsertAlignmentTab(int)",1); // 1 == center

Also, note that when I open the created document with the inserted images, if I select one of the pictures, I can't center it; whereas using the Insert menu, I can select the picture and center it.

Is there a way to center any picture I insert into Word?


Solution

  • I found a way to center the picture in question.

    QAxObject* shapes = selection->querySubObject( "InlineShapes" );
    QAxObject* inlineShape = shapes->querySubObject(
                "AddPicture(const QString&,bool,bool,QVariant)",
                 picPath, false, true )
    ;
    inlineShape->dynamicCall( "ScaleHeight", height );
    inlineShape->dynamicCall( "ScaleWidth", width );
    
    selection->querySubObject( "ParagraphFormat" )->setProperty( "Alignment", 1 );
    
    QAxObject* range = shape->querySubObject( "Range" );
    
    const int start = range->property( "Start" ).toInt();
    const int end = range->property( "End" ).toInt();
    selection->dynamicCall( "SetRange(int,int)", start, end );
    selection->dynamicCall( "Collapse(int)", 0 );
    
    selection->querySubObject( "ParagraphFormat" )->setProperty( "Alignment", 3 );
    

    Instead of querying the Shapes object, I now query the InlineShapes one. This is an important, since now I'm able to use the selection object (which now is pointing to the position of the inserted picture) and specify it's alignment, via the ParagraphFormat subobject.

    After that, I update the selection with the picture Range to start typing after the pic and set the alignment back to justified.