2012-06-05 2 views
0

파일을 열 때 파일을 변경하는 단어 추가 기능으로 응용 프로그램을 만들고 싶습니다.열린 단어 파일 이벤트 잡기

그래서 나는 Visual Studio에서 단어 추가 기능 프로젝트를 생성하고, 이것은 내가 가지고있는 기본적으로 코드입니다 : 당신이 빈 단어 응용 프로그램을 시작하는 경우

namespace WordAddIn1 
{ 
    public partial class ThisAddIn 
    { 
    private void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc) 
    { 
    MessageBox.Show("doc opened"); 
    // do my stuff 
    } 

    #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.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen); 
    } 

    #endregion 
} 
} 
문제가

, 이것은 잘 작동 (더블 클릭 word.exe)을 실행 한 다음 문서를 열지 만 응용 프로그램이라는 단어가 문서 열기와 함께 시작되면 (.doc 파일을 두 번 클릭하면) 문서가 열리지 않습니다.

답변

0

문서를 두 번 클릭하여 Word를 열면 DocumentOpen이 실행되지 않습니다.

이 문제를 해결하려면 Word에서 문서가 열려 있는지 확인한 다음 문서를 Application_DocumentOpen 메서드에 전달하십시오.

BTW - InternalStartup 메서드에서 코드를 변경 한 것 같습니다. 주석에 명시된대로이 작업을 수행해서는 안되며 대신 ThisAddIn_Startup을 사용합니다. 여기에 수행하는 코드는 수도원이 무엇을 제안

0

입니다 :

private void ThisAddIn_Startup(object sender, System.EventArgs a) 
{ 
    try 
    { 
    Word.Document doc = this.Application.ActiveDocument; 
    if (String.IsNullOrWhiteSpace(doc.Path)) 
    { 
     logger.Debug(String.Format("Word initialized with new document: {0}.", doc.FullName)); 
     ProcessNewDocument(doc); 
    } 
    else 
    { 
     logger.Debug(String.Format("Word initialized with existing document: {0}.", doc.FullName)); 
     ProcessOpenedDocument(doc); 
    } 
    } 
    catch (COMException e) 
    { 
    logger.Debug("No document loaded with word."); 
    } 
}