Finding and replacing in MS Word via QAxObject
-
Good day! I try to replace t1 to t2 in document test1.docx and save the result in document test2.docx.
But there is an error: "QAxBase::dynamicCallHelper: Selection: No such property in [???????? Microsoft Word 97?2003]..."
Could you, please, help me to solve this problem?
Best regards, Olga#include "widget.h" #include <QAxObject> #include <QAxWidget> #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); QAxWidget word("Word.Application"); word.setProperty("Visible", true); QAxObject * documents = word.querySubObject("Documents"); QAxObject * document=documents->querySubObject("Open(QString)","C:/Users/Olga/Desktop/test.docx"); QAxObject * sel = document->querySubObject("Selection"); QAxObject * find = sel->querySubObject("Find"); QString sOld = "t1"; // change from this QString sNew = "t2"; // to this one at the time find->dynamicCall("Execute(QString)",sOld); sel->dynamicCall("TypeText(QString)",sNew); document->dynamicCall("SaveAs (const QString&)", QString("C:/Users/Olga/Desktop/test2.docx")); document->dynamicCall("Close (boolean)", false); word.dynamicCall("Quit (void)"); return 0; }
-
Hi, the find and replace command in MS Word is a bit convoluted, try something like:
auto find = document->querySubObject("Content")->querySubObject("Find"); // some params for the find/replace call bool bMatchCase = false; bool bMatchWholeWord = false; bool bMatchWildCards = false; bool bReplaceAll = true; QString sOld = "t1"; // change from this QString sNew = "t2"; // to this one at the time QVariantList vl = { sOld, bMatchCase, bMatchWholeWord, bMatchWildCards, false, false, true, 1, false, sNew, bReplaceAll ? "2" : "1"); find->dynamicCall("Execute(QString,bool,bool,bool,bool,bool,bool,int,bool,QString,int)",vl);
-
Hi, the find and replace command in MS Word is a bit convoluted, try something like:
auto find = document->querySubObject("Content")->querySubObject("Find"); // some params for the find/replace call bool bMatchCase = false; bool bMatchWholeWord = false; bool bMatchWildCards = false; bool bReplaceAll = true; QString sOld = "t1"; // change from this QString sNew = "t2"; // to this one at the time QVariantList vl = { sOld, bMatchCase, bMatchWholeWord, bMatchWildCards, false, false, true, 1, false, sNew, bReplaceAll ? "2" : "1"); find->dynamicCall("Execute(QString,bool,bool,bool,bool,bool,bool,int,bool,QString,int)",vl);
@hskoglund , thank you a lot!
As you can see - I've taken your code from one of your previous posts and helplessly tried to do something :)
Now, with your help it works!Wish you all the best
OlgaP.S.: Soon I'll post new couple of questions - but in another topics..
-
Hi, the find and replace command in MS Word is a bit convoluted, try something like:
auto find = document->querySubObject("Content")->querySubObject("Find"); // some params for the find/replace call bool bMatchCase = false; bool bMatchWholeWord = false; bool bMatchWildCards = false; bool bReplaceAll = true; QString sOld = "t1"; // change from this QString sNew = "t2"; // to this one at the time QVariantList vl = { sOld, bMatchCase, bMatchWholeWord, bMatchWildCards, false, false, true, 1, false, sNew, bReplaceAll ? "2" : "1"); find->dynamicCall("Execute(QString,bool,bool,bool,bool,bool,bool,int,bool,QString,int)",vl);
-
Good day!
Do you know, how to do the same thing via OpenOffice or LibreOffice?
Thank you in advance!
Olga@Oda412
You can't! Well at least if you are asking about this thread, since it's ActiveX to communicate with MS Word.You would have to find an API for LibreOffice. I am not an expert, maybe start out from https://api.libreoffice.org/examples/examples.html ?