vc打开word2003文件,填写表格并退出

本文介绍如何通过VC++调用Word COM接口实现自动化编辑Word文档的方法,包括文档的打开、内容修改及保存等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

完整程序及word文件从下载:http://www.pudn.com/downloads407/sourcecode/windows/file/detail1734028.html

把111.doc拷贝到c盘目录下


1.创建一个基于对话框的程序,选择默认设置即可;


2. 添加新类:【查看】-> 【建立类向导】->【Add Class】-> 【From a type Library】,选择:C:\Program Files\Microsoft Office\OFFICE11\MSWORD.OLB文件(视office安装目录),在出现的类添加中,选择_Application, _Document, Documents,Selection四个类;环境会创建了文件msword.h,msword.cpp两个文件;


3. 在CTest1App::InitInstance()添加


	if (!AfxOleInit())
	{
		return FALSE;
	}

4. 在对话框中添加一个按钮,双击,里面程序如下:


	BeginWaitCursor();
	
	COleVariant 
		vTrue((short)TRUE), 
		vFalse((short)FALSE),
		vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
	
	_Application m_App;//定义Word提供的应用程序对象;
	Documents m_Docs;//定义Word提供的文档对象;
	Selection m_Sel;//定义Word提供的选择对象;
	m_Docs.ReleaseDispatch();
	m_Sel.ReleaseDispatch();
	m_App.m_bAutoRelease=true;
	if(!m_App.CreateDispatch("Word.Application"))
	{ 
		AfxMessageBox("创建Word2003服务失败!"); 
		exit(1); 
	}
	
	//下面是定义VARIANT变量;
	COleVariant varFilePath("c:\\111.doc");//事先写好的表格文件
	COleVariant varstrNull("");
	COleVariant varZero((short)0);
	COleVariant varTrue(short(1),VT_BOOL);
	COleVariant varFalse(short(0),VT_BOOL);
	m_Docs.AttachDispatch(m_App.GetDocuments());//将Documents类对象m_Docs和Idispatch接口关联起来;
	m_Docs.Open(varFilePath,varFalse,varFalse,varFalse,
		varstrNull,varstrNull,varFalse,varstrNull,
		varstrNull,varTrue,varTrue,varTrue, 
		vOpt, vOpt, vOpt, vOpt);
	//打开Word文档; 
	m_Sel.AttachDispatch(m_App.GetSelection());//将Selection类对象m_Sel和Idispatch接口关联起来;
	
	m_Sel.MoveDown(COleVariant((short)4),COleVariant((short)1),COleVariant((short)0));
	m_Sel.MoveDown(COleVariant((short)5),COleVariant((short)1),COleVariant((short)0));
	m_Sel.TypeText("123456789");
	m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
	m_Sel.TypeText("李明");
	m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
	m_Sel.TypeText("25");
	m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
	m_Sel.TypeText("技术员");
	m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
	m_Sel.TypeText("本科");
	m_Sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
	m_Sel.TypeText("上海市虹口区民主路315号");
	
	//save word file
	_Document oActiveDoc; 
	oActiveDoc = m_App.GetActiveDocument(); 
	oActiveDoc.SaveAs(COleVariant("c:\\填写后表格.doc"), 
		COleVariant((short)0), 
		vFalse, COleVariant(""), vTrue, COleVariant(""), 
		vFalse, vFalse, vFalse, vFalse, vFalse,
		vOpt, vOpt, vOpt, vOpt, vOpt);
	
	m_Docs.ReleaseDispatch();//断开关联;
	m_Sel.ReleaseDispatch();
	
	//退出WORD 
	m_App.Quit(vOpt, vOpt, vOpt); 
	m_App.Quit(vOpt, vOpt, vOpt);
	m_App.ReleaseDispatch();
	
	EndWaitCursor();
	
	MessageBox("word表格填写完毕!","提示",MB_ICONEXCLAMATION);

上面中的 MoveDown程序是从word录制宏得来,

Sub Macro1()
'
' Macro1 Macro
' 宏在 2011-12-17 由 微软中国 录制
'
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.MoveLeft Unit:=wdCharacter, Count:=2
    Selection.TypeText Text:="12345"
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="丽丽"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="122"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="艺术"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="无"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="上海市虹口区民主路315号"
    ActiveDocument.Save
End Sub


其中常量值用调试器可以看到:

wdLine = 5;

wdCharacter = 1;

wdCell = 12;


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值