国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 辦公 > Word > 正文

Word自定義右鍵菜單的VBA代碼示例

2024-08-22 13:26:48
字體:
來源:轉載
供稿:網友

  本Word的VBA代碼的功能簡介:在右鍵文本菜單的中部位置(相當于右擊文本時出現的菜單),添加一個自定義命令,并執行相應過程。

  Private Sub Document_Close()

  On Error Resume Next

  Application.CommandBars("Text").Controls("Test").Delete ’恢復原有菜單

  End Sub

  Private Sub Document_Open()

  Dim Half As Byte

  On Error Resume Next

  Dim NewButton As CommandBarButton

  Application.CommandBars("Text").Controls("Test").Delete ‘預防性刪除

  Half = Int(Application.CommandBars("Text").Controls.Count / 2) ‘中間位置

  Set NewButton = Application.CommandBars("Text").Controls.Add(Type:=msoControlButton, Before:=Half)

  With NewButton

  .Caption = "Test" ’命令名稱

  .FaceId = 100 ’命令的FaceId

  .Visible = True ’可見

  .OnAction = "MySub" ‘指定響應過程名

  End With

  End Sub

  Sub MySub()

  MsgBox "It’s A Test For CommandBars(""Text"")!", vbOKOnly + vbInformation

  End Sub

  Sub ComReset() ‘重新設置右鍵菜單,徹底恢復默認設置

  Application.CommandBars("Text").Reset

  End Sub

  生成具有Commandbars(“Toolbar list”)或者當于CommandBars("View").Controls("工具欄(&T)")中的命令按鈕形式:

  Private Sub Document_Close()

  On Error Resume Next

  Application.CommandBars("Text").Controls("New Menu").Delete ’恢復原有菜單

  End Sub

  Private Sub Document_Open()

  Dim i As Byte, Half As Byte, strName As String, NewButton As CommandBarPopup

  Dim MenuAdd As CommandBarButton

  On Error Resume Next

  Application.CommandBars("Text").Controls("New Menu").Delete ‘預防性刪除

  Half = Int(Application.CommandBars("Text").Controls.Count / 2) ‘中間位置

  Set NewButton = Application.CommandBars("Text").Controls.Add(Type:=msoControlPopup, Before:=Half)

  With NewButton ’這是彈出式菜單即右邊帶有小三角型的

  .Caption = "New Menu" ’命令名稱

  .Visible = True ‘可見

  End With

  For i = 1 To 4 ’新建四個子命令,批量生成

  strName = "Menu" & i

  Set MenuAdd = NewButton.Controls.Add(Type:=msoControlButton)

  With MenuAdd

  .Caption = strName

  .OnAction = "MySub"

  .State = msoButtonDown ‘帶勾選的命令按鈕

  .Visible = True

  End With

  Next

  End Sub

  Sub MySub()

  Dim ActionTag As String

  ActionCap = CommandBars.ActionControl.Caption

  MsgBox ActionCap

  Select Case ActionTag

  ’以此來區分各個命令并執行指定過程

  End Select

  With Application.CommandBars("Text").Controls("New Menu")

  If .Controls(ActionCap).State = msoButtonDown Then

  MsgBox "It’s A Test!", vbOKOnly + vbInformation

  .Controls(ActionCap).State = msoButtonUp

  Else

  .Controls(ActionCap).State = msoButtonDown

  End If

  End With

  End Sub

  Sub ComReset() ‘重新設置右鍵菜單,徹底恢復默認設置

  Application.CommandBars("Text").Reset

  End Sub

  以下為禁用命令和快捷鍵的常用方式與保存路徑,提倡使用修改WORD命令更方便。

  Sub Example()

  ‘將自定義菜單欄工具欄或者自定義鍵盤的改變保存于活動文檔中

  Application.CustomizationContext = ActiveDocument

  ‘利用CommandBars(Name).Controls(Caption)來定位按鈕,具有唯一性

  Application.CommandBars("Standard").Controls("打開(&O)...").Enabled = False ‘TRUE

  ‘ 利用來定位按鈕,不太直觀,容易受調整后的命令位置干擾

  Application.CommandBars("Standard").Controls(2).Enabled = True ‘False

  ‘利用Findcontrol(ID:=)來定位按鈕,具有唯一性,并可循環,作用多個此按鈕命令

  Application.CommandBars.FindControl(ID:=23).Enabled = True ‘False

  ‘利用CommandBars(Index).Controls(Index)來定位按鈕,直觀,但受調整后的命令位置干擾

  Application.CommandBars(1).Controls(2).Enabled = False ‘True

  End Sub

  Sub FileOpen() ‘可以將命令與快捷鍵一并禁用

  MsgBox "這是修改WORD命令/打開文件"

  End Sub

  Sub Sample() ’將 CTRL+O快捷鍵重新分配或者修改并保存于當前文檔中

  CustomizationContext = ActiveDocument

  KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyO), _

  KeyCategory:=wdKeyCategoryMacro, Command:="NoFileOpen"

  End Sub

  Sub NoFileOpen()

  MsgBox "This is only a test!"

  End Sub

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奉节县| 文安县| 怀宁县| 武乡县| 都匀市| 城步| 罗平县| 桐柏县| 宜川县| 沙雅县| 鄂托克旗| 湖州市| 横峰县| 黄石市| 广饶县| 龙胜| 余干县| 观塘区| 贵定县| 额尔古纳市| 布尔津县| 英超| 怀集县| 安泽县| 鹤壁市| 临江市| 永吉县| 兴山县| 浦县| 城口县| 涟水县| 桦甸市| 菏泽市| 正蓝旗| 康保县| 阆中市| 贺兰县| 云和县| 永修县| 南安市| 云和县|