c++ – Qt ActiveX Word change table format

Question:

Faced the need to work with Word in Qt. It is necessary to set the format of the width of each column, as well as to combine some cells. On the Excel – it is clear. How to do it in a Word is not clear.

QAxObject *m_word = new QAxObject("Word.Application");
QAxObject* docs = m_word->querySubObject( "Documents" );

QVariant filename("D:\\...\\word\\...\\doc.doc");
QVariant confirmconversions(false);
QVariant readonly(false);
QVariant addtorecentfiles(false);
QVariant passworddocument("");
QVariant passwordtemplate("");
QVariant revert(false);

QAxObject* doc = docs->querySubObject("Open(const QVariant&, const QVariant&, "
                                      "const QVariant&, const QVariant&, const QVariant&, const QVariant&,"
                                      "const QVariant&)", filename, confirmconversions, readonly,
                                      addtorecentfiles, passworddocument, passwordtemplate, revert);

QAxObject *ActiveDocument = m_word->querySubObject("ActiveDocument()");
QAxObject *Range = ActiveDocument->querySubObject("Range()");
QAxObject *font = Range->querySubObject("Font");
Range->dynamicCall("InsertAfter(Text)", "текст");
Range->setProperty("Alignment", 2);
font->setProperty("Alignment", 2);
font->setProperty("Name", "Times New Roman");
font->setProperty("Size", 14);
font->setProperty("Bold", 2);

QAxObject* selection = m_word->querySubObject("Selection") ;
QAxObject* Tables = selection->querySubObject("Tables()");
QAxObject* NewTable = Tables->querySubObject("Add(const QVariant&, const QVariant&, const QVariant&, "
                                             "const QVariant& DefaultTableBehavior, const QVariant& AutoFitBehavior)", ActiveDocument->dynamicCall("Range()"), 3, 4, 1, 1);

QAxObject* cell = NewTable->querySubObject("Cell(Row, Column)" , QVariant(1), QVariant(1)) ;
QAxObject* celR = cell->querySubObject("Range()") ;
celR->dynamicCall("InsertAfter(Text)", "Это ячейка 1:1,kz,kz,zk,zk,zk,zk.kzhtrhrhrthtrhrt");

cell = NewTable->querySubObject("Cell(Row, Column)" , 1, 2) ;
celR = cell->querySubObject("Range()") ;
celR->dynamicCall("InsertAfter(Text)", "Это ячейка 1:2");
cell = NewTable->querySubObject("Cell(Row, Column)" , 1, 3) ;
celR = cell->querySubObject("Range()") ;
celR->dynamicCall("InsertAfter(Text)", "Это ячейка 1:3");
cell = NewTable->querySubObject("Cell(Row, Column)" , 1, 4) ;
celR = cell->querySubObject("Range()") ;
celR->dynamicCall("InsertAfter(Text)", "Это ячейка 1:4");
cell = NewTable->querySubObject("Cell(Row, Column)" , 2, 1) ;
celR = cell->querySubObject("Range()") ;
celR->dynamicCall("InsertAfter(Text)", "Это ячейка 2:1");
cell = NewTable->querySubObject("Cell(Row, Column)" , 2, 2) ;
celR = cell->querySubObject("Range()") ;
celR->dynamicCall("InsertAfter(Text)", "Это ячейка 2:2");
cell = NewTable->querySubObject("Cell(Row, Column)" , 3, 1) ;
celR = cell->querySubObject("Range()") ;
celR->dynamicCall("InsertAfter(Text)", "Это ячейка 3:1");

delete celR;
delete cell;
delete NewTable;
delete Tables;
delete selection;
delete font;
delete Range;

m_word->querySubObject("ActiveDocument")->dynamicCall("Close()");
m_word->dynamicCall("SetDisplayAlerts(bool)", false);
m_word->dynamicCall("Quit()");

delete ActiveDocument;
delete doc;
delete docs;
delete m_word;

Answer:

If the width of the tramline is changed, then this is how:

   QAxObject *razmer = celR->querySubObject("Columns()");
    razmer->setProperty("Width",300);// устанавливаю её размер
Scroll to Top