VC 6下word操作心得-将文字写入word
2012-02-23 10:24
274 查看
(1) 生成Dialog时,要选择ActiveX;
(2) 在initdialog中初始化OLE环境
BOOL CDialogTestDlg::OnInitDialog() 中添加
if(!AfxOleInit())
{
AfxMessageBox("OLE Initialization Failed");
return FALSE;
}
(3) View->ClassWizard->Automation->Add Class From Library:定位到你安装Office的目录找到MSWord.Olb(我的word版本是word2007)
添加_Application, Documents, Selection三个类即可,注意,不能像有些文章中说的把所有类都添加,否则会编译不通过。
在用到的.CPP文件中添加头文件,比如我的button消息响应函数在xxxDlg.cpp中,就在文件中添加#include "msword.h";#include <atlbase.h>。
然后,加入下面的代码即可(这段代码是参考网上的)
// TODO: Add your control notification handler code here
_Application app;
COleVariant vTrue((short)TRUE), vFalse((short)FALSE);
app.CreateDispatch(_T("Word.Application"));
app.SetVisible(FALSE);
//Create New Doc
Documents docs=app.GetDocuments();
CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
docs.Add(&tpl,&NewTemplate,&DocType,&Visble);
//Add Content:Text
Selection sel=app.GetSelection();
sel.TypeText(_T("\t\t\t\t\t \r\n"));
sel.TypeText(_T("\t\t\t\t\t\t\t\t----------先来一个简单的\r\n"));
sel.ReleaseDispatch();
docs.ReleaseDispatch();
app.SetVisible(TRUE);
app.ReleaseDispatch();
(2) 在initdialog中初始化OLE环境
BOOL CDialogTestDlg::OnInitDialog() 中添加
if(!AfxOleInit())
{
AfxMessageBox("OLE Initialization Failed");
return FALSE;
}
(3) View->ClassWizard->Automation->Add Class From Library:定位到你安装Office的目录找到MSWord.Olb(我的word版本是word2007)
添加_Application, Documents, Selection三个类即可,注意,不能像有些文章中说的把所有类都添加,否则会编译不通过。
在用到的.CPP文件中添加头文件,比如我的button消息响应函数在xxxDlg.cpp中,就在文件中添加#include "msword.h";#include <atlbase.h>。
然后,加入下面的代码即可(这段代码是参考网上的)
// TODO: Add your control notification handler code here
_Application app;
COleVariant vTrue((short)TRUE), vFalse((short)FALSE);
app.CreateDispatch(_T("Word.Application"));
app.SetVisible(FALSE);
//Create New Doc
Documents docs=app.GetDocuments();
CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
docs.Add(&tpl,&NewTemplate,&DocType,&Visble);
//Add Content:Text
Selection sel=app.GetSelection();
sel.TypeText(_T("\t\t\t\t\t \r\n"));
sel.TypeText(_T("\t\t\t\t\t\t\t\t----------先来一个简单的\r\n"));
sel.ReleaseDispatch();
docs.ReleaseDispatch();
app.SetVisible(TRUE);
app.ReleaseDispatch();
相关文章推荐
- VC 6下word操作心得二---以.dot模板生成.doc文档
- (非常好的Word操作封装类)VC中进行office编程的-操作word,可以设置文字的样式,新建表格
- C#操作Word(三)——C#向Word文档中写入文字
- [matlab]用matlab建立word,并在word中写入文字和图片
- VC操作word和excel文件,查询与读写[依赖office环境]
- TX Text Control文字处理教程(12)MS Word中字段的导入导出操作
- VC 小工具里应用到excel做数据库时的查询与写入操作
- VC操作Word之(三)
- VC操作Word系列(二)
- 用jacob将一段文字写入到word中,包括页眉页脚
- VC操作Excel写入数据源码
- VC操作Word之(三)
- vc操作word读写
- VC++ 中操作Excel文档的方法,读取,查询,写入,修改,删除
- VC操作Word系列(三)
- 用VC操作word
- c# 操作word写入特殊字符
- TX Text Control文字处理教程(12)MS Word中字段的导入导出操作
- VC操作Word之(四)
- VC++ 中操作Excel文档的方法,读取,查询,写入,修改,删除