2013-01-03 5 views
1

추가 기능 탭 이름이 OPENISMS 인 Microsoft Outlook 추가 기능을 개발 중입니다. 나는 버튼을 볼 수 있었지만 클릭하면 이벤트가 시작되지 않습니다. 나는 이것이 왜 이런 식으로 행동하는지 전혀 모른다. 버튼을 추가하고 이벤트를 추가하는 코드는 다음과 같습니다. 어떤 도움을 주시면 감사하겠습니다.사용자 지정 단추를 클릭 할 때 Outlook 이벤트가 실행되지 않습니다.

private void AddButtonToNewDropdown() 
{ 
    Office.CommandBar commandBar = this.Application.ActiveExplorer().CommandBars["Standard"]; 
    Office.CommandBarControl ctl = commandBar.Controls["&New"]; 
    if (ctl is Office.CommandBarPopup) 
    { 
     Office.CommandBarButton commandBarButton; 
     Office.CommandBarPopup newpopup = (Office.CommandBarPopup)ctl; 
     commandBarButton = (Office.CommandBarButton)newpopup.Controls.Add(1, missing, missing, missing, true); 
     commandBarButton.Caption = "OpenISMS"; 
     commandBarButton.Tag = "OpenISMS"; 
     commandBarButton.FaceId = 6000; 
     //commandBarButton.Enabled = false; 
         commandBarButton.OnAction = "OpenISMSThruMail.ThisAddIn.ContextMenuItemClicked"; 
     commandBarButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ContextMenuItemClicked); 
    } 

} 
private void ContextMenuItemClicked(CommandBarButton Ctrl, ref bool CancelDefault) 
{ 
    if (currentExplorer.Selection.Count > 0) 
    { 
     object selObject = currentExplorer.Selection[1]; 
     if (selObject is MailItem) 
     { 
      // do your stuff with the selected message here 
      MailItem mail = selObject as MailItem; 
      MessageBox.Show("Message Subject: " + mail.Subject); 
     } 
    } 
} 

나는 ThisAddIn_Startup 이벤트에서 AddButtonToNewDropdown() 메소드를 호출하고 있습니다.

답변

4

범위 멤버 변수를 CommandBarButton으로 만들어야합니다. 그렇지 않으면 가비지 수집되고 관찰 한대로 이벤트가 실행되지 않습니다.

public class ThisAddIn 
{ 
    Office.CommandBarButton commandBarButton; 

    private void AddButtonToNewDropdown() 
    { 
    // ... 
    } 
} 

related SO post regarding similar issue를 참조하십시오.

+0

대단히 기쁩니다. 도움이되었습니다. 이것을 다른 사람들이 유익 할 수 있도록 허용 된 대답으로 표시하십시오. 투표 화살표 아래 왼쪽의 틱 (* 확인 표시 *)을 클릭하기 만하면됩니다. – SliverNinja

관련 문제