Issue writing image into Microsoft word using Qt Automation objects

  • Hello,

    I am generating the Microsoft DOC file using Qt and writing a series of images into it. My intention is to write as many images as possible into a page one after the other that fits correctly into the page and then insert a new page (s) for the other images. But i am unable measure the usable height of the page so that i can calculate number images that fit in the page and then progress to new page for writing remaining images. One more issue is the original image size is different to the size of the image that was written to the document. Due to the image in Qt (QPixmap) height is different to the size of the image written to the document, my calculations of measuring the remaining space is going wrong. Can any help please?

    Note: I can able to generate the DOC file, but with 4 pages. (page1 - 3 pix images; page2 - 2 pix images; page3- 2 pix images; page4- 1 pix image).
    Actually all these can go into two pages if i manually copy and paste the images into the word file.

    void MainWindow::on_pushButtonCreateWord_clicked()
    QStringList relay_images_path_list;

    QAxObject* wordApplication = new QAxObject("Word.Application");
    QAxObject* wordDocuments = wordApplication->querySubObject("Documents()");
    QAxObject* newDocument = wordDocuments->querySubObject("Add()");
    QAxObject* activeDocument = wordApplication->querySubObject("ActiveDocument()");
    QAxObject* activeWindow = activeDocument->querySubObject("ActiveWindow");
    int usableHeight = activeWindow->property("UsableHeight").toInt();
    int usableWidth = activeWindow->property("UsableWidth").toInt();
    int remaining_height = usableHeight;
    QAxObject* selection;
    QAxObject* inlineShapes;
    QAxObject* inlineShape;
    for (int i =0; i < relay_images_path_list.count(); i++)
        QString relay_image_path =;
        QPixmap pix(relay_image_path, "PNG");
        int pix_height = pix.height();
        if (pix_height <= remaining_height)
            selection = activeWindow->querySubObject( "Selection" );
            inlineShapes = selection->querySubObject( "InlineShapes" );
            inlineShape = inlineShapes->querySubObject(
                        "AddPicture(const QString&,bool,bool,QVariant)",
                         relay_image_path, false, true );
            remaining_height = remaining_height - pix.height();
            remaining_height = usableHeight;
            selection = activeWindow->querySubObject( "Selection" );
            shapes = selection->querySubObject( "InlineShapes" );
            inlineShape = shapes->querySubObject(
                        "AddPicture(const QString&,bool,bool,QVariant)",
                         relay_image_path, false, true );
            remaining_height = remaining_height - pix.height();


    activeDocument->dynamicCall("SaveAs2(const char *)", "C:\\QtProjects\\QtWordAutomation\\output.docx");


    void MainWindow::insertNewPage(QAxObject* activeWindow)
    QAxObject* selection = activeWindow->querySubObject( "Selection" );
    selection->dynamicCall( "InsertNewPage()" );

  • I have observed that, if i use 'pix.height()/1.5' instead of 'pix.height()' in the above code, then program generates DOC file with two pages with all 8 images. But this is a random try for me, but is there a way to determine the scaling factor of width and height of the QPixmap to the same image that was written to Word document?

Log in to reply