当安装了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事件中写入代码:
上面的代码中的LitwareControlLibrary.LitwarePane就是添加的Windows Control Library的工程中的控件。
然后在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
2
3
4
5
6
7
8
上面的代码中的LitwareControlLibrary.LitwarePane就是添加的Windows Control Library的工程中的控件。
1
Imports word = Microsoft.Office.Interop.Word
2
Public 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
34
End Class
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35