ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:26KB ,
资源ID:17248268      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/17248268.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(VSTO实战技巧Word文档下载推荐.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

VSTO实战技巧Word文档下载推荐.docx

1、若要开发使用 Microsoft Office Word 的解决方案,可以与 Word 对象模型提供的对象进行交互。Word 对象是按层次顺序排列的,层次结构顶端的两个主类是 Application 和 Document 类。这两个类非常重要,原因是在大部分时间里,您要么是在使用 Word 应用程序本身,要么是以某种方式处理 Word 文档。Word 对象模型严格遵循用户界面。Application 对象表示整个应用程序,每个 Document 对象表示单个 Word 文档,Paragraph 对象对应于单个段,以此类推。这些对象各自都有很多方法和属性,您可以使用这些方法和属性操作对象或与对象

2、交互。Application 对象Application 对象表示 Word 应用程序,是其他所有对象的父级。它的所有成员通常作为一个整体应用于 Word。可以使用该对象的属性和方法来控制 Word 环境。Document 对象Microsoft.Office.Interop.Word.Document 对象是 Word 编程的中枢。当您打开文档或创建新文档时,就创建了新的 Microsoft.Office.Interop.Word.Document 对象,该对象被添加到 Word 的 Documents 集合中。焦点所在的文档叫做活动文档,由 Application 对象的 ActiveDo

3、cument 属性表示。可以使用 this 对象引用来访问 ThisDocument 的成员。Selection 对象Selection 对象表示当前选择的区域。如果未选中任何对象,它表示插入点。此外,它也可以是不连续的多个文本块。使用this.Sections可获取当前文档的所有节。Range 对象Range 对象表示文档中的一个连续的区域,由一个起始字符位置和一个结束字符位置定义。Range 对象的数量并不局限于一个。您可以在同一文档中定义多个 Range 对象。Range 对象具有下面的特性: 它的组成成分可以是单独的插入点,也可以是一个文本范围或整个文档。 它包含非打印字符,例如空格、

4、制表符和段落标记。 它可以是当前选择所表示的区域,也可以表示当前选择之外的区域。 与所选内容总是可见不同,它在文档中是不可见的。 它不随文档保存,仅存在于代码运行期间。在向一个范围的末尾插入文本时,Word 会自动扩展该范围以包含插入的文本。一、如何控制当前光标1、获取和设置光标位置 Application.Selection.Start Application.Selection.End 当在计算或者使用程序初始化Word时,随时获取当前光标位置,可用Application.Selection.Start获取。2、判断是否选取文字 Application.Selection.Text 表示W

5、ord中,是否有被选中的反显的文字。 Application.Selection.Text. Length,表示选中长度。3、将光标移动/回到首行(ctrl+Home)object wdUnit = WdUnits.wdStory;object wdExent = WdMovementType.wdMove;this.Application.Selection.HomeKey(ref wdUnit, ref wdExent);/定位书签object what = WdGoToItem.wdGoToBookmark;bookName = end;ThisApplication.Selection

6、.GoTo(ref what, ref missing, ref missing, ref bookName);二、如何获取和设置Range1、如何构建一个rangeobject start = null;object end = null;Range rng = Range(ref start,ref end);2、为Selection设置Rangeint start = 20;int end = 40;this.Application.Selection.SetRange(start,end);this.Application.Selection.Range.Select();3、获取对象

7、的range如:xmlNodebookmark 表格对象tempTable.Cell(1, 2).Range.Start;都有range属性。可根据此,进行操作。三、VSTO的bookmark首先命名空间不一致,Microsoft.Office.Tools.Word.BookmarkMicrosoft.Office.Interop.Word.Bookmark添加一个VSTO bookmark常用方法如下:/定义区域起止位置/获取当前光标开始位置start = this.Application.Selection.Start;/插入“根据公式:this.Application.Selection

8、.TypeText(根据公式:);/回车换行this.Application.Selection.TypeParagraph();/获取结束位置end = this.Application.Selection.End;/根据区域创建bookmarktempbook = this.Controls.AddBookmark(Range(ref start, ref end),”bookname”);此种方法较为灵活和稳定。所有经vsto封装的空间,均派生至 Microsoft.office.tools下。Vsto封装的bookmark,除了具有普通bookmark的特性外,还有一个新增功能就是,封

9、装了很多可以订阅的事件。BeforeDoubleClick -双击BeforeRightClick -显示右键菜单Deselected -失去选择Selected -选中时四、如何定义菜单Office.CommandBarPopup cmdbar = null;Office.CommandBarButton menuInsertGraphics;private void AddMenuBar() try Office.CommandBarPopup cmdBarControl = null; /获取当前word窗口的菜单 Office.CommandBar menubar = (Office.

10、CommandBar)Application.CommandBars.ActiveMenuBar;int controlCount = menubar.Controls.Count;string menuCaption = 作业规程(&Z)/ Add the menu. cmdBarControl = (Office.CommandBarPopup)menubar.Controls.Add( Office.MsoControlType.msoControlPopup, missing, missing, controlCount, true); if (cmdBarControl != nul

11、l) 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

12、.FaceId = 43; menuCapterBtn.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuCapterBtn_Click); / 添加按钮/ 图形 cmdbar = (Office.CommandBarPopup)cmdBarControl.Controls.Add( Office.MsoControlType.msoControlPopup, missing, missing, missing, true); cmdbar.Tag = menuGraphics cm

