PDA

View Full Version : Qt app crashes when trying to open Word doc that is already opened



bobbayribs
1st June 2017, 13:41
Hi all,

I have an app that opens Word docs, parses them, and saves them to text file. Everything works great, except when the Word doc is already opened. Then a popup opens:

File In Use
Do you want to:
Open a Read Only copy
Create a local copy and merge your changes later
Receive notification when the original copy is available

And my app hangs and crashes.

How can I detect a word doc is already opened? Then I just would output an error message, skip opening, and parsing that particular word doc.



my_app = new QAxObject("Word.Application", this);
QAxObject* my_docs = my_app->querySubObject("Documents");

//Open docxFile
QString filename(docxFile);
QVariant confirmconversions(false);
QVariant readonly(false);
QVariant addtorecentfiles(false);
QVariant passworddocument("");
QVariant passwordtemplate("");
QVariant revert(false);

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

//Pull out active document object
QAxObject* active_doc = my_app->querySubObject("ActiveDocument");



Thanks!

Lesiok
1st June 2017, 14:28
All functions return pointer to QAxObject. You have to test if returned pointer is not null.

bobbayribs
1st June 2017, 16:22
Cannot test for null pointer because the "File In Use" popup window appears before a null pointer is returned:

my_app: QAxObject(0x4ec1520)
my_docs: QAxObject(0x4ec1bf8)
//"File In Use" popup window appears here. This is the querySubObject("Open...) command.
doc: QObject(0x0)

d_stranz
1st June 2017, 17:58
If you want to open it read-only, why are you setting the readonly QVariant to "false"? That's likely the source of your file in use error.

And I echo Lesiok's comment: You must test pointers for non-null before dereferencing them (i.e. using them to call another method). It's a guaranteed crash if get one and you don't test it.

bobbayribs
2nd June 2017, 17:45
Thank you d_stranz, I set read-only to true and everything works fine now! :o

Since I was opening the doc file and saving it as a text file, I thought it couldn't be read-only, but I was wrong.

If saving a file, shouldn't one needs read/write permissions on the doc? Anyway, thank you for the help!!