VSTO实战技巧Word文档下载推荐.docx
《VSTO实战技巧Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《VSTO实战技巧Word文档下载推荐.docx(17页珍藏版)》请在冰豆网上搜索。
若要开发使用MicrosoftOfficeWord的解决方案,可以与Word对象模型提供的对象进行交互。
Word对象是按层次顺序排列的,层次结构顶端的两个主类是Application和Document类。
这两个类非常重要,原因是在大部分时间里,您要么是在使用Word应用程序本身,要么是以某种方式处理Word文档。
Word对象模型严格遵循用户界面。
Application对象表示整个应用程序,每个Document对象表示单个Word文档,Paragraph对象对应于单个段,以此类推。
这些对象各自都有很多方法和属性,您可以使用这些方法和属性操作对象或与对象交互。
Application对象
Application对象表示Word应用程序,是其他所有对象的父级。
它的所有成员通常作为一个整体应用于Word。
可以使用该对象的属性和方法来控制Word环境。
Document对象
Microsoft.Office.Interop.Word.Document对象是Word编程的中枢。
当您打开文档或创建新文档时,就创建了新的Microsoft.Office.Interop.Word.Document对象,该对象被添加到Word的Documents集合中。
焦点所在的文档叫做活动文档,由Application对象的ActiveDocument属性表示。
可以使用this对象引用来访问ThisDocument的成员。
Selection对象
Selection对象表示当前选择的区域。
如果未选中任何对象,它表示插入点。
此外,它也可以是不连续的多个文本块。
使用this.Sections可获取当前文档的所有节。
Range对象
Range对象表示文档中的一个连续的区域,由一个起始字符位置和一个结束字符位置定义。
Range对象的数量并不局限于一个。
您可以在同一文档中定义多个Range对象。
Range对象具有下面的特性:
∙它的组成成分可以是单独的插入点,也可以是一个文本范围或整个文档。
∙它包含非打印字符,例如空格、制表符和段落标记。
∙它可以是当前选择所表示的区域,也可以表示当前选择之外的区域。
∙与所选内容总是可见不同,它在文档中是不可见的。
∙它不随文档保存,仅存在于代码运行期间。
在向一个范围的末尾插入文本时,Word会自动扩展该范围以包含插入的文本。
一、如何控制当前光标
1、获取和设置光标位置
Application.Selection.Start
Application.Selection.End
当在计算或者使用程序初始化Word时,随时获取当前光标位置,可用
Application.Selection.Start获取。
2、判断是否选取文字
Application.Selection.Text
表示Word中,是否有被选中的反显的文字。
Application.Selection.Text.Length,表示选中长度。
3、将光标移动
//回到首行(ctrl+Home)
objectwdUnit=WdUnits.wdStory;
objectwdExent=WdMovementType.wdMove;
this.Application.Selection.HomeKey(refwdUnit,refwdExent);
//定位书签
objectwhat=WdGoToItem.wdGoToBookmark;
bookName="
end"
;
ThisApplication.Selection.GoTo(refwhat,refmissing,refmissing,refbookName);
二、如何获取和设置Range
1、如何构建一个range
objectstart=null;
objectend=null;
Rangerng=Range(refstart,refend);
2、为Selection设置Range
intstart=20;
intend=40;
this.Application.Selection.SetRange(start,end);
this.Application.Selection.Range.Select();
3、获取对象的range
如:
xmlNode
bookmark
●表格对象
tempTable.Cell(1,2).Range.Start;
都有range属性。
可根据此,进行操作。
三、VSTO的bookmark
首先命名空间不一致,
Microsoft.Office.Tools.Word.Bookmark
Microsoft.Office.Interop.Word.Bookmark
添加一个VSTObookmark常用方法如下:
//定义区域起止位置
//获取当前光标开始位置
start=this.Application.Selection.Start;
//插入“根据公式:
this.Application.Selection.TypeText("
根据公式:
"
);
//回车换行
this.Application.Selection.TypeParagraph();
//获取结束位置
end=this.Application.Selection.End;
//根据区域创建bookmark
tempbook=this.Controls.AddBookmark(Range(refstart,refend),”bookname”);
此种方法较为灵活和稳定。
所有经vsto封装的空间,均派生至Microsoft.office.tools下。
Vsto封装的bookmark,除了具有普通bookmark的特性外,还有一个新增功能就是,封装了很多可以订阅的事件。
BeforeDoubleClick--双击
BeforeRightClick--显示右键菜单
Deselected--失去选择
Selected--选中时
四、如何定义菜单
Office.CommandBarPopupcmdbar=null;
Office.CommandBarButtonmenuInsertGraphics;
privatevoidAddMenuBar()
{
try
{
Office.CommandBarPopupcmdBarControl=null;
///获取当前word窗口的菜单
Office.CommandBarmenubar=(Office.CommandBar)Application.CommandBars.ActiveMenuBar;
intcontrolCount=menubar.Controls.Count;
stringmenuCaption="
作业规程(&
Z)"
//Addthemenu.
cmdBarControl=(Office.CommandBarPopup)menubar.Controls.Add(
Office.MsoControlType.msoControlPopup,
missing,missing,controlCount,true);
if(cmdBarControl!
=null)
cmdBarControl.Caption=menuCaption;
cmdBarControl.Tag=menuTag;
//添加章节设计按钮
menuCapterBtn=(Office.CommandBarButton)cmdBarControl.Controls.Add(
Office.MsoControlType.msoControlButton,missing,missing,missing,true);
menuCapterBtn.Caption="
章节设计(&
C)"
menuCapterBtn.Tag="
menuCapter"
menuCapterBtn.FaceId=43;
menuCapterBtn.Click+=newMicrosoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuCapterBtn_Click);
//添加按钮
//图形
cmdbar=(Office.CommandBarPopup)cmdBarControl.Controls.Add(
Office.MsoControlType.msoControlPopup,missing,missing,missing,true);
cmdbar.Tag="
menuGraphics"
cmdbar.Caption="
插入图形"
//图形处理工具插
menuGraphicsBtn=(Office.CommandBarButton)cmdbar.Controls.Add(
menuGraphicsBtn.Caption="
从图形处理工具(&
G)"
menuGraphicsBtn.Tag="
menuGraphicsBtn.FaceId=53;
menuGraphicsBtn.Enabled=false;
menuGraphicsBtn.Click+=newMicrosoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuGraphicsBtn_Click);
}
catch(Exceptione)
MessageBox.Show(e.Message);
}
如上述代码所示,向Word菜单中,增加自定义项。
一共分下列几步:
●获取当前文档的menu
●将自定义的Office.CommandBarPopup插入到当前菜单
●将自定义的Office.CommandBarButton插入到自定义菜单项中
●如果需要定义popu菜单,先定义二级CommandBarPopup,并将它插入自定义菜单项
并,为其添加子CommandBarButton
●注意:
无法自定义按钮的图标。
其图标是通过FaceId来实现的(详细参见《officeIco》)
button的tag不要重复
同时为button指定响应事件
cmdBarControl.Tag可以作为自定义菜单是否存在的依据。
所以很重要
五、如何设置工具栏
//当前toolbar
Office.CommandBarcommandToolbar;
//章节结构
Office.CommandBarButtontlCapterBtn;
//添加工具栏
privatevoidAddToolBar()
commandToolbar=this.Application.CommandBars["
ZygcToolBar"
];
catch
if(commandToolbar==null)
commandToolbar=this.Application.CommandBars.Add("
Office.MsoBarPosition.msoBarTop,missing,true);
commandToolbar.Visible=true;
//章节结构
tlCapterBtn=(Office.CommandBarButton)commandToolbar.Controls.Add(Office.MsoControlType.msoControlButton,missing,missing,missing,missing);
tlCapterBtn.Style=Office.MsoButtonStyle.msoButtonIconAndCaption;
tlCapterBtn.Tag="
toolCapter"
tlCapterBtn.FaceId=43;
tlCapterBtn.Caption="
章节结构"
tlCapterBtn.Visible=true;
tlCapterBtn.Click+=new_CommandBarButtonEvents_ClickEventHandler(menuCapterBtn_Click);
//插入公式
如上述所示:
●根据commandtoolbar的tag属性,判断自定义toolbar是否存在
●如果没有就创建
●将自定义button添加进去
●同菜单一样,ico只能通过FaceId来确定
●需要同时指定响应事件
六、如何设置自己的popuMenu
Office.CommandBarmenuFormular;
//公式菜单
Office.CommandBarButtonaddParment;
//添加参数
Office.CommandBarButtonrepariFormular;
//解析公式
///<
summary>
///生成pop菜单
/summary>
privatevoidRepairMenu()
//声明popu菜单
menuFormular=ThisApplication.CommandBars.Add("
MenuFormular"
Office.MsoBarPosition.msoBarPopup,missing,true);
//添加参数
addParment=(Office.CommandBarButton)menuFormular.Controls.Add(1,missing,missing,missing,missing);
addParment.Style=Office.MsoButtonStyle.msoButtonCaption;
addParment.Caption="
添加参数"
addParment.FaceId=137;
addParment.Click+=new_CommandBarButtonEvents_ClickEventHandler(addParment_Click);
//公式解析
repariFormular=(Office.CommandBarButton)menuFormular.Controls.Add(1,missing,missing,missing,missing);
repariFormular.Style=Office.MsoButtonStyle.msoButtonCaption;
repariFormular.Caption="
公式解析"
repariFormular.FaceId=23;
repariFormular.Click+=new_CommandBarButtonEvents_ClickEventHandler(repariFormular_Click);
如上例所示:
●需要操作的是applcation级的CommandBars对象
●需要指定必须的参数为名字、式样Office.MsoBarPosition.msoBarPopup
●菜单项的ICO也是要通过FaceId来获取
●添加自定义popu菜单的子项时,就同时为其指定响应事件
七、如何通过vsto设置和使用style
1、关于style的几个概念
●Word里的格式和式样。
●格式和式样属于文档级。
●如果要达到某种新效果,需要先在文档内新建style,才能应用
●Style可以分级别,最高支持9级
2、如何获取style
Style可通过一个段落或者一个区域获取。
如下边的代码:
//获取区域范围
objectstart=tBk.Range.Start+2;
objectend=tBk.Range.End-2;
//声明中间类
PersonalmStylemstyle=newPersonalmStyle();
//分析区域内style,并将关键值记录
mstyle.FontName=Range(refstart,refend).Font.Name;
mstyle.FontSize=Range(refstart,refend).Font.Size;
mstyle.WDColor=Range(refstart,refend).Font.Color;
mstyle.Bold=Range(refstart,refend).Font.Bold;
mstyle.Shadow=Range(refstart,refend).Font.Shadow;
mstyle.Aligment=Range(refstart,refend).ParagraphFormat.Alignment;
mstyle.UnitLeftIndent=Range(refstart,ref
end).ParagraphFormat.CharacterUnitLeftIndent;
mstyle.UnitRightIndent=Range(refstart,ref
end).ParagraphFormat.CharacterUnitRightIndent;
mstyle.UnitFirstLineIndent=Range(refstart,ref
end).ParagraphFormat.CharacterUnitFirstLineIndent;
mstyle.LineSpaceingRule=Range(refstart,ref
end).ParagraphFormat.LineSpacingRule;
mstyle.LineUnitAfter=Range(refstart,ref
end).ParagraphFormat.LineUnitAfter;
mstyle.LineUnitBefore=Range(refstart,ref
end).ParagraphFormat.LineUnitBefore;
mstyle.OutLineLevel=Range(refstart,refend).ParagraphFormat.OutlineLevel;
记录一个style最关键有2部分:
Font字体属性:
字体名称、字号、颜色、粗体、阴影
ParagraphFormat段落属性:
对齐方式、左右缩进、段前段后间距、段落级别
3、如何应用style
(1)构造一个style
(2)将style添加到ActiveDocument.Styles中
Application.ActiveDocument.Styles.Add(styleName,refstyleType);
(3)选择需要应用的区域,并设置style
objecttt=styleName;
if(cstyle!
for(intstyleIndex=1;
styleIndex<
=ThisApplication.ActiveDocument.Styles.Count;
styleIndex++)//根据style的名字遍历,当前doc的styles
objectaaa=styleIndex;
if(ThisApplication.ActiveDocument.Styles.get_Item(refaaa).NameLocal==tt.ToString())//如果存在该style
rng.set_Style(reftt);
//选择区域内容,应用该格式
(4)还有一种应用style的方式,那就是将style直接设置给Selection。
Selection在随后产生的内容,全部遵守style的约定
this.Application.Selection.set_Style(reftt);
八、如何打开和关闭文档结构图
如果需要通过文档结构图来查看文档结构,那么前提条件是,文档必须按大纲视图制定,并且有一定的等级结构。
可通过下边一个属性来激活文档结构图
ThisApplication.ActiveWindow.DocumentMap=true;
九、表格操作
1、便历所有单元格
objectstart=this.Application.Selection.Start;
Word.Tablet=this.Application.ActiveDocument.Tables.Add(Range(refstart,refstart),4,6,refmissing,refmissing);
for(intintR=1;
intR<
t.Rows.Count+1;
intR++)
for(intintC=1;
intC<
t.Columns.Count+1