4

Outlook 2010 추가 기능을 개발해야하며 대부분 PHP와 JavaScript를 사용하므로 Visual Studio와 C#을 처음 사용합니다. Visual Studio 2010을 사용하고 있으며 기본 제공 Outlook 2010 추가 기능 템플릿을 사용하여 프로젝트를 만들었습니다. folowing 코드를 살펴 보자 :Outlook 2010 addin C# 공개 메서드

// file ThisAddIn.cs 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml.Linq; 
using Outlook = Microsoft.Office.Interop.Outlook; 
using Office = Microsoft.Office.Core; 


namespace OutlookAddIn1 
{ 
    public partial class ThisAddIn 
    { 
     private void ThisAddIn_Startup(object sender, System.EventArgs e) 
     { 
     } 

     private void ThisAddIn_Shutdown(object sender, System.EventArgs e) 
     { 
     } 

     public string displayCount() 
     { 
      Outlook.MAPIFolder inbox = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); 

      Outlook.Items unreadItems = inbox.Items.Restrict("[Unread]=true"); 

      return string.Format("Unread items in Inbox = {0}", unreadItems.Count); 
     } 

     #region VSTO generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InternalStartup() 
     { 
      this.Startup += new System.EventHandler(ThisAddIn_Startup); 
      this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); 
     } 

     #endregion 
    } 
} 

// file Ribbon1.cs 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Office.Tools.Ribbon; 

namespace OutlookAddIn1 
{ 
    public partial class Ribbon1 
    { 
     private void Ribbon1_Load(object sender, RibbonUIEventArgs e) 
     { 

     } 

     private void button1_Click(object sender, RibbonControlEventArgs e) 
     { 
      // call ThisAddIn.displayCount() here 
     } 
    } 
} 

문제, 어떻게 다른 곳 Ribbon1 클래스의 ThisAddIn 클래스의 public 메소드를 호출하거나 할 수있다? 나는 객체 참조가 필요하다는 것을 알고 있지만 인스턴스의 이름을 어떻게 알 수 있습니까? ThisAddIn 인스턴스가 기존 파일의 어느 곳에서나 생성되는 것을 볼 수 없습니다. 아니면 개념을 오해하고 다른 방법으로 수행해야합니까? Office 어드벤스를 만드는 방법에 대한 조언이나 링크를 보내 주시면 감사하겠습니다.

답변

3

추가 기능 초기화시 설정되는 정적 멤버 변수 (관련 정적 게터 포함)를 사용합니다. 코드베이스의 어느 곳에서나 Core (적절한 이름 선택)으로 액세스 할 수 있습니다. 물론, 추가 기능 객체가 문맥에서 사용 가능하다면 전달하려고 시도하지만 때로는 수행하기가 어렵습니다.

이 클래스는 추가 기능 컨테이너/로더에 의해 자동으로 인스턴스화됩니다 (COM 구성 요소로 실제로 노출됨, 적어도 이것이 ADX에서 작동하는 방식 임).

해피 코딩.


코드는 다음과 같을 수 있습니다 VSTO 프로젝트에서

// inside ThisAddIn class 
public static ThisAddIn Active { 
    get; 
    private set; 
} 

// inside ThisAddIn_Startup 
Active = this; 

// later on, after add-in initialization, say in Ribbon1.button1_Click 
ThisAddIn.Active.displayCount(); 
+0

코드 샘플을 제공해 주시겠습니까? – noname

6

Globals라는 자동 생성 봉인 된 클래스는 어디 프로젝트 내에서 사용할 수 있습니다. Globals에는 여러 개의 공개 또는 내부 정적 속성이 포함되며 그 중 하나는 ThisAddIn (적절하게 유형이 ThisAddIn)입니다. 위 코드 대신 다음과 같이 코드가 표시됩니다. Ribbon1.cs에서

: 도움이

public void DoSomethingOnRibbon(Office.IRibbonControl control) 
{ 
    string count = Globals.ThisAddIn.displayCount(); 
    ... 
} 

희망.

관련 문제