bcyde
May 3rd, 2001, 06:38 PM
I have a COM addin that adds 2 buttons to the outlook message windows and they seem to work only on the first time I install the add-in, and every time after the event handler for both buttons gets mapped to the first. For example, in the code below button1 pops up a messagebox that says Action1 and button2 pops up a messagebox saying Action2. On the first, after installation, everything works fine, but every time after button2 does nothing and button1 pops up both message boxes. I'm not sure if somehow the inspectors and event handlers are getting mixed up, any help, comments, criticisms, etc. would be greatly appreciated. Thanks in advance.
option Explicit
Implements IDTExtensibility2
Dim goOutlook as Outlook.Application
Dim withevents goInspectors as Outlook.Inspectors 'handle inspector events so we can detect when user is creating a newmail message
Dim withevents Button1 as Office.CommandBarButton 'Action1
Dim withevents Button2 as Office.CommandBarButton 'Action2
'OnConnection - this is called by Outlook when the addin is first connected (loaded)
private Sub IDTExtensibility2_OnConnection(byval Application as Object, byval ConnectMode as AddInDesignerObjects.ext_ConnectMode, byval AddInInst as Object, custom() as Variant)
on error resume next
set goOutlook = Application 'store outlook application object so we can use it later
set goInspectors = goOutlook.Inspectors 'store the inspectors object so we can detect when the user is composing a new mail message
End Sub
private Sub goInspectors_NewInspector(byval Inspector as Outlook.Inspector)
on error resume next
'see if our button already exist on the commandbar
Dim oBar as Office.CommandBar
set oBar = Inspector.CommandBars("Standard")
set Button1 = oBar.FindControl(Type:=msoControlButton, Id:=oBar.Controls("Action1").Id)
set Button2 = oBar.FindControl(Type:=msoControlButton, Id:=oBar.Controls("Action2").Id)
'if they do not, then add it to the controlbar
If Button1 is nothing then
set Button1 = oBar.Controls.Add(Type:=msoControlButton)
Button1.Caption = "Action1"
Button1.ToolTipText = "Action1 message text"
Button1.Tag = "Action1"
Button1.OnAction = "!<myOutlook.Connect>"
End If
If Button2 is nothing then
set Button2 = oBar.Controls.Add(Type:=msoControlButton)
Button2.Caption = "Action2"
Button2.ToolTipText = "Action2 message text"
Button2.Tag = "Action2"
Button2.OnAction = "!<myOutlook.Connect>"
End If
'use the same icon as the Send button for our button
Dim oSendButton as Office.CommandBarButton
set oSendButton = oBar.FindControl(Type:=msoControlButton, Id:=oBar.Controls("Send").Id)
'only try setting the properties if found the Send button
If Not oSendButton is nothing then
Button1.Style = msoButtonIconAndCaption
Button1.FaceId = oSendButton.FaceId
Button2.Style = msoButtonIconAndCaption
Button2.FaceId = oSendButton.FaceId
End If
'only show our button if this is a mail message
If Inspector.CurrentItem.Class = olMail then
Button1.Visible = true
Button2.Visible = true
else
Button1.Visible = false
Button2.Visible = false
End If
End Sub
private Sub Button1_Click(byval Ctrl as Office.CommandBarButton, CancelDefault as Boolean)
on error resume next
MsgBox ("Action1")
End Sub
private Sub Button2_Click(byval Ctrl as Office.CommandBarButton, CancelDefault as Boolean)
on error resume next
MsgBox ("Action2")
End Sub
private Sub IDTExtensibility2_OnStartupComplete(custom() as Variant)
End Sub
private Sub IDTExtensibility2_OnAddInsUpdate(custom() as Variant)
End Sub
private Sub IDTExtensibility2_OnBeginShutdown(custom() as Variant)
End Sub
private Sub IDTExtensibility2_OnDisconnection(byval RemoveMode as AddInDesignerObjects.ext_DisconnectMode, custom() as Variant)
set goOutlook = nothing
set goInspectors = nothing
set Button1 = nothing
set Button2 = nothing
End Sub
private Sub oMyExplorer_Activate()
End Sub
option Explicit
Implements IDTExtensibility2
Dim goOutlook as Outlook.Application
Dim withevents goInspectors as Outlook.Inspectors 'handle inspector events so we can detect when user is creating a newmail message
Dim withevents Button1 as Office.CommandBarButton 'Action1
Dim withevents Button2 as Office.CommandBarButton 'Action2
'OnConnection - this is called by Outlook when the addin is first connected (loaded)
private Sub IDTExtensibility2_OnConnection(byval Application as Object, byval ConnectMode as AddInDesignerObjects.ext_ConnectMode, byval AddInInst as Object, custom() as Variant)
on error resume next
set goOutlook = Application 'store outlook application object so we can use it later
set goInspectors = goOutlook.Inspectors 'store the inspectors object so we can detect when the user is composing a new mail message
End Sub
private Sub goInspectors_NewInspector(byval Inspector as Outlook.Inspector)
on error resume next
'see if our button already exist on the commandbar
Dim oBar as Office.CommandBar
set oBar = Inspector.CommandBars("Standard")
set Button1 = oBar.FindControl(Type:=msoControlButton, Id:=oBar.Controls("Action1").Id)
set Button2 = oBar.FindControl(Type:=msoControlButton, Id:=oBar.Controls("Action2").Id)
'if they do not, then add it to the controlbar
If Button1 is nothing then
set Button1 = oBar.Controls.Add(Type:=msoControlButton)
Button1.Caption = "Action1"
Button1.ToolTipText = "Action1 message text"
Button1.Tag = "Action1"
Button1.OnAction = "!<myOutlook.Connect>"
End If
If Button2 is nothing then
set Button2 = oBar.Controls.Add(Type:=msoControlButton)
Button2.Caption = "Action2"
Button2.ToolTipText = "Action2 message text"
Button2.Tag = "Action2"
Button2.OnAction = "!<myOutlook.Connect>"
End If
'use the same icon as the Send button for our button
Dim oSendButton as Office.CommandBarButton
set oSendButton = oBar.FindControl(Type:=msoControlButton, Id:=oBar.Controls("Send").Id)
'only try setting the properties if found the Send button
If Not oSendButton is nothing then
Button1.Style = msoButtonIconAndCaption
Button1.FaceId = oSendButton.FaceId
Button2.Style = msoButtonIconAndCaption
Button2.FaceId = oSendButton.FaceId
End If
'only show our button if this is a mail message
If Inspector.CurrentItem.Class = olMail then
Button1.Visible = true
Button2.Visible = true
else
Button1.Visible = false
Button2.Visible = false
End If
End Sub
private Sub Button1_Click(byval Ctrl as Office.CommandBarButton, CancelDefault as Boolean)
on error resume next
MsgBox ("Action1")
End Sub
private Sub Button2_Click(byval Ctrl as Office.CommandBarButton, CancelDefault as Boolean)
on error resume next
MsgBox ("Action2")
End Sub
private Sub IDTExtensibility2_OnStartupComplete(custom() as Variant)
End Sub
private Sub IDTExtensibility2_OnAddInsUpdate(custom() as Variant)
End Sub
private Sub IDTExtensibility2_OnBeginShutdown(custom() as Variant)
End Sub
private Sub IDTExtensibility2_OnDisconnection(byval RemoveMode as AddInDesignerObjects.ext_DisconnectMode, custom() as Variant)
set goOutlook = nothing
set goInspectors = nothing
set Button1 = nothing
set Button2 = nothing
End Sub
private Sub oMyExplorer_Activate()
End Sub