2012-07-27 1 views
2

Windows 용 Microsoft Excel 2010을 사용하고 있습니다.Excel에서 추가 기능의 단일 메뉴 항목을 자동으로 추가합니다.

보조 main을 포함하는 추가 기능 addin.xlam을 이미 개발했습니다. addin.xlam이 올바른 위치에 있으므로 메뉴 Developer -> Add-Ins을 통해 표시되고 선택할 수 있습니다. 일반 워크 북 test.xlsm을 열고 Alt + F11을 누르면 addin.xlam 코드가로드 된 것을 볼 수 있습니다.

내 목표는 사용자가 mainadd-in.xlam을 시작할 수 있도록 Excel의 메뉴 모음에 단일 메뉴 항목을 추가하는 것입니다.

Option Explicit 
Dim cControl As CommandBarButtonPrivate 
Sub Workbook_AddinInstall() 
    On Error Resume Next 'Just in case 

    'Delete any existing menu item that may have been left. 
    Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete 

    'Add the new menu item and Set a CommandBarButton Variable to it 
    Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add 

    'Work with the Variable 
    With cControl 
     .Caption = "Super Code" 
     .Style = msoButtonCaption 
     .OnAction = "main" 'Macro stored in a Standard Module 
    End With 
    On Error GoTo 0 
End Sub 

Private Sub Workbook_AddinUninstall() 
    On Error Resume Next 'In case it has already gone. 
    Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete 
    On Error GoTo 0 
End Sub 

이 코드는 잘 addin.xlamThisWorkbook에 배치되고, 또한 test.xlsm에서 볼 수 있습니다 : 다음과 같이 link을 다음으로 addin.xlam 내 코드입니다. 그러나 메뉴 모음에서 변경 사항을 볼 수 없습니다.

아무도 모르게됩니까?

답변

4

AddinInstall 및 AddinUninstall 이벤트는 추가 기능이 Excel Addin Manager를 사용하여 "설치"또는 "제거"될 때만 발생합니다.

이럴 경우 문제가 발생할 수 있으므로 항상 Workbook_Open 및 Workbook_BeforeClose 이벤트를 대신 사용하는 것이 좋습니다.

+0

동의. 나는이 방법을 사용한다. –

0

Charles가 맞습니다. Workbook_AddinInstall()Workbook_Open()으로 대체하고 Workbook_AddinUninstall()Workbook_BeforeClose()으로 바꿔야합니다.

또한 CommandBarButton이 아닌 CommandBarButtonPrivate이 필요합니다.

행운을 빈다!

관련 문제