2016-08-11 1 views
1

コンテキストメニューに特定のスクリプトを表示するスクリプトを作成しました。 プログラミングした後、コンテキストメニューには起動したいスクリプトが表示され、スクリプトは正常に実行されます。コンテキストメニューで特定のコマンドバーが閉じない

'SCRIPT POUR MENU CONTEXTUEL 
Sub AddToCellMenu() 
    Dim ContextMenu As CommandBar 
    Dim MySubMenu As CommandBarControl 

    ' Delete the controls first to avoid duplicates. 
    Call DeleteFromCellMenu 

    ' Set ContextMenu to the Cell context menu. 
    Set ContextMenu = Application.CommandBars("Cell") 

    ' Add one custom button to the Cell context menu "BL OK". 
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=20) 
     .OnAction = "'" & ThisWorkbook.Name & "'!" & "Bouton_BonLivraisonOK" 
     .FaceId = 71 
     .Caption = "Bon Livraison OK" 
     .Tag = "My_Cell_Control_Tag" 
    End With 

    ' Add one custom button to the Cell context menu "LIVRAISON OK". 
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=21) 
     .OnAction = "'" & ThisWorkbook.Name & "'!" & "Bouton_LivraisonOK" 
     .FaceId = 72 
     .Caption = "Expédition OK" 
     .Tag = "My_Cell_Control_Tag" 
    End With 

    ' Add one custom button to the Cell context menu "Livraison avec RELIQUAT". 
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=22) 
     .OnAction = "'" & ThisWorkbook.Name & "'!" & "Bouton_LivraisonReliquat" 
     .FaceId = 73 
     .Caption = "Expédition avec RELIQUAT" 
     .Tag = "My_Cell_Control_Tag" 
    End With 

    ' Add one custom button to the Cell context menu "RAZ". 
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=23) 
     .OnAction = "'" & ThisWorkbook.Name & "'!" & "Bouton_CellBlank" 
     .FaceId = 70 
     .Caption = "RAZ" 
     .Tag = "My_Cell_Control_Tag" 
    End With 

    ' Add a separator to the Cell context menu. 
    ContextMenu.Controls(20).BeginGroup = True 

End Sub 

問題は、コンテキストメニューがファイルを閉じた後も他のExcelファイルで実行されていることです。 他のExcelファイルを操作するときに、コンテキストメニューの関数を閉じるためにあなたの助けが必要です。

Sub DeleteFromCellMenu() 
    Dim ContextMenu As CommandBar 
    Dim ctrl As CommandBarControl 

    ' Set ContextMenu to the Cell context menu. 
    Set ContextMenu = Application.CommandBars("Cell") 

    ' Delete the custom controls with the Tag : My_Cell_Control_Tag. 
    For Each ctrl In ContextMenu.Controls 
     If ctrl.Tag = "My_Cell_Control_Tag" Then 
      ctrl.Delete 
     End If 
    Next ctrl 

    ' Delete the custom built-in Save button. 
    On Error Resume Next 
    ContextMenu.FindControl(ID:=3).Delete 
    On Error GoTo 0 
End Sub 

答えて

0

解決策が見つかりました。 ブックのCellMenuの開閉を宣言する必要があります。 次のコードを参照してください:

Private Sub Workbook_Activate() 
    Call AddToCellMenu 
End Sub 

Private Sub Workbook_Deactivate() 
    Call DeleteFromCellMenu 
End Subenter code here 
関連する問題