Result Copy the table in the picture to the end of the document
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.
Copy Table
1 2 3 4 5 6 7 8 9 10 11 12 13
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);
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";
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 = newQAxObject(); 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;