Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2009, 01:26 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error setting menu control's OnAction property

Hello, I'm having a problem with some code in my personal.xls which aims to create a menu to run macros which I've written.



I've used this code for a few months, but its suddenly started erroring on a line which sets the OnAction property of a menu control. The error is "Method 'OnAction' of object '_CommandBarButton' failed".



The macro I'm trying to set this button to is in the same module as the code which creates the menu. I've tried commenting the first menu control but it still happens on the next one. I don't remember making any changes that may have caused this to suddenly start erroring.



In case that helps the full sub is below:


Code:
Sub CreateAndrewMenu()
Dim MenuBar As CommandBar Dim NewMenu As CommandBarPopup Dim NewControl As CommandBarControl Const strMenuName As String = "Worksheet Menu Bar" Set MenuBar = CommandBars(strMenuName) Dim NoOfControls As Byte Dim EachControl As Byte Dim blnFound As Boolean Dim NameOfSub As String Dim strToolTip As String Dim strCaption As String
'iterate the menus on Worksheet Menu Bar to see if Andrew menu exists:
NoOfControls = MenuBar.Controls.Count For EachControl = 1 To NoOfControls If MenuBar.Controls.Item(EachControl).Caption = "&Andrew" Then blnFound = True Next EachControl
'If the Andrew menu exists, delete it so we can start again - in case its corrupt
If blnFound Then MenuBar.Controls("Andrew").Delete End If
'add Andrew menu to Worksheet Menu Bar:
Set NewMenu = MenuBar.Controls.Add(Type:=msoControlPopup) With NewMenu .Caption = "&Andrew" .Visible = True .Enabled = True End With
'Add sub DefaultTableFormat:
NameOfSub = "DefaultTableFormat" strCaption = "Default &table format" strToolTip = "Convert data to default table format" GoSub CheckThatItemExists
'Add sub SentenceCase:
NameOfSub = "SentenceCase" strCaption = "Change to &sentence case" strToolTip = "Change the selection to sentence case" GoSub CheckThatItemExists
'Add sub CopyFormula:
NameOfSub = "CopyFormula" strCaption = "Copy formula" strToolTip = "Copy formula of current cell without adjusting references" GoSub CheckThatItemExists
TidyUp:
On Error Resume Next Set MenuBar = Nothing Set NewMenu = Nothing Set NewControl = Nothing Exit Sub
ErrorCode:
If Err.Number = -2147467259 Then 'possibly cos in a protected book - no action Else ShowErrorMessage Err.Number, Err.Description End If Resume TidyUp
CheckThatItemExists:
'iterate the options on the Andrew menu to see if NameOfSub is accessed from the menu: blnFound = False NoOfControls = NewMenu.Controls.Count For EachControl = 1 To NoOfControls If InStr(NewMenu.Controls.Item(EachControl).OnAction, NameOfSub) > 0 Then blnFound = True Next EachControl If Not (blnFound) Then 'add this sub as a new control: Set NewControl = NewMenu.Controls.Add(Type:=msoControlButton) With NewControl .Caption = strCaption .OnAction = NameOfSub 'THIS LINE NOW ERRORS .Style = msoButtonCaption .TooltipText = strToolTip End With End If Return
End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
Error setting Range Value property Keith Smith Excel VBA 3 March 14th, 2008 07:32 AM
Setting property value of class - objectdatasouce ranjan74feb ASP.NET 2.0 Professional 0 October 20th, 2007 03:51 AM
Setting "DBEngine.SystemDB" property jkuusik BOOK: Access 2003 VBA Programmer's Reference 2 December 28th, 2005 06:51 AM
setting a web control's ID programatically lcsgeek General .NET 3 January 24th, 2005 02:50 PM
Error setting Formula Array property for a Range arnowitz Excel VBA 2 February 5th, 2004 02:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.