1. Interface for word operation, I studied corresponding api against blogger's code in early stage QT operate Word, insert table

  2. Use Qt to operate Word document

Result Copy the table in the picture to the end of the document

Image Description

I wanted to implement a requirement today, copying the table in the word template. Searching major engines mostly modify internal properties of table or value assignment operations, no requirement to copy a table as a whole. Before starting, should understand querySubObject vs dynamicCall in QAxObjext method in QT first. Simply put, the former is calling method or calling property. Because it has been too long, my relevant knowledge is also swallowed whole, understanding is not very clear. To operate word requires this function to call COM API functions. I searched for relevant functions Microsoft, because this call comes from Microsoft.Office.Interop.Word.dll. People like me can directly search c# code, then change a little bit.

Below enters the topic. Copying table needs to use Copy() and Paste() two functions, just like copy paste when we operate word table. ps: I didn’t add pictures in this step, so I didn’t find any problems.

  1. Copy Table
QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
    if(nullptr == table)
        return;
    table->dynamicCall("Select()");
    word->querySubObject("Selection")->dynamicCall("Copy()");
	//Move cursor to end of document
    QAxObject* selec = word->querySubObject("Selection");
    QVariantList params2;
    params2.append(6);
    params2.append(0);
    selec->dynamicCall("Endof(QVariant&,QVariant&",params2);

    word->querySubObject("Selection")->dynamicCall("Paste()");

Add a c# code compared from other places, so it looks easier to understand. Source C# Copy table in word document and paste to next page

object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word._Application oWord;
            Microsoft.Office.Interop.Word._Document oDoc;
            oWord = new Microsoft.Office.Interop.Word.Application();
            //show word document
            oWord.Visible = true;
            //get word file template
            object fileName = System.Windows.Forms.Application.StartupPath + "\word.doc";
            //generate a new document based on template, equivalent to save as
            oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
                            ref oMissing, ref oMissing);

            //copy first table
            oDoc.Tables[1].Select();
            oWord.Selection.Copy();

            //operate text in table here
            oDoc.Tables[1].Cell(1, 1).Range.Text = "This is the first table";

            //next page
            object mymissing = System.Reflection.Missing.Value;
            object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
            oWord.Selection.EndKey(ref myunit, ref mymissing);
            object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
            oWord.Selection.InsertBreak(ref pBreak);

            //paste first table
            oWord.Selection.Paste();

            oDoc.Tables[2].Cell(1, 1).Range.Text = "This is second table"; 

All creation class code can realize copying table based on template docx/dot. If we want to modify values inside the table, we cannot use bookmark method. We need to assign values to this list template cell separately. Just create a method class separately. This place can create a factory in advance

    //put a box and set: star out put
    QMessageBox::warning(this,"Start","Start Output",QMessageBox::Yes);
    //create new doc application
    QAxObject* word = new QAxObject();
    bool flag = word->setControl("Word.Application");
    if(!flag){
        flag = word->setControl("kwps.Application");
        if(!flag)
            return;
    }

    //QAxWidget* word = new QAxWidget("Word.Application",0,Qt::MSWindowsOwnDC);
    //set unvisible
    word->setProperty("Visible",false);
    word->setProperty("DisplayAlerts",false);
    //get all document
    QAxObject* documents = word->querySubObject("Documents");
    //use document2.dot template to create a new document
    documents->dynamicCall("Add(QString)",QString::fromLocal8Bit("C:/Users/****/Desktop/4444.docx"));
    //get active document
    QAxObject* document = word->querySubObject("ActiveDocument");

    //get bookmark which name is test in this document
    QAxObject* bookmark_test = document->querySubObject("BookMarks(QVariant)","test");
    //select target bookmark and set value
    if(!bookmark_test->isNull()){
        QString sText = ui->pcEdit->text();
        qDebug()<<sText;
        bookmark_test->dynamicCall("Select(void)");
        bookmark_test->querySubObject("Range")->setProperty("Text",sText);
    }
    // insert table
//    int row = 14;
//    int col = 0;
    int tableIndex = 4;
    QAxObject* tables = document->querySubObject("Tables");
    if(nullptr == tables)
        return;
    QAxObject* selection = word->querySubObject("Selection");
    if(nullptr == selection)
        return;
    QAxObject* range = selection->querySubObject("Range");
    if(nullptr == range)
        return;

    QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
    if(nullptr == table)
        return;
    int table_count = tables->dynamicCall("Count").toInt();
    qDebug()<<table_count;

    QAxObject* cols = table->querySubObject("Rows");
    int count = cols->dynamicCall("Count").toInt();
    qDebug()<<count;

    /*QAxObject* selec = word->querySubObject("Selection");
    QVariantList params2;
    params2.append(6);
    params2.append(0);
    selec->dynamicCall("Endof(QVariant&,QVariant&)",params2);*/


    table->dynamicCall("Select()");

    word->querySubObject("Selection")->dynamicCall("Copy()");

    QAxObject* selec = word->querySubObject("Selection");
    QVariantList params2;
    params2.append(6);
    params2.append(0);
    selec->dynamicCall("Endof(QVariant&,QVariant&",params2);

    qDebug()<<"selection tables count:"<<selection->querySubObject("Tables")->dynamicCall("Count").toInt();
    QVariantList t_params;
    t_params.append("WdRecoveryType.wdUseDestinationStylesRecovery");
    //selection->dynamicCall("PasteAndFormat(WdRecoveryType)",t_params);

    word->querySubObject("Selection")->dynamicCall("Paste()");

    //this table index is test value
    QAxObject* t_table = tables->querySubObject("Item(int)",5);
    QVariantList cellPara;
    cellPara.append(0);
    cellPara.append(0);
    t_table->querySubObject("Cell(QVariant&,QVariant&)",cellPara)->querySubObject("Range")->dynamicCall("SetText(QString)",_unit->getProblemSymbol());
    
    
    
    
    
    //save file as doc|docx
    QString path_save = QFileDialog::getSaveFileName(this,"Save","../","word(*doc)");
    if(path_save.isEmpty() == true){
        return;
    }
    document->dynamicCall("SaveAs(const QString&))",QDir::toNativeSeparators(path_save));
    document->dynamicCall("Close (boolean)",false);
    word->dynamicCall("Quit()");
    QMessageBox::warning(this,"Done","File saved",QMessageBox::Yes);

Insert New Page

 m_word->querySubObject("Selection")->dynamicCall("InsertNewPage()");