可以在加载项选项卡中插入自定义按钮、创建自定义工具栏,效果如下:
Sub 插入自定义按钮()Dim cmb As Office.CommandBarDim bt As Office.CommandBarButtonSet cmb = Application.CommandBars("Formatting")Set bt = cmb.Controls.Add(Type:=Office.MsoControlType.msoControlButton)With bt.Caption = "右对齐".FaceId = 2950.Style = msoButtonIconAndCaption.OnAction = "Right"End WithEnd Sub
下面的Right 过程是自定义按钮的回调函数。
Sub Right()Selection.HorizontalAlignment = Excel.Constants.xlRightEnd Sub
添加控件:
Sub 添加控件() '添加的控件用回调函数响应 On Error Resume Next Dim cmb As Office.CommandBar Dim pop As Office.CommandBarPopup Dim bt As Office.CommandBarButton Dim combo As Office.CommandBarComboBox Dim drop As Office.CommandBarControl Dim txt As Office.CommandBarControl Application.CommandBars("Everything").Delete '添加子菜单控件 Set cmb = Application.CommandBars.Add(Name:="Everything