13、dbar.Caption = 插入图形 /图形处理工具插 menuGraphicsBtn = (Office.CommandBarButton)cmdbar.Controls.Add( menuGraphicsBtn.Caption = 从图形处理工具(&G) menuGraphicsBtn.Tag = menuGraphicsBtn.FaceId = 53; menuGraphicsBtn.Enabled = false; menuGraphicsBtn.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEvent

14、Handler(menuGraphicsBtn_Click); catch (Exception e) MessageBox.Show(e.Message);如上述代码所示,向Word菜单中,增加自定义项。一共分下列几步: 获取当前文档的menu 将自定义的Office.CommandBarPopup插入到当前菜单 将自定义的Office.CommandBarButton 插入到自定义菜单项中 如果需要定义popu菜单,先定义二级CommandBarPopup,并将它插入自定义菜单项并,为其添加子CommandBarButton 注意:无法自定义按钮的图标。其图标是通过FaceId来实现的(详

15、细参见officeIco)button的tag不要重复同时为button指定响应事件cmdBarControl.Tag可以作为自定义菜单是否存在的依据。所以很重要五、如何设置工具栏/当前toolbarOffice.CommandBar commandToolbar;/章节结构Office.CommandBarButton tlCapterBtn;/添加工具栏private void AddToolBar() commandToolbar = this.Application.CommandBarsZygcToolBar; catchif (commandToolbar = null) comm

16、andToolbar = 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 = Of

17、fice.MsoButtonStyle.msoButtonIconAndCaption; tlCapterBtn.Tag = toolCapter tlCapterBtn.FaceId = 43; tlCapterBtn.Caption = 章节结构 tlCapterBtn.Visible = true; tlCapterBtn.Click += new _CommandBarButtonEvents_ClickEventHandler(menuCapterBtn_Click); /插入公式如上述所示: 根据commandtoolbar的tag属性,判断自定义toolbar是否存在 如果没有就

18、创建 将自定义button添加进去 同菜单一样,ico只能通过FaceId来确定 需要同时指定响应事件六、如何设置自己的popuMenuOffice.CommandBar menuFormular; /公式菜单Office.CommandBarButton addParment;/添加参数Office.CommandBarButton repariFormular;/解析公式/ / 生成pop菜单/summaryprivate void RepairMenu() /声明popu菜单 menuFormular = ThisApplication.CommandBars.Add(MenuFormu

19、lar, 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

20、_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; repa

21、riFormular.Click += new _CommandBarButtonEvents_ClickEventHandler(repariFormular_Click);如上例所示: 需要操作的是applcation级的CommandBars对象 需要指定必须的参数为名字、式样Office.MsoBarPosition.msoBarPopup 菜单项的ICO也是要通过FaceId来获取 添加自定义popu菜单的子项时,就同时为其指定响应事件七、如何通过vsto设置和使用style1、关于style的几个概念 Word里的格式和式样。 格式和式样属于文档级。 如果要达到某种新效果,需要先在

22、文档内新建style,才能应用 Style可以分级别,最高支持9级2、如何获取styleStyle可通过一个段落或者一个区域获取。如下边的代码:/获取区域范围object start = tBk.Range.Start + 2;object end = tBk.Range.End - 2;/声明中间类PersonalmStyle mstyle = new PersonalmStyle();/分析区域内style,并将关键值记录mstyle.FontName = Range(ref start, ref end).Font.Name;mstyle.FontSize = Range(ref sta

23、rt, ref end).Font.Size;mstyle.WDColor = Range(ref start, ref end).Font.Color;mstyle.Bold = Range(ref start, ref end).Font.Bold;mstyle.Shadow = Range(ref start, ref end).Font.Shadow;mstyle.Aligment = Range(ref start, ref end).ParagraphFormat.Alignment;mstyle.UnitLeftIndent = Range(ref start, refend).

24、ParagraphFormat.CharacterUnitLeftIndent;mstyle.UnitRightIndent = Range(ref start, ref end).ParagraphFormat.CharacterUnitRightIndent;mstyle.UnitFirstLineIndent = Range(ref start, refend).ParagraphFormat.CharacterUnitFirstLineIndent;mstyle.LineSpaceingRule = Range(ref start, ref end).ParagraphFormat.L

25、ineSpacingRule;mstyle.LineUnitAfter = Range(ref start, ref end).ParagraphFormat.LineUnitAfter;mstyle.LineUnitBefore = Range(ref start, ref end).ParagraphFormat.LineUnitBefore;mstyle.OutLineLevel = Range(ref start, ref end).ParagraphFormat.OutlineLevel;记录一个style最关键有2部分:Font字体属性:字体名称、字号、颜色、粗体、阴影Paragr

26、aphFormat段落属性:对齐方式、左右缩进、段前段后间距、段落级别3、如何应用style(1)构造一个style(2)将style添加到ActiveDocument.Styles中Application.ActiveDocument.Styles.Add(styleName, ref styleType);(3)选择需要应用的区域,并设置styleobject tt = styleName;if (cstyle ! for (int styleIndex = 1; styleIndex = ThisApplication.ActiveDocument.Styles.Count; style

27、Index+)/根据style的名字遍历,当前doc的styles object aaa = styleIndex; if (ThisApplication.ActiveDocument.Styles.get_Item(ref aaa).NameLocal = tt.ToString()/如果存在该style rng.set_Style(ref tt);/选择区域内容,应用该格式(4)还有一种应用style的方式,那就是将style直接设置给Selection。Selection在随后产生的内容,全部遵守style的约定this.Application.Selection.set_Style(

28、ref tt);八、如何打开和关闭文档结构图如果需要通过文档结构图来查看文档结构,那么前提条件是,文档必须按大纲视图制定,并且有一定的等级结构。可通过下边一个属性来激活文档结构图ThisApplication.ActiveWindow.DocumentMap = true;九、表格操作1、便历所有单元格object start=this.Application.Selection.Start;Word.Table t = this.Application.ActiveDocument.Tables.Add(Range(ref start, ref start), 4, 6, ref missing, ref missing);for (int intR = 1; intR t.Rows.Count + 1; intR+) for (int intC = 1; intC t.Columns.Count + 1

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1