technofantasy

博客园 首页 新随笔 联系 订阅 管理
当安装了Office 2007以及VSTO for 2007以后,打开Visual Studio可以看到在新建工程对话框里面增加了“Office 2007 Add-in”的选项。比如要添加Word的面板,选择新建一个Word Add-in。
然后在Solution中添加一个Windows Control Library的工程,这个工程中的控件就是添加到Office中的面板。
在Word Add-in工程的ThisAddin.vb的ThisAddIn_Startup事件中写入代码:
1        '创建面板控件
2        Dim myPane As LitwareControlLibrary.LitwarePane = New LitwareControlLibrary.LitwarePane
3
4        '初始化控件,将Word Aplication对象传递过去
5        myPane.Init(Me.Application)
6        '添加自定义面板
7        Dim CustomPane As Microsoft.Office.Tools.CustomTaskPane = Me.CustomTaskPanes.Add(myPane, "This my word pane")
8        CustomPane.Visible = True

上面的代码中的LitwareControlLibrary.LitwarePane就是添加的Windows Control Library的工程中的控件。
 1Imports word = Microsoft.Office.Interop.Word
 2Public Class LitwarePane
 3    Dim wordapp As word.Application
 4
 5    ''' <summary>
 6    ''' 初始化
 7    ''' </summary>
 8    ''' <param name="app">Word Application对象</param>
 9    ''' <remarks></remarks>
10    Public Sub Init(ByVal app As word.Application)
11        '获得Word Application对象
12        wordapp = app
13        wordapp.Documents.Close(Type.Missing, Type.Missing, Type.Missing)
14
15        '获得模板
16        Dim template As Object = "Customer.dotx"
17        '根据模板新建文档
18        Dim doc As word.Document = wordapp.Documents.Add(template)
19
20        Dim NameBMDate As Object = "Date"
21        Dim NameBMCustomer As Object = "Customer"
22
23        '获得文档中的书签
24        Dim bmDate As word.Bookmark = doc.Bookmarks(NameBMDate)
25        Dim bmCustomer As word.Bookmark = doc.Bookmarks(NameBMCustomer)
26
27        '更改书签中的内容
28        bmDate.Range.Text = Date.Now.ToShortDateString
29        bmCustomer.Range.Text = ContactNameTextBox.Text
30    End Sub

31    Private Sub LitwarePane_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
32        Me.CustomersTableAdapter.Fill(NorthwndDataSet.Customers)
33    End Sub

34End Class

35
posted on 2007-01-02 15:07  陈锐  阅读(1369)  评论(8)    收藏  举报