c++windowsqtms-wordword-interop

Where can I find the Qt operating Word document?


Qt operating Word example

QAxObject   *document = documents->querySubObject("Open(const QString&, bool)", inFile1, true);
QAxObject   *selection = axObject.querySubObject("Selection");
...
document->dynamicCall("SaveAs(const QString&)", outFile);
document->dynamicCall("Close()");

I'm not talking about these Qt functions querySubObject, dynamicCall. I'm talking about the Word / ActiveX functions Open, SaveAs, Close ...

I only can glimpse a small part of these functions in some example code fragment. Where can I find the full document?

It seems ms-word functions, but considering Open(const QString&, bool), there's QString, that dispels the idea.


Solution

  • The question you linked already answers yours but seeing how the accepted answer comes with no explanations at all, let us first go through what is missing there, then the correct solution (which the accepted answer is not).


    What you need to know is that Qt provides conversion functions (in several files from ActiveQt). Example, converting QString to ActiveX's BSTR:

    static inline BSTR QStringToBSTR(const QString &str)
    {
        return SysAllocStringLen(reinterpret_cast<const OLECHAR*>(str.unicode()), UINT(str.length()));
    }
    

    What matters for QAxBase::querySubObject is elsewhere: the pair of functions QVariantToVARIANT and VARIANTToQVariant manage the conversion from Qt to ActiveX type and back for all the method's parameters.


    The correct solution is to use the unaccepted most voted answer from that same question, that is:

    1. run dumpcpp.exe on WinWord. It creates the header you need to work with Word as simply as if you were on VBA.
    2. Use the classes in the Word namespace the same way you would do in VBA.

    Between the above most voted answer and Qt documentation, it should be clear how much more simple this approach is.