以前的方法,我是直接指定确切路径打开文档的
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
这就是我得到的解决方案
// 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