VC 操作Word.docx
- 文档编号:27657433
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:7
- 大小:17.73KB
VC 操作Word.docx
《VC 操作Word.docx》由会员分享,可在线阅读,更多相关《VC 操作Word.docx(7页珍藏版)》请在冰豆网上搜索。
VC操作Word
VC2010操作Word2010
1.创建MFC工程,非Unicode。
2.添加Word的类库添加所有的接口,可以作为一个文件夹,以后再使用只要加入该文件夹就可以了。
由于函数命名冲突,需要在生成的头文件中做相应处理,即:
//#import“C:
\\ProgramFiles\\MicrosoftOffice\\Office14\\MSWORD.OLB“no_namespace#import“C:
\\ProgramFiles\\MicrosoftOffice\\Office14\\MSWORD.OLB“no_namespace\raw_interfaces_only\rename(“FindText“,“_FindText“)
\rename(“Rectangle“,“_Rectangle“)
\rename(“ExitWindows“,“_ExitWindows“)
对于不同版本的office,其默认的类型库位置如下表所示:
Office版本路径Office2003C:
\ProgramFiles\MicrosoftOffice\Office11Office
xxC:
\ProgramFiles\MicrosoftOffice\Office12Office
xxC:
\ProgramFiles\MicrosoftOffice\Office14Office
xxC:
\ProgramFiles\MicrosoftOffice\Office15特别的,wordxx在vcxx下必须手动指定类型库文件位置。
3.在stdafx中添加调用的头文件#include“Wordxx/CApplication.h“//word程序对象#include“Wordxx/CDocuments.h“//文档集对象#include“Wordxx/CDocument0.h“//单个docx对象#include“Wordxx/CSelection.h“//该对象代表窗口或窗格中的当前所选内容,使用率最高#include“Wordxx/CCell.h“//表格单元格#include“Wordxx/CCells.h“//表格单元格集合#include“Wordxx/CRange.h“//该对象代表文档中的一个连续范围#include“Wordxx/CTable0.h“//单个表格#include“Wordxx/CTables0.h“//表格集合#include“Wordxx/CFont0.h“//字体#include“Wordxx/CParagraphs.h“//段落集合#include“Wordxx/CParagraphFormat.h“//段落样式#include“Wordxx/CParagraph.h“//单个段落#include“Wordxx/CnlineShape.h“//Inline图形对象集合#include“Wordxx/CnlineShapes.h“//单个Inline图像对象#include“Wordxx/CRow.h“//单个行#include“Wordxx/CRows.h“//行集合#include“Wordxx/CFields.h“#include“Wordxx/CPane0.h“//页眉等设置#include“Wordxx/CWindow0.h“//页眉等设置#include“Wordxx/CView0.h“//页眉等设置#include“Wordxx/CPageSetup.h“//页面设置
4.注销每个引入的头文件的import行//#import“C:
\\ProgramFiles\\MicrosoftOffice\\Office14\\MSWORD.OLB“no_namespace不过如果使用wdMove等参数的话,只需要在CApplication.h文件中改为:
//#import“C:
\\ProgramFiles\\MicrosoftOffice\\Office14\\MSWORD.OLB“no_namespace#import“C:
\\ProgramFiles\\MicrosoftOffice\\Office14\\MSWORD.OLB“no_namespace\raw_interfaces_only\rename(“FindText“,“_FindText“)
\rename(“Rectangle“,“_Rectangle“)
\rename(“ExitWindows“,“_ExitWindows“)
否则,如果没有上面一段,则报错提示为(即全部头文件都注销import的情况下):
但是上述的方法会带来一个问题,就是当word版本改变时会出现问题,解决办法就是上述报错的变量都用其数值代替就不会有问题了(写代码时可以加上用以查看wdMove等变量的值),如下面一些:
wdAlignParagraphLeft=0wdAlignParagraphCenter=1wdAlignParagraphRight=2wdAlignParagraphJustify=3wdParagraph=4wdMove=0wdStyleHeading1=2)
);//设置为“标题1“样式,wdStyleHeading1=-2//设置选择区域字体,一定要放在样式后,否则格式会被样式的覆盖CFont0font=wordSelection.get_Font();font.put_Name(_T(“微软雅黑“));font.put_Size(16);//必须选择该行才可以修改,即必须有HomeKey那行//获得当前段落,并设置对齐方式CParagraphlastPara=GetCurParagraph(docx);lastPara.put_Alignment
(1);//wdAlignParagraphLeft=0,wdAlignParagraphCenter=1,wdAlignParagraphRight=2//结束当前段落编辑,移动光标到段落后wordSelection.EndOf(COleVariant((short)4),COleVariant((short)0));//wdParagraph=4,wdMove=0wordSelection.TypeParagraph();//新起一段COleVariantcovTime(_T(“yyyy-MM-dd:
dddd“));//时间格式可调整wordSelection.InsertDateTime(covTime,covFalse,covOptional,covOptional,covOptional);//插入当前时间wordSelection.EndOf(COleVariant((short)4),COleVariant((short)0));//结束当前段落编辑,wdParagraph=4,wdMove=0//生成表格MakeRTITable(docx,wordSelection);//以下为为不同段落设置不同字体和对齐方式示例wordSelection.TypeParagraph();//新起一段wordSelection.TypeText(_T(“endofthestory!
“));wordSelection.HomeKey(COleVariant((short)5),COleVariant((short)1));//wdLine=5,返回当前行首,并选择当前行/*CFont0*/font=wordSelection.get_Font();font.put_Size(20);//必须选择该行才可以修改,即必须有HomeKey那行/*CParagraph*/lastPara=GetCurParagraph(docx);lastPara.put_Alignment
(3);//右对齐wordSelection.EndOf(COleVariant((short)4),COleVariant((short)0));//结束当前段落编辑,wdParagraph=4,wdMove=0wordSelection.TypeParagraph();//新起一段wordSelection.TypeText(_T(“Thanksforreading!
“));wordSelection.HomeKey(COleVariant((short)5),COleVariant((short)1));//wdLine=5,返回当前行首,并选择当前行/*CFont0*/font=wordSelection.get_Font();font.put_Size(10);//必须选择该行才可以修改,即必须有HomeKey那行font.put_Name(_T(“TimesNewRoman“));/*CParagraph*/lastPara=GetCurParagraph(docx);lastPara.put_Alignment
(1);//居中对齐wordSelection.EndOf(COleVariant((short)4),COleVariant((short)0));//结束当前段落编辑,wdParagraph=4,wdMove=0//插入分页符,用于换页wordSelection.InsertBreak(covOptional);//插入公式,操作域CFieldsfields=wordSelection.get_Fields();COleVariantofont=_variant_t(_T(“TimesNewRoman“));COleVarianttext=_variant_t(_T(“EQ\\a\\ar\\co2\\vs3\\hs3(Axy,Bxy,A,B)“));//注意要两个\\,一个转义后不对!
!
!
fields.Add(wordSelection.get_Range(),covOptional,text,covFalse);wordSelection.HomeKey(COleVariant((short)5),COleVariant((short)1));//wdLine=5,返回当前行首,并选择当前行lastPara=GetCurParagraph(docx);lastPara.put_Alignment(0);//左对齐wordSelection.EndOf(COleVariant((short)4),COleVariant((short)0));//结束当前段落编辑,wdParagraph=4,wdMove=0//获取应用当前Debug路径charfileName[MAX_PATH];GetModuleFileName(NULL,fileName,MAX_PATH);chardir[260];chardirver[100];_splitpath(fileName,dirver,dir,NULL,NULL);CStringstrAppPath=dirver;strAppPath+=dir;//CStringstrAppPath=_T(“D:
\\“);//******************插入图片示例******************//需要CWindow0.h,CPane0.h,CView0.hwordSelection.TypeParagraph();//另起一段CStringstrPicture=strAppPath+_T(“\\截图.jpg“);CnlineShapesnLineShapes=wordSelection.get_InlineShapes();CnlineShapenLineshape=nLineShapes.AddPicture(strPicture,covFalse,covTrue,covOptional);//******************设置页眉页脚******************CWindow0oWind=docx.get_ActiveWindow();CPane0oPane=oWind.get_ActivePane();//一定将CPane改为CPane0或其他CView0oView=oPane.get_View();//===============设置页眉===============oView.put_SeekView(9);//wdSeekCurrentPageHeader=9/*CFont0*/font=wordSelection.get_Font();//设置选择区域字体font.put_Name(_T(“华文楷体“));font.put_Size(16);/*CParagraphFormat*/lastPara=wordSelection.get_ParagraphFormat();//默认为居中lastPara.put_Alignment
(1);//wdAlignParagraphLeft=0,wdAlignParagraphCenter=1,wdAlignParagraphRight=2wordSelection.TypeText(_T(“网络大学“));//===============设置页脚,包括页码===============oView.put_SeekView(10);//wdSeekCurrentPageFooter=10/*CFont0*/font=wordSelection.get_Font();//设置选择区域字体,一定要放在样式后,否则格式会被样式的覆盖font.put_Name(_T(“华文楷体“));font.put_Size(16);/*CParagraphFormat*/lastPara=wordSelection.get_ParagraphFormat();//默认为居中lastPara.put_Alignment
(1);//wdAlignParagraphLeft=0,wdAlignParagraphCenter=1,wdAlignParagraphRight=2//添加页码wordSelection.TypeText(_T(“第页共页“));wordSelection.MoveLeft(COleVariant((short)1),COleVariant((short)4),/*CFields*/fields=wordSelection.get_Fields();fields.Add(wordSelection.get_Range(),COleVariant((short)33),COleVariant(“PAGE“),//增加页码域,当前页码wordSelection.MoveRight(COleVariant((short)1),COleVariant((short)3),fields.Add(wordSelection.get_Range(),COleVariant((short)26),COleVariant(“NUMPAGES“),//增加页码域,总页数oView.put_SeekView(0);//关闭页眉页脚,wdSeekMainDocument=0,回到主控文档//Word程序可见,显示报表//wordApp.put_Visible(TRUE);//保存成果CStringstrSavePath=strAppPath;strSavePath+=_T(“\\报表.docx“);docx.SaveAs(COleVariant(strSavePath),covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional);//退出word应用docx.Close(covFalse,covOptional,covOptional);wordApp.Quit(covOptional,covOptional,covOptional);wordApp.ReleaseDispatch();MessageBox(_T(“生成成功!
“));}}表格插入函数MakeRTITable(CDocument0nRowSize=nRowSize==0?
2:
nRowSize;//表格至少两行//newparagraphselection.TypeParagraph();//新起一段//Addtabletitleselection.TypeParagraph();//新起一段selection.TypeText(RTITableTitle);CParagraphlastPara=GetCurParagraph(oDoc);//lastPara.put_Alignment(wdAlignParagraphCenter);//下面表格内容也受此控制//selection.EndOf(COleVariant((short)wdParagraph),COleVariant((short)wdMove));lastPara.put_Alignment
(1);//下面表格内容也受此控制selection.EndOf(COleVariant((short)4),COleVariant((short)0));//AddtableCTables0wordTables=oDoc.get_Tables();CRangewordRange=selection.get_Range();COleVariantcovTrue((short)TRUE),covFalse((short)FALSE),covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);CTable0wordTable=wordTables.Add(wordRange,nRowSize,RTITableColumnSize,covTrue,covFalse);//添加表格wordRange=wordTable.get_Range();//MakeheaderCCellcell;for(inti=1;i {cell=wordTable.Cell(1,i);//表格第一行第i列单元格cell.Select();selection.TypeText(RTITableFieldArray[i-1]);}//selection.EndOf(COleVariant((short)wdStory),COleVariant((short)wdMove));//结束表格编辑selection.EndOf(COleVariant((short)6),COleVariant((short)0));//结束表格编辑for(inti=1;i {cell=wordTable.Cell(2,i);//表格第一行第i列单元格cell.Select();selection.TypeText(_T(“111.0“));}//selection.EndOf(COleVariant((short)wdStory),COleVariant((short)wdMove));//结束表格编辑selection.EndOf(COleVariant((short)6),COleVariant((short)0));//结束表格编辑//合并单元格,需要注意的是,合并整行前不能有单元格的合并,否则无法获取表格的行信息CRowsrows;rows=wordTable.get_Rows();//获取表格的行CRowrow;row=rows.Item (3);//指向第三行wordRange=row.get_Range();CCellscells;cells=wordRange.get_Cells();//得到该行所有单元格cells.Merge();//合并第三行为一列cell=wordTable.Cell(4,1);cell.Merge(wordTable.Cell(5,1) );//合并第一列中的第四行与第五行//光标的移动方式.通过Selection类对象的方法对光标进行上下,左右等移动,实现对光标的定位功能selection.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0) );//向右移动鼠标到下一个字符selection.MoveDown(COleVariant((short)5),covOptional,covOptional);//向下移动鼠标到下一行COleVariantvUnit((short)RTITableColumnSize);//光标移动方式为行COleVariantvCount((short)3);selection.Move(//移动3行cells.Merge();//合并第三行为一列COleVariantvEnd((short)5);selection.EndKey(//将光标移动到行尾}自定义函数GetCurParagraph(CDocument0CParagraphlastPara=paragraphs.get_Last();returnlastPara;} 7.生成文档结果 8.说明 (1)关于设置页边距时的参数说明以缇为单位,一个与屏幕无关的单位,用来保证屏幕应用程序对屏幕元素的定位和比例在所有的显示系统上的一致性。 一缇等价于1/20个打印机的磅。 一逻辑英寸大约有1440缇,一逻辑厘米约567缇(打印时的一英寸或一厘米所对应的屏幕上的长度)。 但实际word文档中显示和打印效果来看,56.7缇=2cm,10缇=0.35cm。 (2)加载CPane.h文件前,将CPane改为CPane0,否则会出现重定义,还有一个afxpane.h中有CPane类(3)生成的word文档,如果页码显示和公式显示为代码形式,一个可以直接全选右键“切换域代码”,对于页码则选中页脚同样操作即可;另一个可以设置word文档的格式,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VC 操作Word 操作 Word