不废话先贴代码 别忘了装office
{
HRESULT r = OleInitialize(0);
if (r != S_OK && r != S_FALSE) {
qWarning("Qt: Could not initialize OLE (error %x)", (unsigned int)r);
}
QAxObject *word_axobject = new QAxObject("Word.Application", NULL);
word_axobject->setProperty("Visible", false);
//获取所有的工作文档
QAxObject * documents = word_axobject->querySubObject("Documents");
//以文件template.doc为模版新建一个文档
documents->dynamicCall("Add(QString)", QString::fromUtf8("C:\\Users\\DELL\\Desktop\\model.docx"));
//获取当前激活的文档
QAxObject *document = word_axobject->querySubObject("ActiveDocument");
//获取文档中名字为text的标签
QAxObject*bookmark_text = document->querySubObject("Bookmarks(QVariant)", "text");
//选中标签,将字符InsertText插入到标签位置
bookmark_text = document->querySubObject("Bookmarks(QVariant)", "text");
if (!bookmark_text->isNull())
{
bookmark_text->dynamicCall("Select(void)");
bookmark_text->querySubObject("Range")->setProperty("Text", QString::fromStdString(title_str));
}
// 将文件另存为docbyqt.doc,关闭工作文档,退出应用程序
document->dynamicCall("SaveAs (const QString&)", QString::fromStdString(new_file_path));
document->dynamicCall("Close (boolean)", false);
word_axobject->dynamicCall("Quit()");
OleUninitialize();
}
这段话 放到线程里的一个函数里去执行 一开始发现怎么都创建不了对象 非常着急 这老哥提醒了下 这种对象需要自己初始化和释放