QAxObject ms Word - Unable to replace text, non default arguments syntax of dynamicCall()
-
I'm having trouble trying to replace text in a ms word doc through QAxObject.
I didn't find the right syntax to pass only some arguments in dynamicCall, leaving the default ones.e.g. in vba you can do this:
@myRange.Find.Execute FindText:="hi", ReplaceWith:="hello"@while in the online code I have seen the coders always pass all the arguments.
this syntax seems to work to me
@content->dynamicCall("InsertAfter (Text:=QVariant&)", "hello");@But I'm not sure if it is correct.
the real problem however is that I can't replace text in the document using the Find.Execute() method.
if I try to call this method the Find object emits an exception signal with these parameters:
@Code: -119799803
Source: ""
Description: ""
Help: "" @Here is the code that gives the error:
@ QAxWidget* mWordWidget = new QAxWidget("Word.Application", this);
mWordWidget->setVisible(true);
mWordWidget->dynamicCall( "SetVisible(bool)", true);QAxObject* documents = mWordWidget->querySubObject("Documents"); QAxObject* doc = documents->querySubObject("Add()"); doc = mWordWidget->querySubObject("ActiveDocument"); QAxObject* content = doc->querySubObject("Content"); content->dynamicCall("InsertAfter (Text:=QVariant&)", "hello"); content = doc->querySubObject("Content"); QAxObject* find = content->querySubObject("Find"); connect(find, SIGNAL(exception(int, const QString&, const QString&, const QString&)), this, SLOT(catchException(int, const QString&, const QString&, const QString&))); QList<QVariant> list; list.push_back("hello"); list.push_back(true); list.push_back(true); list.push_back(false); list.push_back(false); list.push_back(false); list.push_back(QVariant()); list.push_back(QVariant()); list.push_back(QVariant()); list.push_back("world"); list.push_back(QVariant()); list.push_back(QVariant("all")); list.push_back(false); list.push_back(false); list.push_back(false); qDebug() << "is find object null: " << find->isNull(); find->dynamicCall("Execute (QVariant&, QVariant&, QVariant&, QVariant&, QVariant&, QVariant&, QVariant&, QVariant&, QVariant& ,QVariant& ,QVariant& , QVariant&, QVariant&, QVariant&, QVariant&)", list);
// find->dynamicCall("Execute (FindText:=QVariant&, ReplaceWith:=QVariant&)", "hello", "world");@
here a Qt project with the code, ready to be compiled.
https://dl.dropbox.com/u/11874818/replaceTextQAxObjectMsWord.zip
-
Here is the code that replace the text successfully:
@QAxWidget* mWordWidget = new QAxWidget("Word.Application", this);
mWordWidget->setProperty("Visible", true);QAxObject* documents = mWordWidget->querySubObject("Documents");
documents->dynamicCall("Add()");
QAxObject doc = mWordWidget->querySubObject("ActiveDocument");
QAxObject content = doc->querySubObject("Content");
content->dynamicCall("InsertAfter (Text:=QVariant&)", "hello");
content = doc->querySubObject("Content");
QAxObject selection = mWordWidget->querySubObject("Selection");
QAxObject find = selection->querySubObject("Find");QList<QVariant> list2;
list2.append("hello");//find text
list2.append(QVariant());//2
list2.append(QVariant());//3
list2.append(QVariant());//4
list2.append(QVariant());//5
list2.append(QVariant());//6
list2.append(QVariant());//7
list2.append(QVariant());//8
list2.append(QVariant());//9
list2.append("olleh");//replace text
list2.append(2);//replace all:2
list2.append(QVariant());//12
list2.append(QVariant());//13
list2.append(QVariant());//14
list2.append(QVariant());//15
connect(find, SIGNAL(exception(int, const QString&, const QString&, const QString&)), this, SLOT(catchException(int, const QString&, const QString&, const QString&)));find->dynamicCall("Execute (QVariant&, QVariant&, QVariant&, QVariant&, QVariant&, QVariant&, QVariant&, QVariant&, QVariant& ,QVariant& ,QVariant& , QVariant&, QVariant&, QVariant&, QVariant&)", list2);
@