Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue writing image into Microsoft word using Qt Automation objects
Qt 6.11 is out! See what's new in the release blog

Issue writing image into Microsoft word using Qt Automation objects

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    picl
    wrote on last edited by
    #1

    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;
    relay_images_path_list.append("C:\QtProjects\QtWordAutomation\pix1.png");
    relay_images_path_list.append("C:\QtProjects\QtWordAutomation\pix2.png");
    relay_images_path_list.append("C:\QtProjects\QtWordAutomation\pix3.png");
    relay_images_path_list.append("C:\QtProjects\QtWordAutomation\pix4.png");
    relay_images_path_list.append("C:\QtProjects\QtWordAutomation\pix5.png");
    relay_images_path_list.append("C:\QtProjects\QtWordAutomation\pix6.png");
    relay_images_path_list.append("C:\QtProjects\QtWordAutomation\pix7.png");
    relay_images_path_list.append("C:\QtProjects\QtWordAutomation\pix8.png");

    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 = relay_images_path_list.at(i);
    
        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();
        }
        else
        {
            insertNewPage(activeWindow);
            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");
    activeDocument->dynamicCall("Close()");
    wordApplication->dynamicCall("Quit()");
    

    }

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

    1 Reply Last reply
    0
    • P Offline
      P Offline
      picl
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved