willsonlincake 发表于 2022-5-13 11:20:02

不需要VSTO,VBA也能添加按钮到工具栏

看下面的代码
Set oToolbar = CommandBars.Add(Name:=MyToolbar, _
      Position:=msoBarFloating, Temporary:=True)
    If Err.Number <> 0 Then
          ' The toolbar's already there, so we have nothing to do
          Exit Sub
    End If

    On Error GoTo ErrorHandler

    ' Now add a button to the new toolbar
    Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)

    ' And set some of the button's properties

    With oButton

         .DescriptionText = "This is my first button"
          'Tooltip text when mouse if placed over button

         .Caption = "DCP Bullet Format"
         'Text if Text in Icon is chosen

         .OnAction = "dcp_bullets"
          'Runs the Sub Button1() code when clicked

         .Style = msoButtonIconAndCaption
          ' Button displays as icon, not text or both

         .FaceId = 12
          ' chooses icon #52 from the available Office icons

    End With
页: [1]
查看完整版本: 不需要VSTO,VBA也能添加按钮到工具栏