Preventing multiple instances of commandbar button during COM add-in startup

S

Senapathy K

Hi,

I had posted a question regarding packaging of a Outlook macro. Taking Sue
Mosher's suggestion, I have now packaged it as a COM addin.

The setting for the load behaviour of my add in is "Startup".
I have taken the following code from MSDN article (
http://support.microsoft.com/kb/238228 ) to create 2 new buttons on the
"Standard" toolbar.

' this is in AddinInstance_OnConnection
Set oStandardBar =
Application.ActiveExplorer.CommandBars("Standard")

Set AFIButton = oStandardBar.Controls.Add(1)
With AFIButton
...
End With

Set IUSButton = oStandardBar.Controls.Add(1)
With IUSButton
...
End With

Now the problem is that when I load the Add in for the first time everything
is fine. 2 buttons are added. When I restart Outlook, the buttons are
getting added again. This is to be expected from the above code. But the
trouble is I am not able to find in any way whether the 2 buttons already
exist.

I have tried to check with the code from the same MSDN article
(http://support.microsoft.com/kb/238228) that says to check for the item
before adding:

' In case the button was not deleted, use the exiting one...
Set AFIButton = oStandardBar.Controls.Item("AFI Mail")
If AFIButton Is Nothing Then
Set AFIButton = oStandardBar.Controls.Add(1)
...
End if

I have tried this from both OnConnection and OnStartupComplete subroutines.
But this simply does not succeed. AFIButton is being returned as 'Nothing'
everytime. And new button is being added every time I restart Outlook.

I have even tried looping through the controls of the CommandBar using the
Index. However, the Count property of the CommandBars("Standard") is
returned as 0, so I cannot even loop through the CommandBar buttons and
check for the captions.

How can I check for existence of the button before adding afresh?

Regards,
Sena
 
S

Senapathy K

I found out the problem. It was a simple typo from my side.

Instead of using:
Set AFIButton = oStandardBar.Controls.Item("AFI Mail")
^^^^^^
If AFIButton Is Nothing Then

I was giving:
Set AFIButton = oStandardBar.Item("AFI Mail")
^
If AFIButton Is Nothing Then

Quite obviously, it was always returning me 'Nothing' as the result.

Warm regards,
Sena
 
M

Michael Bauer

Hi Sena,

for checking by control name whether your button exists or not, you have
to add the button with an unique name, of course.

In addition at least for OL (Office) 2000 the CommandBar or MenuBar
handling is buggy. It´s recommended not to manipulate the existing Bars,
but add your own.

Please see this sample:

Private Function CreateCommandBarButton(oBars As Office.CommandBars) As
Office.CommandBarButton
On Error Resume Next
Dim oMenu As Office.CommandBar
Dim oBtn As Office.CommandBarButton
Const BAR_NAME As String = "YourCommandBarName"
Const CMD_NAME As String = "YourButtonName"

Set oMenu = oBars(BAR_NAME)
If oMenu Is Nothing Then
Set oMenu = oBars.Add(BAR_NAME, msoBarTop, , True)
Set oBtn = oMenu.Controls.Add(msoControlButton, , CMD_NAME, , True)
oBtn.Caption = CMD_NAME
oBtn.Tag = CMD_NAME

Else
Set oBtn = oMenu.FindControl(, , CMD_NAME)
If oBtn Is Nothing Then
Set oBtn = oMenu.Controls.Add(msoControlButton, , CMD_NAME, ,
True)
End If
End If

oMenu.Visible = True
Set CreateCommandBarButton = oBtn
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top