首页 VSTO实战技巧

VSTO实战技巧

举报
开通vip

VSTO实战技巧目 录 1VSTO基本概念 1Application 对象 1Document 对象 1Selection 对象 1Range 对象 2一、如何控制当前光标 3二、如何获取和设置Range 31、如何构建一个range 32、为Selection设置Range 33、获取对象的range 4三、VSTO的bookmark 5四、如何定义菜单 7五、如何设置工具栏 8六、如何设置自己的popuMenu 9七、如何通过vsto设置和使用style 9...

VSTO实战技巧
目 录 1VSTO基本概念 1Application 对象 1Document 对象 1Selection 对象 1Range 对象 2一、如何控制当前光标 3二、如何获取和设置Range 31、如何构建一个range 32、为Selection设置Range 33、获取对象的range 4三、VSTO的bookmark 5四、如何定义菜单 7五、如何设置工具栏 8六、如何设置自己的popuMenu 9七、如何通过vsto设置和使用style 91、关于style的几个概念 92、如何获取style 93、如何应用style 11八、如何打开和关闭文档结构图 11十、表格操作 111、便历所有单元格 112、修改边框式样、合并单元格 12十一、如何利用xml对文档 内容 财务内部控制制度的内容财务内部控制制度的内容人员招聘与配置的内容项目成本控制的内容消防安全演练内容 进行分割管理 14十二、设置目录 15十三、如何控制自定义面板 151、添加和移除面板 152、关闭面板 153、使用Word中的数据 VSTO基本概念 使用 VSTO 2005 创建的最基本形式的自定义项由两个文件组成:一个 Word 文档或 Excel 工作簿以及一个程序集(Visual Studio 将其编译为 .dll 文件) 若要开发使用 Microsoft Office Word 的解决 方案 气瓶 现场处置方案 .pdf气瓶 现场处置方案 .doc见习基地管理方案.doc关于群访事件的化解方案建筑工地扬尘治理专项方案下载 ,可以与 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 会自动扩展该范围以包含插入的文本。 Application{Doucument {Bookmarks,Range}, Selection { Bookmarks,Document,Range}} 一、如何控制当前光标 1、获取和设置光标位置 Application.Selection.Start Application.Selection.End 当在计算或者使用程序初始化Word时,随时获取当前光标位置,可用 Application.Selection.Start获取。 2、判断是否选取文字 Application.Selection.Text 表示Word中,是否有被选中的反显的文字。 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.GoTo(ref what, ref missing, ref missing, ref bookName); 二、如何获取和设置Range 1、如何构建一个range object start = null; object end = null; Range rng = Range(ref start,ref end); 2、为Selection设置Range int start = 20; int end = 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 添加一个VSTO bookmark常用方法如下: //定义区域起止位置 object start = null; object end = null; //获取当前光标开始位置 start = this.Application.Selection.Start; //插入“根据公式: this.Application.Selection.TypeText("根据公式:"); //回车换行 this.Application.Selection.TypeParagraph(); //获取结束位置 end = this.Application.Selection.End; //根据区域创建bookmark tempbook = this.Controls.AddBookmark(Range(ref start, ref end),”bookname”); 此种方法较为灵活和稳定。 所有经vsto封装的空间,均派生至 Microsoft.office.tools下。 Vsto封装的bookmark,除了具有普通bookmark的特性外,还有一个新增功能就是,封装了很多可以订阅的事件。 BeforeDoubleClick --双击 BeforeRightClick --显示右键菜单 Deselected --失去选择 Selected --选中时 四、如何定义菜单 Office.CommandBarPopup cmdbar = null; Office.CommandBarButton menuInsertGraphics; private void AddMenuBar() { try { Office.CommandBarPopup cmdBarControl = null; ///获取当前word窗口的菜单 Office.CommandBar menubar = (Office.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 != 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 += new Microsoft.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( Office.MsoControlType.msoControlButton, missing, missing, missing, true); menuGraphicsBtn.Caption = "从图形处理工具(&G)"; menuGraphicsBtn.Tag = "menuGraphics"; menuGraphicsBtn.FaceId = 53; menuGraphicsBtn.Enabled = false; menuGraphicsBtn.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuGraphicsBtn_Click); } } catch (Exception e) { MessageBox.Show(e.Message); } } 如上述代码所示,向Word菜单中,增加自定义项。一共分下列几步: · 获取当前文档的menu · 将自定义的Office.CommandBarPopup插入到当前菜单 · 将自定义的Office.CommandBarButton 插入到自定义菜单项中 · 如果需要定义popu菜单,先定义二级CommandBarPopup,并将它插入自定义菜单项 并,为其添加子CommandBarButton · 注意:无法自定义按钮的图标。其图标是通过FaceId来实现的(详细参见《officeIco》) · 注意:button的tag不要重复 · 注意:同时为button指定响应事件 · 注意:cmdBarControl.Tag可以作为自定义菜单是否存在的依据。所以很重要 五、如何设置工具栏 //当前toolbar Office.CommandBar commandToolbar; //章节结构 Office.CommandBarButton tlCapterBtn; //添加工具栏 private void AddToolBar() { try { commandToolbar = this.Application.CommandBars["ZygcToolBar"]; } catch { } if (commandToolbar == null) { commandToolbar = this.Application.CommandBars.Add("ZygcToolBar", 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 Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuCapterBtn_Click); //插入公式 } 如上述所示: · 根据commandtoolbar的tag属性,判断自定义toolbar是否存在 · 如果没有就创建 · 将自定义button添加进去 · 同菜单一样,ico只能通过FaceId来确定 · 需要同时指定响应事件 六、如何设置自己的popuMenu Office.CommandBar menuFormular; //公式菜单 Office.CommandBarButton addParment;//添加参数 Office.CommandBarButton repariFormular;//解析公式 /// /// 生成pop菜单 /// private void RepairMenu() { //声明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 Microsoft.Office.Core._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 Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(repariFormular_Click); } 如上例所示: · 需要操作的是applcation级的CommandBars对象 · 需要指定必须的参数为名字、式样Office.MsoBarPosition.msoBarPopup · 菜单项的ICO也是要通过FaceId来获取 · 添加自定义popu菜单的子项时,就同时为其指定响应事件 七、如何通过vsto设置和使用style 1、关于style的几个概念 · Word里的格式和式样。 · 格式和式样属于文档级。 · 如果要达到某种新效果,需要先在文档内新建style,才能应用 · Style可以分级别,最高支持9级 2、如何获取style Style可通过一个段落或者一个区域获取。 如下边的代码: //获取区域范围 object start = tBk.Range.Start + 2; object end = tBk.Range.End - 2; //声明中间类 PersonalmStyle mstyle = new PersonalmStyle(); // 分析 定性数据统计分析pdf销售业绩分析模板建筑结构震害分析销售进度分析表京东商城竞争战略分析 区域内style,并将关键值 记录 混凝土 养护记录下载土方回填监理旁站记录免费下载集备记录下载集备记录下载集备记录下载 mstyle.FontName = Range(ref start, ref end).Font.Name; mstyle.FontSize = Range(ref start, 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, ref end).ParagraphFormat.CharacterUnitLeftIndent; mstyle.UnitRightIndent = Range(ref start, ref end).ParagraphFormat.CharacterUnitRightIndent; mstyle.UnitFirstLineIndent = Range(ref start, ref end).ParagraphFormat.CharacterUnitFirstLineIndent; mstyle.LineSpaceingRule = Range(ref start, ref end).ParagraphFormat.LineSpacingRule; 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字体属性: 字体名称、字号、颜色、粗体、阴影 ParagraphFormat段落属性: 对齐方式、左右缩进、段前段后间距、段落级别 3、如何应用style (1)构造一个style (2)将style添加到ActiveDocument.Styles中 Application.ActiveDocument.Styles.Add(styleName, ref styleType); (3)选择需要应用的区域,并设置style object tt = styleName; if (cstyle != null) { for (int styleIndex = 1; styleIndex <= ThisApplication.ActiveDocument.Styles.Count; styleIndex++)//根据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(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; intC++) { t.Cell(intR, intC).Range.Text = "第" + intR.ToString() + "行,第" + intC.ToString() + "列"; } } 如上例所示: · 表格行列数索引为从1开始 · 确定为一单元格需要使用cell对象。格式为Cell(行,列) · 单元格有Range属性,可通过range.text对其进行赋值 2、修改边框式样、合并单元格 对多个单元格进行操作时,都需要用到Cells对象。所以,要先为Selection创造一个cells对象。 object start = t.Cell(1, 2).Range.Start; object end = t.Cell(4, 3).Range.End; Word.Range rng = Range(ref start, ref end); rng.Select(); 当一个或多个单元格的区域被选中时,Selection.Cells对象将不再为空。 合并单元格可通过: this.Application.Selection.Cells.Merge(); 修改边框可通过: this.Application.Selection.Cells.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleDouble; this.Application.Selection.Cells.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineWidth = Word.WdLineWidth.wdLineWidth050pt; 注意:当进行合并操作时,行列索引会发生改变。所以,要从后往前进行合并操作。 十、如何利用xml对文档内容进行分割管理 1、什么是XmlSchema XML Schema是负责定义和描述XML文档的结构和内容模式。它可以定义XML文档中存在哪些元素和元素之间的关系,并且可以定义元素和属性的数据类型。 XML Schema本身是一个XML文档,它符合XML语法结构。可以用通用的XML解析器解析它。 如果把带有数据的xml文件看成一个二维表,那么xmlSchema就可以理解为,这个数据表的表结构。那可以为这个表的每个字段指定类型、大小、约束。只是xmlSchema比我的举例更复杂一些,它对数据类型的支持不仅仅是简单的: string,boolean,decimal,float,double,duration,datetime,time,date,gYearMonth,gYear,gMonthDay, dDay,gMonth,hexBinary,base64Binary,any URI 还支持一些更复杂的自定义类型: sequence 在其定义范围之内的所有元素都必须按顺序出现,范围由minOccurs和 maxOccurs指定。 choice 其范围内有且只有一个元素必须出现。 any 定义的任何元素都必须出现。 simpleContent 这种复杂类型只包含了非嵌套元素。可以通过包含扩展元素的方式扩展先前定义的简单类型。 complexContent 这种复杂类型只能包含其他元素。可以通过包含扩展元素的方式扩展先前定义的复杂类型。 attribute 这种复杂类型只能包含命名属性。 2、如何利用xmlNode划分Word文档 (1)为Word添加schema,用以限定XmlNode内数据的类型 private void CheckNameSpaceIsAdd() { bool isAdd = false; object mAlias = "zj"; nameS = "http://jingchengsoft.zygc.zjjg.xml"; try { this.Application.ActiveDocument.XMLSchemaReferences. AutomaticValidation = true; this.Application.ActiveDocument.XMLSchemaReferences. AllowSaveAsXMLWithoutValidation = false; foreach (XMLNamespace xName in this.Application.XMLNamespaces) { if (xName.URI.ToString().ToLower() == nameS.ToString().ToLower()) { xName.Delete(); } } } finally { wordxml = this.Application.XMLNamespaces. Add(Path + "\\zt.xml", ref nameS, ref mAlias, true); mWord = this.Application.ActiveDocument; wordxml.AttachToDocument(ref mWord); } } (2)将schema内所声明的Node类型,添加到Word中 XMLNode parNode = this.Application.ActiveDocument.XMLNodes.Add("gcElement", nameS.ToString(), ref rng); (3)Vsto关于xml操作的限制 Vsto提供的添加xmlNode的方法均为在设计时,不能再运行时,通过代码添加。 通过ActiveDocument.XMLNodes.Add()方法添加的xmlNode,均为Microsoft.Office.Interop.Word类型。 不是Microsoft.Office.Tools.Word.XMLNode类型。 只有Microsoft.Office.Tools.Word.XMLNode类型的xmlNode才有响应事件的能力。 十一、设置目录 //分页符 object pageBreak = WdBreakType.wdPageBreak; //插入下一节 object pageBreakNext = WdBreakType.wdSectionBreakNextPage; this.Application.Selection.InsertBreak(ref pageBreakNext); 根据章节设置的需要,插入分页符和分节符。 //目录 Word.TableOfContents tempTable = null; tempTable = ThisApplication.ActiveDocument.TablesOfContents.Add(Range(ref start, ref start), ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); tempTable.TabLeader = WdTabLeader.wdTabLeaderDots; ThisApplication.ActiveDocument.TablesOfContents.Format = WdTocFormat.wdTOCFormal; tempTable.Update(); 十二、如何控制自定义面板 1、添加和移除面板 this.ActionsPane.Controls.Clear(); if (cpaterEditor == null)//面板没有创建 { cpaterEditor = new CapterEditor();//创建面板 //订阅更新事件 cpaterEditor.onRequestRefushCapter += new EventHandler(cpaterEditor_onRequestRefushCapter); this.ActionsPane.Controls.Add(cpaterEditor); cpaterEditor.Show(); } else//如果已经有了 { //调出来 this.Application.TaskPanes[WdTaskPanes.wdTaskPaneDocumentActions] .Visible = true; //this.ActionsPane.Controls[cpaterEditor.Name].Show(); this.ActionsPane.Controls.Add(cpaterEditor); } 2、关闭面板 //关闭task pane object indx = "Task Pane"; Globals.ThisDocument.CommandBars[indx].Visible = false; 3、使用Word中的数据 Globals.ThisDocument.变量 PAGE 7
本文档为【VSTO实战技巧】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_573671
暂无简介~
格式:doc
大小:128KB
软件:Word
页数:16
分类:互联网
上传时间:2012-04-13
浏览量:165