Qt 使用QAxObject 访问 Word 异常问题 --- 持续更新中

喜夏-厌秋 提交于 2020-05-10 00:43:31

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]

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!