PDA

View Full Version : onAction Problems



cosmarchy
11-26-2010, 12:41 PM
Hi all,

I have the following code which compiles and runs with error but for some reason the function under the .onAction property never gets executed when the menu iten it clicked. Can someone point me in the right direction?

Sub Application_ItemContextMenuDisplay(ByVal CommandBar As Office.CommandBar, ByVal Selection As Selection)
Dim objButton As Office.CommandBarButton
On Error GoTo ErrRoutine

' check to see whether we are in a folder that conains mail items
If (Application.ActiveExplorer.CurrentFolder.DefaultItemType = olMailItem) Then
Set objbuttons = CommandBar.Controls.Add(MsoControlType.msoControlButton, , , , True)

With objbuttons
.BeginGroup = True
.Caption = "Custom Function"
.OnAction = "'DoFunc "" & Selection & ""'"
End With
End If
EndRoutine:
Set objButtons = Nothing
Exit Sub
ErrRoutine:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly Or vbCritical, "Application_ItemContextMenuDisplay"
GoTo EndRoutine
End Sub

Private Function DoFunc(ByVal Selection As Selection)

MsgBox "hello"

End Function


Thanks

JP2112
11-30-2010, 07:21 PM
I don't use Outlook 2007, but that OnAction value doesn't look right.

The OnAction property takes a string value representing the name of the macro you want to run when the button is clicked. Selection is an object passed to the event, that represents the items you've selected, so it doesn't make sense to use it as a string.

HTH

mbm123
12-07-2010, 12:36 AM
I don't believe that the main problem, what does the debugger say?

Paul_Hossler
12-07-2010, 05:28 PM
Shot in the dark --

1. I don't think you have Option Explicit in your module

Shouldn't objButton be objButtons ?



Dim objButton As Office.CommandBarButton

....
Set objbuttons = CommandBar.Controls.Add(MsoControlType.msoControlButton, , , , True)

With objbuttons



2. Maybe


Private Function DoFunc(ByRef myMailItem As olMailItem)

MsgBox "hello"

End Function



Assuming that you can pass paramaters that way

I'd Set the selection to a global variable, and just have a parameter-less DoFunc
Paul

JP2112
12-10-2010, 09:09 AM
It would help if the OP returned and explained what has or hasn't worked.