尝试从有关 Word 加载项的 Microsoft 教程中了解代码的特定部分
Trying to understand a particular part of code from microsoft tutorials regarding Word Add-Ins
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.DocumentOpen +=
new Word.ApplicationEvents4_DocumentOpenEventHandler(WorkWithDocument);
((Word.ApplicationEvents4_Event)this.Application).NewDocument +=
new > Word.ApplicationEvents4_NewDocumentEventHandler(WorkWithDocument);
}
private void WorkWithDocument(Microsoft.Office.Interop.Word.Document Doc)
{
try
{
Word.Range rng = Doc.Range(0, 0);
rng.Text = "New Text";
rng.Select();
}
catch (Exception ex)
{
// Handle exception if for some reason the document is not available.
}
}
这是完整代码。据我了解,它应该启动加载项,检查文档是否可用。我难以理解的部分是:
((Word.ApplicationEvents4_Event)this.Application).NewDocument ...
我不明白的是this.Application
前面的(Word.ApplicationEvents4_Event)
。那是某种类似事件的类型转换吗?我不知道。
The documentation states 它是一个接口,因此代码将 this.Application
转换为该接口。
Application 的文档解释了这一点:
This is a .NET interface derived from a COM coclass that is required
by managed code for interoperability with the corresponding COM
object. Use this derived interface to access all method, property, and
event members of the COM object. However, if a method or event you
want to use shares the same name under the same COM object, cast to
the corresponding primary interface to call the method, and cast to
the latest events interface to connect to the event. Refer to this
topic for information about the COM object. For information about the
method and property members of the COM object, see _Application. For
information about the event members of the COM object, see
ApplicationEvents4_Event.
欧文回答的补充:
ApplicationEvents4_Event.NewDocument 事件
创建新文档时发生。
因此,您的 WorkWithDocument 方法将在打开文档(来自您的第一个事件订阅)以及创建新文档(来自您的第二个事件订阅)时被调用。
+= 只是订阅那些事件并告诉他们调用您的方法。
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.DocumentOpen +=
new Word.ApplicationEvents4_DocumentOpenEventHandler(WorkWithDocument);
((Word.ApplicationEvents4_Event)this.Application).NewDocument +=
new > Word.ApplicationEvents4_NewDocumentEventHandler(WorkWithDocument);
}
private void WorkWithDocument(Microsoft.Office.Interop.Word.Document Doc)
{
try
{
Word.Range rng = Doc.Range(0, 0);
rng.Text = "New Text";
rng.Select();
}
catch (Exception ex)
{
// Handle exception if for some reason the document is not available.
}
}
这是完整代码。据我了解,它应该启动加载项,检查文档是否可用。我难以理解的部分是:
((Word.ApplicationEvents4_Event)this.Application).NewDocument ...
我不明白的是this.Application
前面的(Word.ApplicationEvents4_Event)
。那是某种类似事件的类型转换吗?我不知道。
The documentation states 它是一个接口,因此代码将 this.Application
转换为该接口。
Application 的文档解释了这一点:
This is a .NET interface derived from a COM coclass that is required by managed code for interoperability with the corresponding COM object. Use this derived interface to access all method, property, and event members of the COM object. However, if a method or event you want to use shares the same name under the same COM object, cast to the corresponding primary interface to call the method, and cast to the latest events interface to connect to the event. Refer to this topic for information about the COM object. For information about the method and property members of the COM object, see _Application. For information about the event members of the COM object, see ApplicationEvents4_Event.
欧文回答的补充:
ApplicationEvents4_Event.NewDocument 事件
创建新文档时发生。
因此,您的 WorkWithDocument 方法将在打开文档(来自您的第一个事件订阅)以及创建新文档(来自您的第二个事件订阅)时被调用。
+= 只是订阅那些事件并告诉他们调用您的方法。