Qt 使用QAxObject 訪問 Word 異常問題 --- 持續更新中


1、

CoCreateInstance failure (尚未調用CoInitialize。)

QAxbase::dynamicCallHelper: Object is not initialized, or initialization failed.

添加 OleInitialize的調用

    HRESULT result = OleInitialize(nullptr);   //換成CoInitialize似乎也是一樣的
    if (result != S_OK && result != S_FALSE)
    {
        qDebug()<<QString("Could not initialize OLE (error %x)").arg(static_cast<unsigned int>(result));
    }

    m_pWord = new QAxObject();
    bool bFlag = m_pWord->setControl( "word.Application" );

 

2、QAxBase: Error calling IDispatch member Add: Exception thrown by server

        Code    : -2146823114

        Source    :Microsoft Word

        Description   :?????????????????????

        Help      :wdmain11.chm[24654]

    Connect to the exception<int, QString, QString, Qstring> signal to catch this exception

      QAxBase: Error calling IDispatch member ActiveDocument: Exception thrown by server

        Code    : -2146824040

        Source    :Microsoft Word

        Description   :?????????????????????

        Help      :wdmain11.chm[37016]

    Connect to the exception<int, QString, QString, Qstring> signal to catch this exception

QAxBase::
Signals:
exception(int code, const QString &source, const QString &desc, const QString &help)
根據提示,捕獲異常
//.h
public slots:
    void slots_exception(/*int a, QString b, QString c, QString d*/);

//usecase
connect(m_pWord, SIGNAL(exception(int, QString, QString, QString)), this, SLOT(slots_exception()));

這里有兩個注意點:
a-QAxObject從QAxBase繼承來的信號,不能用最新版函數指針版connect,只能使用舊版的宏字符串版本connect。
b-舊版的宏字符串版本的connect關聯的槽函數,必須是"訪問修飾符+slots:"聲明的槽函數。 否則會報錯No Such slots。 
   我們知道,新版函數指針的版的connect,槽函數可以是普通的成員函數。

-2146823114 "Mircosoft Word" 抱歉, 找不到您的文件。是否可能被移動、重命名或刪除? wdmain11.chm [24654]
-2146824040 "Mircosoft Word" 因為沒有打開的文檔,所以這一命令無效。 wdmain11.chm [37016]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM