private void ThisDocument_Startup(object sender, System.EventArgs e) { Word.Range P_Range1 = this.Paragraphs[1].Range; //得到文档范围 Microsoft.Office.Tools.Word.Controls.Button P_btn = //向文档中添加按钮 this.Controls.AddButton(P_Range1, 50, 20, "button1"); P_btn.Text = "Button按钮"; //设置按钮文本 P_btn.Height = 50; //设置按钮高度 P_btn.Width = 100; //设置按钮宽度 P_btn.Click += new EventHandler(P_btn_Click); //添加单击事件 }
private void ThisDocument_Startup(object sender, System.EventArgs e) { Word.Range P_Range1 = this.Paragraphs[1].Range; //得到文件檔範圍 Microsoft.Office.Tools.Word.Controls.Button P_btn = //向文件檔中新增按鈕 this.Controls.AddButton(P_Range1, 50, 20, "button1"); P_btn.Text = "Button按鈕"; //設定按鈕內容 P_btn.Height = 50; //設定按鈕高度 P_btn.Width = 100; //設定按鈕寬度 P_btn.Click += new EventHandler(P_btn_Click); //新增按一下事件 }
// </Snippet1> private void ThisAddIn_Startup(object sender, System.EventArgs e) { // <Snippet2> this.adventureWorksDataSet = new AdventureWorksLTDataSet(); this.customerTableAdapter = new AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter(); this.customerTableAdapter.Fill(this.adventureWorksDataSet.Customer); this.customerBindingSource = new System.Windows.Forms.BindingSource(); // </Snippet2> // <Snippet3> Word.Document currentDocument = this.Application.ActiveDocument; Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument); // </Snippet3> // <Snippet4> extendedDocument.Paragraphs[1].Range.InsertParagraphBefore(); extendedDocument.Paragraphs[1].Range.InsertParagraphBefore(); extendedDocument.Paragraphs[1].Range.Text = "The companies listed in the AdventureWorksLT database: \n"; extendedDocument.Paragraphs[2].Range.Text = " "; Word.Range range1 = extendedDocument.Paragraphs[2].Range.Characters.First; Word.Range range2 = extendedDocument.Paragraphs[2].Range.Characters.Last; Word.Range range3 = extendedDocument.Paragraphs[1].Range.Characters.Last; // </Snippet4> // <Snippet5> this.button1 = extendedDocument.Controls.AddButton(range1, 60, 15, "1"); this.button1.Text = "Previous"; this.button2 = extendedDocument.Controls.AddButton(range2, 60, 15, "2"); this.button2.Text = "Next"; this.customerContentControl = extendedDocument.Controls.AddRichTextContentControl( range3, "richTextContentControl1"); // </Snippet5> // <Snippet6> this.customerBindingSource.DataSource = this.adventureWorksDataSet.Customer; this.customerContentControl.DataBindings.Add("Text", this.customerBindingSource, "CompanyName", true, this.customerContentControl.DataBindings.DefaultDataSourceUpdateMode); this.button1.Click += new EventHandler(button1_Click); this.button2.Click += new EventHandler(button2_Click); // </Snippet6> }
internal void ToggleButtonOnDocument() { Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument); string name = "MyButton"; if (Globals.Ribbons.MyRibbon.addButtonCheckBox.Checked) { Word.Selection selection = this.Application.Selection; if (selection != null && selection.Range != null) { button = vstoDocument.Controls.AddButton( selection.Range, 100, 30, name); } } else { vstoDocument.Controls.Remove(name); } }