1: Public Class UIcontrols
2:
3: #Region "Declarations"
4:
5: 'Others Removed for Remove for brevity
6:
7: Private adoc As TheDocument = New TheDocument()
8: 'menu bar declarations
9: Private WithEvents menuCommand As Office.CommandBarButton
10: Private menuTag As String = "Air Stories Helper Program"
11: Private menuID As Object = "AirStories"
12:
13: #End Region
14:
15: #Region "properties"
16: Private thisapp As Word.Application
17: Public Property ThisApplication() As Word.Application
18: Get
19: Return thisapp
20: End Get
21: Set(ByVal value As Word.Application)
22: thisapp = value
23: End Set
24: End Property
25: #End Region
26:
27: #Region "Public Methods"
28: 'loads the controls on the toolbar
29: Public Sub LoadControls()
30:
31: 'apply menu bars
32: Me.CheckIfMenuBarExists()
33: Me.AddMenuBar()
34:
35: End Sub
36:
37: ' If the menu already exists, remove it.
38: Private Sub CheckIfMenuBarExists()
39: Try
40: Dim foundMenu As Office.CommandBarPopup = _
41: thisapp.Application.CommandBars.ActiveMenuBar.FindControl( _
42: Office.MsoControlType.msoControlPopup, Tag:=menuTag, Visible:=True, Recursive:=True)
43: If foundMenu IsNot Nothing Then
44: foundMenu.Delete(True)
45: End If
46:
47: Catch ex As Exception
48: MessageBox.Show(ex.Message)
49: End Try
50: End Sub
51:
52: ' Create the menu,
53: Private Sub AddMenuBar()
54:
55: Try
56: Dim menuBar As Office.CommandBar = WordAddIn1.Globals.ThisAddIn.Application. _
57: CommandBars.ActiveMenuBar
58: Dim menuCaption As String = "AIR Menu"
59:
60: If menuBar IsNot Nothing Then
61: Dim cmdBarControl As Office.CommandBarPopup = Nothing
62: Dim controlCount As Integer = menuBar.Controls.Count
63:
64: ' Add the new menu.
65: cmdBarControl = CType(menuBar.Controls.Add(_
66: Type:=Office.MsoControlType.msoControlPopup, _
67: Before:=controlCount, Temporary:=True), _
68: Office.CommandBaPopup)
69:
70: cmdBarControl.Caption = menuCaption
71: cmdBarControl.Tag = menuTag
72:
73:
74: ' Add the menu command.
75: menuCommand = CType(cmdBarControl.Controls.Add( _
76: Type:=Office.MsoControlType.msoControlButton, Temporary:=True), _
77: Office.CommandBarButton)
78:
79: With menuCommand
80: .Caption = "Change Articles Order"
81: .Tag = "Menu item"
82: .FaceId = 300
83: End With
84:
85: ' Add the menu command.
86: menuCommand = CType(cmdBarControl.Controls.Add( _
87: Type:=Office.MsoControlType.msoControlButton, Temporary:=True), _
88: Office.CommandBarButton)
89:
90: With menuCommand
91: .Caption = "Process Articles"
92: .Tag = "Menu item"
93: .FaceId = 342
94: End With
95:
96: ' Add the menu command.
97: menuCommand = CType(cmdBarControl.Controls.Add( _
98: Type:=Office.MsoControlType.msoControlButton, Temporary:=True), _
99: Office.CommandBarButton)
100:
101: With menuCommand
102: .Caption = "New Document"
103: .Tag = "Menu item"
104: .FaceId = 278
105: End With
106:
107: End If
108:
109: Catch ex As Exception
110: MessageBox.Show(ex.Message)
111: End Try
112: End Sub
113:
114: 'Event Handlers Remove for brevity
115:
116: #End Region
117:
118: End Class