- pro 文件中要加入
CONFIG += qaxcontainer
2. main.cpp
- #include <QApplication>
- #include <QAxWidget>
- #include <QAxObject>
- int main(int argc, char* argv[])
- {
- QApplication app(argc, argv);
- QAxWidget *word=new QAxWidget("Word.Application", 0, Qt::MSWindowsOwnDC);
- word->setProperty("Visible", true);
- // 获取所有的工作文档
- QAxObject * documents = word->querySubObject("Documents");
- // 以文件template.doc为模版新建一个文档
- documents->dynamicCall("Add(QString)",QString::fromLocal8Bit("D:/doc/template.doc"));
- // 获取当前激活的文档
- QAxObject *document=word->querySubObject("ActiveDocument");
- // 获取文档中名字为text的标签
- QAxObject*bookmark_text=document->querySubObject("Bookmarks(QVariant)","text");
- // 选中标签,将字符InsertText插入到标签位置
- if(!bookmark_text->isNull()){
- bookmark_text->dynamicCall("Select(void)");
- bookmark_text->querySubObject("Range")->setProperty("Text","InsertText");
- }
- // 将文件另存为docbyqt.doc,关闭工作文档,退出应用程序
- document->dynamicCall("SaveAs (const QString&)", QString("D:/doc/docbyqt.doc"));
- document->dynamicCall("Close (boolean)", false);
- word->dynamicCall("Quit()");
- return app.exec();
- }
http://blog.csdn.net/zbc415766331/article/details/36396159
网上关于qt操作word的资料实在是太少了,搞了半天终于能在word 表格中插入标签的地方插入图片了。
1.在pro中加入
CONFIG += qaxcontainer
2.main.cpp
- #include <QApplication>
- #include <QAxWidget>
- #include <QAxObject>
- int main(int argc, char* argv[])
- {
- QApplication app(argc, argv);
- QAxWidget *word=new QAxWidget("Word.Application", 0, Qt::MSWindowsOwnDC);
- word->setProperty("Visible", true);
- // 获取所有的工作文档
- QAxObject * documents = word->querySubObject("Documents");
- // 以文件template.doc为模版新建一个文档
- documents->dynamicCall("Add(QString)",QString::fromLocal8Bit("D:/doc/template.doc"));
- // 获取当前激活的文档
- QAxObject *document=word->querySubObject("ActiveDocument");
- // 获取文档中名字为text的标签
- QAxObject*bookmark_text=document->querySubObject("Bookmarks(QVariant)","text");
- // 选中标签,将字符InsertText插入到标签位置
- if(!bookmark_text->isNull()){
- bookmark_text->dynamicCall("Select(void)");
- bookmark_text->querySubObject("Range")->setProperty("Text","InsertText");
- }
- // 获取文档中名字为pic的标签
- QAxObject*bookmark_pic=document->querySubObject("Bookmarks(QVariant)","pic");
- // 选中标签,将图片插入到标签位置
- if(!bookmark_pic->isNull())
- {
- bookmark_pic->dynamicCall("Select(void)");
- QAxObject *range;
- range = bookmark_pic->querySubObject("Range");
- QVariant tmp = range->asVariant();
- QList<QVariant>qList;
- qList<<QVariant("D:/1.png");
- qList<<QVariant(false);
- qList<<QVariant(true);
- qList<<tmp;
- QAxObject *Inlineshapes = document->querySubObject("InlineShapes");
- Inlineshapes->dynamicCall("AddPicture(const QString&, QVariant, QVariant ,QVariant)",qList);
- }
- // 将文件另存为docbyqt.doc,关闭工作文档,退出应用程序
- document->dynamicCall("SaveAs (const QString&)", QString("D:/doc/docbyqt.doc"));
- document->dynamicCall("Close (boolean)", false);
- word->dynamicCall("Quit()");
- return app.exec();
- }
addpicture 微软官方注解:http://msdn.microsoft.com/en-us/library/office/aa171535(v=office.11).aspx
http://blog.csdn.net/zbc415766331/article/details/36659993
HRESULT r;
r = CoInitialize(0);
// r =CoInitializeEx(0, COINIT_MULTITHREADED);
// m_word = new QAxWidget("Word.Application", widget, Qt::MSWindowsOwnDC );
m_word = new QAxObject("Word.Application" );
if( m_word == NULL ) return true;
//m_word->setProperty("Visible", false); //设置为隐藏
QAxObject * documents = m_word->querySubObject("Documents"); //获取所有工作的文档
http://www.qtcn.org/bbs/read-htm-tid-60802.html