- 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");
-
- documents->dynamicCall("Add(QString)",QString::fromLocal8Bit("D:/doc/template.doc"));
-
- QAxObject *document=word->querySubObject("ActiveDocument");
-
-
- QAxObject*bookmark_text=document->querySubObject("Bookmarks(QVariant)","text");
-
- if(!bookmark_text->isNull()){
- bookmark_text->dynamicCall("Select(void)");
- bookmark_text->querySubObject("Range")->setProperty("Text","InsertText");
- }
-
-
- 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");
-
- documents->dynamicCall("Add(QString)",QString::fromLocal8Bit("D:/doc/template.doc"));
-
- QAxObject *document=word->querySubObject("ActiveDocument");
-
-
- QAxObject*bookmark_text=document->querySubObject("Bookmarks(QVariant)","text");
-
- if(!bookmark_text->isNull()){
- bookmark_text->dynamicCall("Select(void)");
- bookmark_text->querySubObject("Range")->setProperty("Text","InsertText");
- }
-
-
- 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);
- }
-
-
- 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
Qt 向word中插入文字(使用QAxWidget和QAxObject)
原文:http://www.cnblogs.com/findumars/p/5346612.html