Results 1 to 5 of 5

Thread: Qt app crashes when trying to open Word doc that is already opened

  1. #1
    Join Date
    Feb 2016
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Qt app crashes when trying to open Word doc that is already opened

    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.

    Qt Code:
    1. my_app = new QAxObject("Word.Application", this);
    2. QAxObject* my_docs = my_app->querySubObject("Documents");
    3.  
    4. //Open docxFile
    5. QString filename(docxFile);
    6. QVariant confirmconversions(false);
    7. QVariant readonly(false);
    8. QVariant addtorecentfiles(false);
    9. QVariant passworddocument("");
    10. QVariant passwordtemplate("");
    11. QVariant revert(false);
    12.  
    13. 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);
    14.  
    15. //Pull out active document object
    16. QAxObject* active_doc = my_app->querySubObject("ActiveDocument");
    To copy to clipboard, switch view to plain text mode 


    Thanks!

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt app crashes when trying to open Word doc that is already opened

    All functions return pointer to QAxObject. You have to test if returned pointer is not null.

  3. #3
    Join Date
    Feb 2016
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt app crashes when trying to open Word doc that is already opened

    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)

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt app crashes when trying to open Word doc that is already opened

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    bobbayribs (2nd June 2017)

  6. #5
    Join Date
    Feb 2016
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt app crashes when trying to open Word doc that is already opened

    Thank you d_stranz, I set read-only to true and everything works fine now!

    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!!

Similar Threads

  1. Replies: 1
    Last Post: 24th February 2015, 13:13
  2. Replies: 2
    Last Post: 8th January 2012, 11:31
  3. Open Ms Word, Ms excel, ppt files in QT -Linux
    By amankamboj1 in forum Qt Programming
    Replies: 3
    Last Post: 5th September 2009, 08:55
  4. can Qt open MS word document?
    By coder1985 in forum Qt Programming
    Replies: 1
    Last Post: 25th December 2007, 14:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.