无法在 QT 中打开 MS Word 文档 (.docx) 作为资源

问题描述 投票:0回答:1

以前的方法,我是直接指定确切路径打开文档的

QAxObject *wApp = new QAxObject("Word.Application");
auto docs = wApp->querySubObject("Documents");
auto doc  = docs->querySubObject("Open(QString)", "C:\\pathToFile\\qwe.docx");

if (doc == nullptr) {
    QMessageBox::information(this, "File opening", "File not found");
    wApp->dynamicCall("Quit()");
    delete wApp;
    return;
}

现在我想把.exe文件存放在目录下的文档文件,即这样的资源 文件路径示例
我试图以这种方式打开文件,但它不起作用。我做错了什么?

auto doc = docs->querySubObject("Open(QString)", ":/documents/qwe.docx");

错误:

QAxBase: Error calling IDispatch member Open: Exception thrown by server
             Code       : -2146823114
             Source     : Microsoft Word
 (C:\documents\qwe.docx)
             Help       : wdmain11.chm [24654]
         Connect to the exception(int,QString,QString,QString) signal to catch this exception
c++ qt ms-word qt5
1个回答
0
投票

这就是我得到的解决方案

// Get path to temporary directory
QTemporaryDir tempDir;
QString tempFilePath = tempDir.path() + "/qwe_copy.docx";

// Copying a file from resources to a temporary directory
QFile::copy(":/documents/qwe.docx", tempFilePath);

QAxObject *wApp = new QAxObject("Word.Application");
auto docs = wApp->querySubObject("Documents");
auto doc = docs->querySubObject("Open(const QString&)", tempFilePath); // Path to temporary file
© www.soinside.com 2019 - 2024. All rights reserved.