M
Mita Garg
Hi All,
I am writting a COM Add-in. All i want to do is create a button on the
standard Toolbar of the "New Mail Message" (i.e. when you click on the
"New" button to write emails).
I have managed to create the button on the toolbar, but then this
button appears when you click on the Contacts, Calendar e.t.cs "New"
button. Also it appears when you read each mail from the Inbox.
I WANT TO APPEAR IT OLNY ON THE MAIL "NEW" BUTTON (AND NOT ANYWHERE).
Another problem is it stays in memory when i close Outlook.
My code looks something like this.
Connect.dsr.........
'---------------------------------
Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
On Error Resume Next
If Application.Explorers.Count = 0 And
Application.Inspectors.Count = 0
Then
Exit Sub
End If
'AddInInst represents COMAddIn object
'Create and Initialize a base class
gBaseClass.InitHandler Application, AddInInst.ProgId
'DebugWrite "IDT2 OnConnection"
End Sub
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode _
As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
gBaseClass.UnInitHandler
If RemoveMode = ext_dm_UserClosed Then
Else
End If
Set gBaseClass = Nothing
'DebugWrite "AddinInstance_OnDisconnection"
End Sub
'-----------------------------------
class.....
Friend Sub InitHandler(olApp As Outlook.Application, strProgID As
String)
'Declared WithEvents
Set objOutlook = olApp 'Application Object
'Instantiate a public module-level Outlook application variable
Set golApp = olApp
'CDO Session if required
Set gobjCDO = CreateObject("MAPI.Session")
gobjCDO.Logon "", "", False, False
If Err <> 0 Then
MsgBox "Bad Logon: " & Err.Description
End If
'ProgID string required for CommandBarControls
gstrProgID = strProgID
Set colInsp = objOutlook.Inspectors 'Inspectors Object
End Sub
Friend Sub UnInitHandler()
On Error Resume Next
Set frmTLX = Nothing
objCBButton.Delete
Set objCBButton = Nothing
Set objCB = Nothing
Set objInsp = Nothing
Set objExpl = Nothing
Set colInsp = Nothing
Set objNS = Nothing
Set golNs = Nothing
Set golApp = Nothing
Set gobjCDO = Nothing 'Uncomment for CDO
Set objOutlook = Nothing
End Sub
'Add Set statements as required
'Also remove unnecessary object declarations
Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
Dim objItem As Object
On Error Resume Next
Dim str As String
Set objInsp = Inspector
Set objItem = objInsp.CurrentItem
Select Case objItem.Class
Case olMail
Set objCB = objInsp.CommandBars("Standard")
'Set objItem = objInsp.CurrentItem
Set objCBButton = CreateAddInCommandBarButton(gstrProgID,
objCB, "TestSMS", "TestSMS", "Send Test SMS", 0, True,
msoButtonIconAndCaption)
End Select
End Sub
Private Sub objCBButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
frmTLX.Show
End Sub
'Due to MAPI issues, On_Disconnection might not fire during Outlook
shutdown
'Call UnInitHandler in objExpl_Close event
Private Sub objExpl_Close()
On Error Resume Next
If golApp.Explorers.Count <= 1 And golApp.Inspectors.Count = 0
Then
UnInitHandler
End If
End Sub
'Call UnInitHandler in objInsp_Close event
Private Sub objInsp_Close()
On Error Resume Next
If golApp.Explorers.Count <= 1 And golApp.Inspectors.Count = 0
Then
UnInitHandler
End If
End Sub
Public Function CreateAddInCommandBarButton _
(strProgID As String, objCommandBar As CommandBar, _
strCaption As String, strTag As String, strTip As String, _
intFaceID As Integer, blnBeginGroup As Boolean, intStyle As
Integer) _
As Office.CommandBarButton
Dim ctlBtnAddIn As CommandBarButton
Dim objPicture As stdole.IPictureDisp 'Only for Office XP
Dim objMask As stdole.IPictureDisp 'Only for Office XP
Dim str As String
str = App.Path + "\MESSAGEmanager.bmp"
On Error Resume Next
' Test to determine if button exists on command bar.
Set ctlBtnAddIn = objCommandBar.FindControl(Tag:=strTag)
If ctlBtnAddIn Is Nothing Then
' Add new button.
Set ctlBtnAddIn =
objCommandBar.Controls.Add(Type:=msoControlButton,
Parameter:=strTag)
Set objPicture = LoadPicture(str)
Set objMask = LoadPicture(str)
With ctlBtnAddIn
.Caption = strCaption
.Tag = strTag
'for image comment these three lines
'If intStyle <> msoButtonIconAndCaption Then
' .FaceId = intFaceID
'End If
.Style = intStyle
.Picture = objPicture
.Mask = objMask
.ToolTipText = strTip
.BeginGroup = blnBeginGroup
' Set the OnAction property with ProgID of Add-In
.OnAction = "<!" & strProgID _
& ">"
End With
End If
' Return reference to new commandbar button.
Set CreateAddInCommandBarButton = ctlBtnAddIn
End Function
I am writting a COM Add-in. All i want to do is create a button on the
standard Toolbar of the "New Mail Message" (i.e. when you click on the
"New" button to write emails).
I have managed to create the button on the toolbar, but then this
button appears when you click on the Contacts, Calendar e.t.cs "New"
button. Also it appears when you read each mail from the Inbox.
I WANT TO APPEAR IT OLNY ON THE MAIL "NEW" BUTTON (AND NOT ANYWHERE).
Another problem is it stays in memory when i close Outlook.
My code looks something like this.
Connect.dsr.........
'---------------------------------
Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
On Error Resume Next
If Application.Explorers.Count = 0 And
Application.Inspectors.Count = 0
Then
Exit Sub
End If
'AddInInst represents COMAddIn object
'Create and Initialize a base class
gBaseClass.InitHandler Application, AddInInst.ProgId
'DebugWrite "IDT2 OnConnection"
End Sub
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode _
As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
gBaseClass.UnInitHandler
If RemoveMode = ext_dm_UserClosed Then
Else
End If
Set gBaseClass = Nothing
'DebugWrite "AddinInstance_OnDisconnection"
End Sub
'-----------------------------------
class.....
Friend Sub InitHandler(olApp As Outlook.Application, strProgID As
String)
'Declared WithEvents
Set objOutlook = olApp 'Application Object
'Instantiate a public module-level Outlook application variable
Set golApp = olApp
'CDO Session if required
Set gobjCDO = CreateObject("MAPI.Session")
gobjCDO.Logon "", "", False, False
If Err <> 0 Then
MsgBox "Bad Logon: " & Err.Description
End If
'ProgID string required for CommandBarControls
gstrProgID = strProgID
Set colInsp = objOutlook.Inspectors 'Inspectors Object
End Sub
Friend Sub UnInitHandler()
On Error Resume Next
Set frmTLX = Nothing
objCBButton.Delete
Set objCBButton = Nothing
Set objCB = Nothing
Set objInsp = Nothing
Set objExpl = Nothing
Set colInsp = Nothing
Set objNS = Nothing
Set golNs = Nothing
Set golApp = Nothing
Set gobjCDO = Nothing 'Uncomment for CDO
Set objOutlook = Nothing
End Sub
'Add Set statements as required
'Also remove unnecessary object declarations
Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
Dim objItem As Object
On Error Resume Next
Dim str As String
Set objInsp = Inspector
Set objItem = objInsp.CurrentItem
Select Case objItem.Class
Case olMail
Set objCB = objInsp.CommandBars("Standard")
'Set objItem = objInsp.CurrentItem
Set objCBButton = CreateAddInCommandBarButton(gstrProgID,
objCB, "TestSMS", "TestSMS", "Send Test SMS", 0, True,
msoButtonIconAndCaption)
End Select
End Sub
Private Sub objCBButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
frmTLX.Show
End Sub
'Due to MAPI issues, On_Disconnection might not fire during Outlook
shutdown
'Call UnInitHandler in objExpl_Close event
Private Sub objExpl_Close()
On Error Resume Next
If golApp.Explorers.Count <= 1 And golApp.Inspectors.Count = 0
Then
UnInitHandler
End If
End Sub
'Call UnInitHandler in objInsp_Close event
Private Sub objInsp_Close()
On Error Resume Next
If golApp.Explorers.Count <= 1 And golApp.Inspectors.Count = 0
Then
UnInitHandler
End If
End Sub
Public Function CreateAddInCommandBarButton _
(strProgID As String, objCommandBar As CommandBar, _
strCaption As String, strTag As String, strTip As String, _
intFaceID As Integer, blnBeginGroup As Boolean, intStyle As
Integer) _
As Office.CommandBarButton
Dim ctlBtnAddIn As CommandBarButton
Dim objPicture As stdole.IPictureDisp 'Only for Office XP
Dim objMask As stdole.IPictureDisp 'Only for Office XP
Dim str As String
str = App.Path + "\MESSAGEmanager.bmp"
On Error Resume Next
' Test to determine if button exists on command bar.
Set ctlBtnAddIn = objCommandBar.FindControl(Tag:=strTag)
If ctlBtnAddIn Is Nothing Then
' Add new button.
Set ctlBtnAddIn =
objCommandBar.Controls.Add(Type:=msoControlButton,
Parameter:=strTag)
Set objPicture = LoadPicture(str)
Set objMask = LoadPicture(str)
With ctlBtnAddIn
.Caption = strCaption
.Tag = strTag
'for image comment these three lines
'If intStyle <> msoButtonIconAndCaption Then
' .FaceId = intFaceID
'End If
.Style = intStyle
.Picture = objPicture
.Mask = objMask
.ToolTipText = strTip
.BeginGroup = blnBeginGroup
' Set the OnAction property with ProgID of Add-In
.OnAction = "<!" & strProgID _
& ">"
End With
End If
' Return reference to new commandbar button.
Set CreateAddInCommandBarButton = ctlBtnAddIn
End Function