2012-09-25 2 views
2

C#의 Microsoft Word 문서에 대한 DocumentBeforeClose 이벤트에 연결된 이벤트가 있습니다. 어떤 논리가 사실이라면MS Word 문서 닫기 이벤트가 취소되지 않습니다

this.Application.DocumentBeforeClose += 
       new MSWord.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose); 

, 내가 true로 취소 플래그를 설정 문서에 가까이하지 않도록. 그러나 이벤트가 발생하고 취소 플래그가 true로 설정되어 있지만 문서가 여전히 닫힙니다.

이것은 버그입니까?

+0

조금만 걸어 나가십시오. 논리적으로 "취소"를 false로 설정하면 "취소"기능이 작동해서는 안되며 true로 설정하면 제대로 작동합니다. 네가 한 말은 취소를 참으로 설정 한거야. – Steven

+1

@Steven 설정을 true로 취소한다는 것은 "예, 닫기 이벤트를 취소하십시오"를 의미합니다. Joe가 취소를 true로 설정했지만 문서가 닫히면 문제가있는 것처럼 보입니다. – phoog

+0

@phoog 아, 고맙습니다. – Steven

답변

2

나는 그것을 마침내 발견했습니다. 또한 실제 Word 문서 (Microsoft.Office.Tools.Word.Document) 개체에 이벤트 처리기를 연결해야했습니다.

this.Application.DocumentBeforeClose += new Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose); 

Application_DocumentBeforeClose(Interop.Word.Document document, ref bool Cancel) 
{ 
    // Documents is a list of the active Tools.Word.Document objects. 
    if (this.Documents.ContainsKey(document.FullName)) 
    { 
    // I set the tag to true to indicate I want to cancel. 
    this.Document[document.FullName].Tag = true; 
    } 
} 

public MyDocument() 
{ 
    // Tools.Office.Document object 
    doc.BeforeClose += new CancelEventHandler(WordDocument_BeforeClose); 
} 

private void WordDocument_BeforeClose(object sender, CancelEventArgs e) 
{ 
    Tools.Word.Document doc = sender as Tools.Word.Document; 

    // This is where I now check the tag I set. 
    bool? cancel = doc.Tag as bool?; 
    if (cancel == true) 
    { 
    e.Cancel = true; 
    } 
} 

그래서 논리가 응용 프로그램 클래스 코드에서 이루어집니다 내 응용 프로그램의 모든 때문에, 내가 필요로 내 MyDocument에 표시 할 수있는 방법을 (Tools.Word.Document 및 Interop.Word.Document은 ... 나에게 두통 제공) close 이벤트를 취소하려는 클래스 이벤트. 그렇기 때문에 태그 객체를 사용하여 플래그를 저장합니다.

관련 문제