1: Public Class MenuBuilder
2:
3: #Region "Declarations"
4:
5: Private TemplateCollection As System.Collections.Generic.Dictionary(Of String, String)
6: Private MenuTag As String = "Air Stories Helper Program"
7: Dim menuCommand() As Office.CommandBarButton
8: #End Region
9:
10: #Region "properties"
11: Private thisapp As Word.Application
12: Public Property ThisApplication() As Word.Application
13: Get
14: Return thisapp
15: End Get
16: Set(ByVal value As Word.Application)
17: thisapp = value
18: End Set
19: End Property
20:
21: #End Region
22:
23: #Region "Public Methods"
24: 'loads the controls on the toolbar
25: Public Sub LoadData(ByVal subject As Object)
26: Dim configData As ConfigFile = TryCast(subject, ConfigFile)
27: If Not IsNothing(configData) Or Not IsDBNull(configData) Then
28: TemplateCollection = configData.TemplateCollection
29: End If
30: End Sub
31:
32:
33: Public Sub LoadControls()
34:
35: 'Test for data
36: If Not IsNothing(TemplateCollection) Then
37: 'check if menu bar exists function
38: Call CheckIfMenuBarExists()
39:
40: 'load function
41: Call AddMenuBar()
42: Else
43: MessageBox.Show("No menu data present plese contant Administrator")
44:
45: End If
46: End Sub
47:
48: ' Create the menu, if it does not exist.
49: Private Sub AddMenuBar()
50: Try
51: Dim menuBar As Office.CommandBar = WordAddIn1.Globals.ThisAddIn.Application.CommandBars.ActiveMenuBar
52: Dim menuCaption As String = "AIR Template Menu"
53: Dim intcounter As Integer = 0
54:
55: If menuBar IsNot Nothing Then
56: Dim cmdBarControl As Office.CommandBarPopup = Nothing
57: Dim controlCount As Integer = menuBar.Controls.Count
58:
59: ' Add the new menu.
60: cmdBarControl = CType(menuBar.Controls.Add(Type:=Office.MsoControlType.msoControlPopup, Before:=controlCount, Temporary:=True), _
61: Office.CommandBarPopup)
62: cmdBarControl.Caption = "AirStories"
63: cmdBarControl.Tag = MenuTag
64:
65: For Each item As String In TemplateCollection.Keys
66: Dim Button As Office.CommandBarButton
67: 'Add the menu command.
68: Button = CType(cmdBarControl.Controls.Add(Type:=Office.MsoControlType.msoControlButton, Temporary:=True), Office.CommandBarButton)
69: With Button
70: .Caption = item.ToString
71: .Tag = item.ToString
72: .FaceId = 300
73: End With
74: AddHandler Button.Click, AddressOf menuCommand_Click
75: ReDim Preserve menuCommand(intcounter) ' re-size the array
76: menuCommand(intcounter) = Button
77: intcounter = intcounter + 1 ' increment the counter
78: Next
79:
80: End If
81: Catch ex As Exception
82: MessageBox.Show(ex.Message)
83: End Try
84: End Sub
85:
86:
87: #Region "Constructor"
88: Public Sub New()
89:
90:
91: End Sub
92: #End Region
93:
94: #End Region
95:
96: #Region "Private Methods"
97:
98: ' If the menu already exists, remove it.
99: Private Sub CheckIfMenuBarExists()
100: Try
101: Dim foundMenu As Office.CommandBarPopup = thisapp.Application.CommandBars.ActiveMenuBar.FindControl(Office.MsoControlType.msoControlPopup, Tag:=MenuTag, Visible:=True, Recursive:=True)
102: If foundMenu IsNot Nothing Then
103: foundMenu.Delete(True)
104: End If
105:
106: Catch ex As Exception
107: MessageBox.Show(ex.Message)
108: End Try
109: End Sub
110:
111:
112: 'Method to verify the template location
113: Private Function VerifyTemplateLoc(ByVal Template As String) As Boolean
114: Dim Applic As Word.Application = Globals.ThisAddIn.Application
115:
116: 'split the file name and the location
117: Dim TemplateSplit() As String = Template.Split("\")
118: Dim fname As String = TemplateSplit(TemplateSplit.Length - 1)
119: Dim flocation As String = Template.Replace(fname, "")
120: flocation = flocation.Trim()
121:
122: 'found bool
123: Dim found As Boolean = False 'default is false
124: Try
125: Applic.System.Cursor = Word.WdCursorType.wdCursorWait
126: With Applic.FileSearch
127: .FileName = fname
128: .LookIn = flocation
129: .SearchSubFolders = True
130: If .Execute() = 1 Then
131: found = True
132: Else
133: found = False
134: End If
135: End With
136: Finally
137: Applic.System.Cursor = Word.WdCursorType.wdCursorNormal
138: End Try
139: Return found 'return bool
140: End Function
141:
142: Private Sub Newdocument(ByVal location As String)
143:
144: MessageBox.Show(location.ToString)
145:
146: End Sub
147:
148: #End Region
149: