Problem with OnAction

A

Ashley Sutcliffe

Hi all,

It has been a while since I posted my last conundrum. I think it's high
time you had some more brain exercise ...

I have read a number of posts about the OnAction property, but none of
them really address the problem that I am having.

I am using VB 6 with Office/Outlook 2000 and Windows 2000 to create an
Outlook COM addin.

I want to have a number of buttons on a menu bar. The number of buttons
and the properties of these buttons will be determined by information
from a database. I therefore cannot (elegantly) use the WithEvents
keyword and a normal event handler.

The approach that have tried is to set the OnAction property to a sub
that will handle the button click event. Unfortunately, I cannot get
this to work at all.

Here is some very simple source code that demonstrates the problem that
I have:

Option Explicit

Private mOLApp As Outlook.Application
Private MyButton As Office.CommandBarControl

Public Sub ShowMessage()
MsgBox "hello?"
End Sub

Private Sub AddinInstance_OnConnection(ByVal Application As Object,
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal
AddInInst As Object, custom() As Variant)

Set mOLApp = Application

MsgBox "Confirm that the addin has loaded"

Dim oPop As Office.CommandBarPopup

'I want to use the Actions menu for a mail item
Set oPop = mOLApp.ActiveExplorer.CommandBars("Menu
Bar").FindControl(Id:=30131)

Set MyButton = oPop.Controls.Add(Type:=msoControlButton,
Temporary:=True, Before:=1)
MyButton.Caption = "Test"
MyButton.OnAction = "=ShowMessage"
End Sub

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As
AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
End Sub

Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
End Sub

If you try this source code in a simple addin, then nothing will happen
when you click on the button. The Microsoft documentation on this
subject is quite scant and hard to find. The code that I have found all
seems to do exactly what I have done, with, I assume, the notable
exception that it works.

If anyone can point out what I am doing wrong or an alternative way of
achieving my goal, then I would be very grateful.

Regards

Ashley
 
K

Ken Slovak - [MVP - Outlook]

The solution is to use a wrapper collection and in the class module
you create that gets wrapped in the collection to declare a WithEvents
button object. Each button that is added is instantiated in the class
module, the class module is added to the collection and each button
event fires independently if you assign a unique Tag to the button.
I've used that method when I wanted to add a variable number of menu
items to a custom menu depending on how many entries were in either a
database table or listed in a post form in a public folder.

See the ItemsCB COM addin sample, which shows an Explorer wrapper that
can be used as a model for a button wrapper. It's on the Resources
page at www.microeye.com
 
A

Ashley Sutcliffe

Hi Ken,

You are clearly right. I have used wrappers for a number of objects in
my program but it didn't occur to me to use one for this problem.

As a matter of interest, should the OnAction business work?

Regards

Ashley
 
K

Ken Slovak - [MVP - Outlook]

For OnAction to fire and work correctly in a COM addin you must use a
special syntax:
.OnAction = "!<" & gstrProgID & ">"
where gstrProgID is the AddInInst.ProgId

When you have more than one button that is the same, for example in
multiple Inspectors or Explorers each button must use a unique .Tag
property. Again a wrapper collection is used so each member Inspector
or Explorer has its own event handler to handle the button click event
if you want each button to operate independently of the other buttons.
 

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