示例代码以将控件添加到窗体
下面的 Microsoft Outlook Visual Basic for Applications 代码演示了如何您可以以编程方式将一个 标签 和 列表框 控件添加到默认的联系人窗体的新页面。 一定要运行该代码之前,引用 Microsoft Forms 2.0 对象库。Sub CreateForm()
Dim oNewItem As Outlook.ContactItem
Dim oInsp As Outlook.Inspector
Dim oPage As Object
Dim oLabel As MSForms.Control
Dim oLB As MSForms.Control
Set oNewItem = Application.CreateItem(olContactItem)
Set oInsp = oNewItem.GetInspector
' Create a new form page called "My Page"
Set oPage = oInsp.ModifiedFormPages.Add("My Page")
' Add the label to the form page
Set oLabel = oPage.Controls.Add("Forms.Label.1", "lblCompanies", True)
' Size and position the label
PlaceControl oLabel, 5, 10, 13, 100
' Set the caption of the label
oLabel.Caption = "Companies:"
' Add the listbox to the form page
Set oLB = oPage.Controls.Add("Forms.ListBox.1", "cmdCompanies", True)
' Size and position the listbox
PlaceControl oLB, 18, 10, 50, 100
' Populate the listbox
oLB.AddItem "Microsoft"
oLB.AddItem "ACME"
oLB.AddItem "Northwind"
' Change the form page so it defaults to "My Page"
oInsp.SetCurrentFormPage "My Page"
oNewItem.Display
' Clean up
Set oListBox = Nothing
Set oLabel = Nothing
Set oPage = Nothing
Set oInsp = Nothing
Set oNewItem = Nothing
End Sub
Sub PlaceControl(Ctrl As MSForms.Control, T As Integer, _
L As Integer, H As Integer, W As Integer)
Ctrl.Top = T
Ctrl.Left = L
Ctrl.Height = H
Ctrl.Width = W
End Sub
此代码示例只是显示一个新创建的窗体,窗体。 有关如何以编程方式创建和发布一个新的自定义窗体的其他信息,单击下列文章编号以查看 Microsoft 知识库中相应的文章:
208520
OL2000: programming examples for referencing items and folders
请参阅相应的文章中的 Items.Add 和 CreateItemFromTemplate 方法。