K
Kevin Waddle
Hello,
I am trying to take code that works in Outlook VBA and wrap it in a COM
Add-In.
I have everything figured out except the OnAction Event for the custom
CommandBar Buttons.
I have searched OutlookCode.Com and every other source I know.
Please review my code and see if you can point me in the right direction.
Thanks in Advance,
Kevin
I use the following code to create the buttons:
'From a Standard Module
'*** START CODE ***
Sub MakeMenu()
'Call the code to delete the CommandBar
Call DelMenu
Dim ButtonEvents As Collection
Dim ButtonEvent
Dim cls As New clsEmailSaver
Dim objACC As Object
Dim custBar As Office.CommandBar
Dim Cusbutton
Dim x, db, rs
Dim strDB As String
'Test to make sure the code has started
MsgBox "MenuCode Started"
Set custBar = _
Outlook.Application.ActiveExplorer.CommandBars.Add(Name:="Show", _
Position:=msoBarTop, Temporary:=True)
custBar.Visible = True
'Find the db to pull the menu items
strDB = cls.RegistryGet("EmailSaver", "DefaultPath", "dbPath") & "\Mail.mdb"
'Open Access
Set objACC = CreateObject("Access.Application")
'Open the db
objACC.OpenCurrentDatabase strDB
Set db = objACC.CurrentDb
'Open the Table
Set rs = db.openrecordset("CommandBarItems")
'Loop through the table...
With rs
x = 1
.MoveFirst
Do While Not .EOF
'...and create a button for each record...
Set Cusbutton =
custBar.Controls.Add(Type:=msoControlButton)
With
Outlook.Application.ActiveExplorer.CommandBars("Show").Controls(x)
.Style = msoButtonWrapCaption
.Caption = rs.fields("Caption")
.OnAction = rs.fields("OnAction")
.ToolTipText = rs.fields("ToolTipText")
.BeginGroup = rs.fields("BeginGroup")
.Tag = rs.fields("Tag")
End With
.MoveNext
x = x + 1
Loop
End With
db.Close
Set rs = Nothing
Set db = Nothing
'This is what I came up with to fire the code...not working
Set ButtonEvent = New cbEvents
For x = 1 To
Outlook.Application.ActiveExplorer.CommandBars("Show").Controls.count
Set ButtonEvent.cbBtn = _
Outlook.Application.ActiveExplorer.CommandBars("Show").Controls(x)
ButtonEvents.Add ButtonEvent
Debug.Print x
Next x
'Test to make sure code completed
MsgBox "MenuCode Finished"
End Sub
Public Sub DelMenu()
On Error Resume Next
Outlook.Application.ActiveExplorer.CommandBars("Show").Delete
End Sub
'*** END CODE ***
This is the OnClick Event I am trying to pull from a Class Module
'From a Class Module
'*** START CODE ***
Public WithEvents cbBtn As CommandBarButton
Private Sub cbBtn_Click(ByVal Ctrl As Office.CommandBarButton, _
CancelDefault As Boolean)
'supress errors
On Error Resume Next
'check Tag property
Select Case Ctrl.Tag
Case "AddItemToDB"
Call AddItemToDB
Case "SaveMailItemToDrive"
Call SaveMailItemToDrive
Case "JunkIt"
Call JunkIt
Case "MailCalendar"
Call MailCalendar
End Select
'cancel looking for the sub specified in the OnAction property
CancelDefault = True
End Sub
I am trying to take code that works in Outlook VBA and wrap it in a COM
Add-In.
I have everything figured out except the OnAction Event for the custom
CommandBar Buttons.
I have searched OutlookCode.Com and every other source I know.
Please review my code and see if you can point me in the right direction.
Thanks in Advance,
Kevin
I use the following code to create the buttons:
'From a Standard Module
'*** START CODE ***
Sub MakeMenu()
'Call the code to delete the CommandBar
Call DelMenu
Dim ButtonEvents As Collection
Dim ButtonEvent
Dim cls As New clsEmailSaver
Dim objACC As Object
Dim custBar As Office.CommandBar
Dim Cusbutton
Dim x, db, rs
Dim strDB As String
'Test to make sure the code has started
MsgBox "MenuCode Started"
Set custBar = _
Outlook.Application.ActiveExplorer.CommandBars.Add(Name:="Show", _
Position:=msoBarTop, Temporary:=True)
custBar.Visible = True
'Find the db to pull the menu items
strDB = cls.RegistryGet("EmailSaver", "DefaultPath", "dbPath") & "\Mail.mdb"
'Open Access
Set objACC = CreateObject("Access.Application")
'Open the db
objACC.OpenCurrentDatabase strDB
Set db = objACC.CurrentDb
'Open the Table
Set rs = db.openrecordset("CommandBarItems")
'Loop through the table...
With rs
x = 1
.MoveFirst
Do While Not .EOF
'...and create a button for each record...
Set Cusbutton =
custBar.Controls.Add(Type:=msoControlButton)
With
Outlook.Application.ActiveExplorer.CommandBars("Show").Controls(x)
.Style = msoButtonWrapCaption
.Caption = rs.fields("Caption")
.OnAction = rs.fields("OnAction")
.ToolTipText = rs.fields("ToolTipText")
.BeginGroup = rs.fields("BeginGroup")
.Tag = rs.fields("Tag")
End With
.MoveNext
x = x + 1
Loop
End With
db.Close
Set rs = Nothing
Set db = Nothing
'This is what I came up with to fire the code...not working
Set ButtonEvent = New cbEvents
For x = 1 To
Outlook.Application.ActiveExplorer.CommandBars("Show").Controls.count
Set ButtonEvent.cbBtn = _
Outlook.Application.ActiveExplorer.CommandBars("Show").Controls(x)
ButtonEvents.Add ButtonEvent
Debug.Print x
Next x
'Test to make sure code completed
MsgBox "MenuCode Finished"
End Sub
Public Sub DelMenu()
On Error Resume Next
Outlook.Application.ActiveExplorer.CommandBars("Show").Delete
End Sub
'*** END CODE ***
This is the OnClick Event I am trying to pull from a Class Module
'From a Class Module
'*** START CODE ***
Public WithEvents cbBtn As CommandBarButton
Private Sub cbBtn_Click(ByVal Ctrl As Office.CommandBarButton, _
CancelDefault As Boolean)
'supress errors
On Error Resume Next
'check Tag property
Select Case Ctrl.Tag
Case "AddItemToDB"
Call AddItemToDB
Case "SaveMailItemToDrive"
Call SaveMailItemToDrive
Case "JunkIt"
Call JunkIt
Case "MailCalendar"
Call MailCalendar
End Select
'cancel looking for the sub specified in the OnAction property
CancelDefault = True
End Sub