Click 事件
优质
小牛编辑
127浏览
2023-12-01
该事件发生在用户单击CommandBarButton 对象时。
Private Sub CommandBarButton_Click
(ByVal Ctrl As CommandBarButton,
ByVal CancelDefault As Boolean)
Click 事件的语法中包含两个参数,下表中列出了对这两个参数的说明。
参数说明CtrlCommandBarButton 类型,必需。指示初始化该事件的CommandBarButton 控件。CancelDefaultBoolean 类型,必需。如果执行了与CommandBarButton 控件关联的默认操作,该值为False,除非其他过程或加载项取消了此操作。说明
Click 事件由CommandBarButton 对象识别。若要返回特定CommandBarButton 控件的Click 事件,请用关键词WithEvents 声明一个变量,并将变量的值设置为该控件。
示例
以下示例在宿主应用程序的“文件”菜单中创建了一个新命令栏按钮,它使用户可以 CSV (逗号分隔)文件格式保存工作簿。(本示例可在所有应用程序中运行,但有关存为 CSV 格式的上下文只适用于 Microsoft Excel。)
Private HostApp As Object
Sub createAndSynch()
Dim iIndex As Integer
Dim iCount As Integer
Dim fBtnExists As Boolean
Dim obCmdBtn As Object
Dim btnSaveAsCSVHandler as new Class1
On Error GoTo errHandler
Set HostApp = Application
Dim barHelp As Office.CommandBar
Set barHelp = Application.CommandBars("File")
fBtnExists = False
iCount = barHelp.Controls.Count
For iIndex = 1 To iCount
If barHelp.Controls(iIndex).Caption = "Save As CSV (Comma Delimited)" Then fBtnExists = True
Next
Dim btnSaveAsCSV As Office.CommandBarButton
If fBtnExists Then
Set btnSaveAsCSV = barHelp.Controls("Save As CSV (Comma Delimited)")
Else
Set btnSaveAsCSV = barHelp.Controls.Add(msoControlButton)
btnSaveAsCSV.Caption = "Save As CSV (Comma Delimited)"
End If
btnSaveAsCSV.Tag = "btn1"
btnSaveAsCSVHandler.SyncButton btnSaveAsCSV
Exit Sub
errHandler:
' Insert error handling code here
End Sub