Shortcut menu doesn't work in .xlt

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

I create my own shortcut menu when my workbook opens. This worked
well until I saved my spreadsheet as a template. Now, although the
menu appears the OnAction event doesn't fire. I've found that if I
save the template first it will then work. However, if I already have
another workbook open then saving is not enough - I must close and
then reopen my spreadsheet before OnAction fires.

Of course the simple answer is not to distribute the spreadsheet as a
template but I am also curious to know what is causing this behaviour.
I guess that shortcut menus must be tied back to a specific workbook
and somehow this is affecting things. Can anyone else shed any more
light on this?

Thanks,
Andrew
 
Hi Andrew,

I haven't encountered this problem. Post the code that you're using to
create your shortcut menu. There may be something in that code that is
causing this problem to occur.

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *
 
Hi Rob,

Thanks for your offer - here is my code (hopefully not too long
winded). Note that I call 'SetMenuValue' from an object event to
populate the edit boxes with relevant numbers. Also, my previous
description wasn't quite accurate - the problem occurs whether there
is another workbook open or not, ie. the 'HandleEditBox' only fires
after first saving then closing and re-opening.

PS. Thanks for your CodeCleaner tool which I have found v. useful on a
number of occassions!

Andrew


' Create a new shortcut menu to display details about the field when
its icon
' is right clicked (format is same as the menu displayed when the
worksheet is
' right clicked)
' Menu is temporary so will be deleted when xl is closed

Public Sub CreateCstmMenu()
Dim cbr As CommandBar
Dim ctl As CommandBarControl
Dim cbt As CommandBarButton
Dim strCaption As String
Dim i As Integer

Dim astrMenus(5) As String

' Define the menus where we want an edit box _
(ensure that there is a macro for each of these controls)
astrMenus(0) = "Max_Rate"
astrMenus(1) = "Min_Rate"
astrMenus(2) = "Opt_Rate"
astrMenus(3) = "Priority"
astrMenus(4) = "CGR"
astrMenus(5) = "CO2"

' Delete the menu first incase it already exists
DeleteMenu
' Create the menu
Set cbr = Application.CommandBars.Add(g_strMENU_NAME, msoBarPopup,
, True)

' Add controls listed above to the menu
For i = UBound(astrMenus) To 0 Step -1
' Format the caption so edit boxes line up
Select Case i
Case 0, 2
strCaption = astrMenus(i)
Case 1
strCaption = astrMenus(i) & " "
Case 3
strCaption = astrMenus(i) & " "
Case Is >= 4
strCaption = astrMenus(i) & " "
End Select

' Add the control
Set ctl = Application.CommandBars(g_strMENU_NAME).Controls.Add
_
(Type:=msoControlEdit, Before:=1, Temporary:=True)
With ctl
.Caption = " " & strCaption
.OnAction = ThisWorkbook.Name & "!HandleEditBox"
.Text = "#N/A" 'default - update when field clicked
End With
Next

' Add the enabled ctl button
Set cbt = Application.CommandBars(g_strMENU_NAME).Controls.Add _
(Type:=msoControlButton, Before:=1, Temporary:=True)
With cbt
.Caption = "Enabled"
.FaceId = 990 'give the button a tick icon
.State = msoButtonDown 'show the field as enabled
.OnAction = ThisWorkbook.Name & "!Enabled"
End With

' Add a button to display field header
Set cbt = Application.CommandBars(g_strMENU_NAME).Controls.Add _
(Type:=msoControlButton, Before:=1, Temporary:=True)
With cbt
.Caption = "===== Jintan ====="
.Enabled = False
End With

End Sub

Public Sub DeleteMenu()
On Error Resume Next
Application.CommandBars(g_strMENU_NAME).Delete
End Sub


Sub SetMenuValue(intIndex As Integer, varNewValue As Variant)
'on error goto ErrTrap

Dim ctl As CommandBarControl
Set ctl = Application.CommandBars(g_strMENU_NAME).Controls(intIndex)
'title (only field which is not enabled)
If ctl.Enabled = False Then
ctl.Caption = "===== " & varNewValue & " ====="
'values
ElseIf ctl.Type = msoControlEdit Then
ctl.Text = CStr(varNewValue)
'enabled/disabled
ElseIf ctl.Type = msoControlButton Then
If varNewValue = False Then
ctl.FaceId = 840 'cross
ctl.State = msoButtonUp
Else
ctl.FaceId = 990
ctl.State = msoButtonDown
End If
End If


ErrTrap:
End Sub



' MACROS TO HANDLE CHANGES IN VALUES!!!!

Sub Enabled()
' Toggle enabled setting
g_objField.Enabled = Not (g_objField.Enabled)
End Sub

' NOTE - user MUST hit enter after entry has been made. OnAction is
not
' triggered if you just click back on the spreadsheet.

Public Sub HandleEditBox()
Dim ctlCaller As CommandBarControl
Dim sngPosLeft As Single
Dim sngPosTop As Single

Set ctlCaller = CommandBars.ActionControl

' Record menu position (so can redisplay menu)
sngPosLeft = ctlCaller.Parent.Left
sngPosTop = ctlCaller.Parent.Top

Select Case Trim(ctlCaller.Caption)
Case "Max_Rate"
' Transfer value to cField obj
g_objField.MaxRate = ctlCaller.Text
' Write value to set-up sheet
Range("Field1").Offset(g_objField.ID,
intOFFSET_TO_MAXRATE).Value = ctlCaller.Text

Case "Min_Rate"
' Transfer value to cField obj
g_objField.MinRate = ctlCaller.Text
' Write value to set-up sheet
Range("Field1").Offset(g_objField.ID,
intOFFSET_TO_MINRATE).Value = ctlCaller.Text

Case "Priority"
' Transfer value to cField obj
g_objField.Priority = ctlCaller.Text
' Write value to set-up sheet
Range("Field1").Offset(g_objField.ID,
intOFFSET_TO_PRIO).Value = ctlCaller.Text

Case "CO2"
' Transfer value to cField obj
g_objField.CO2 = ctlCaller.Text
' Write value to set-up sheet
Range("Field1").Offset(g_objField.ID,
intOFFSET_TO_CO2).Value = ctlCaller.Text

Case Else
' Don't allow changes to opt rate or CGR
Beep
End Select
' Re-display the menu again so user can see the change
'ctlCaller.Parent.ShowPopup sngPosLeft, sngPosTop 'found this to
be annoying

End Sub
 
Sometimes the answer is staring you in the face when you look at it on
a new day!
I replaced
..OnAction = ThisWorkbook.Name & "!HandleEditBox"
with
..OnAction = "HandleEditBox"
and everything works like clockwork again. Didn't think about it
because I only converted to .xlt after I had everything working and by
then I had forgotten exactly what I'd written in here.
 

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

Back
Top