【问题标题】:Change the parent button name from context menu从上下文菜单更改父按钮名称
【发布时间】:2020-05-05 15:51:44
【问题描述】:

我创建了一个自定义右键上下文菜单

当我点击任何子菜单项时,我想更新父按钮标题

Option Explicit

Public Const Mname As String = "MyPopUpMenu"

Sub PopUpMenu()

    ' Create the custom right click menu.
    Call RClickMenu

    ' Display the popup menu.
    On Error Resume Next
    Application.CommandBars(Mname).ShowPopup
    On Error GoTo 0
End Sub

自定义右键菜单:

Sub RClickMenu()

Dim MenuItem As CommandBarPopup
Dim SectionType As String
 = "Sections"

' Add the popup menu.
With Application.CommandBars.Add(Name:=Mname, Position:=msoBarPopup, _
     MenuBar:=False, Temporary:=True)

    Set MenuItem = .Controls.Add(Type:=msoControlPopup)
    With MenuItem
        .caption = "File Type"

        With .Controls.Add(Type:=msoControlButton)
            .caption = "File 1"
            .OnAction = "setCaption"
            .Parameter = "file1"
        End With

        With .Controls.Add(Type:=msoControlButton)
            .caption = "File 2"
            .OnAction = "setCaption"
            .Parameter = "file2"
        End With

    End With

End With
End Sub

鼠标右键点击事件:

Public Sub btnFindSections_MouseDown(ByVal button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If button = 1 Then
        ActiveWorkbook.FollowHyperlink "https://www.file.com"
    ElseIf button = 2 Then
        PopUpMenu
    End If
End Sub

设置字幕方式:

Sub setCaption()
    Select Case CommandBars.ActionControl.Parameter
    Case "Sections"
        ActiveSheet.Shapes("CommandButton1").Name = "Sections" // Error saying item not found
    End Select
End Sub

setCaption() 中,我需要找到父按钮并将其标题从Find files 更改为Find Sections

【问题讨论】:

  • 我试着玩你的“PopUpMenu”,我问自己为什么在Application.CommandBars(Mname).ShowPopup 行之前使用On Error Resume Next。通常,只有当特定的CommandBar 可以创建两次时,才会发生错误。在这种情况下,最好在创建之前删除它,但我很好奇你的情况是什么原因......
  • @FaneDuru 该行是从我发现的示例中复制的。我需要检查它的意义
  • 试试,请在ShowRightClickMenu sub 中使用DeleteToolbar,就在With Application.CommandBars.aDD(... 行之前。潜艇看起来像:Private Sub DeleteToolbar()On Error Resume NextApplication.CommandBars(Mname).DeleteOn Error GoTo 0End Sub。注意,有 5 行代码。在这里,On Error Resume Next 对于不存在这样的条的情况很重要...
  • @FaneDuru 知道如何在右键菜单中将控制按钮字体设置为粗体吗? stackoverflow.com/questions/61677579/…
  • 是的,我想是的,但我现在没有时间......

标签: excel vba


【解决方案1】:

尝试替换(如果是 ActiveX 按钮):

ActiveSheet.OLEObjects("CommandButton1").Object.Caption = "Lists"

ActiveSheet.OLEObjects("CommandButton1").Name = "Lists"

如果是“表单”按钮,请将同一行替换为:

ActiveSheet.Buttons("CommandButton1").Caption = "Lists"

在上面的示例中,代码使用按钮Name,就像您要求的那样。

如果您需要使用其Caption 查找ActiveX 按钮,则必须使用下一个迭代代码:

Sub TestButtonCaptionSearch()
  Dim but As OLEObject
    For Each but In ActiveSheet.OLEObjects
        If but.Object.Caption = "CommandButton1" Then
            but.Object.Caption = "Lists": Exit For
        End If
    Next
End Sub

【讨论】:

  • 错误:无法获取 Form 的 Worksheet 类的按钮属性
  • 错误:错误:无法获取 Active X 的 Worksheet 类的 OLEOjects 属性
  • @kittu:你的按钮是什么类型的?它是放在一张纸上,还是放在一张表格上?如果在工作表上,看起来很可能,它是 activeX 类型还是 Form 类型?
  • 它是一个 ActiveX 按钮
  • @kittu:如果您标记我 (@FaneDuru),我可以尝试帮助...但是您没有使代码成为公认的答案...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
  • 2019-01-04
  • 1970-01-01
相关资源
最近更新 更多