跟踪鼠标-单击MS-Word上的自定义插件

本文关键字:自定义 插件 MS-Word 鼠标 单击 跟踪 | 更新日期: 2023-09-27 17:53:42

我正在开发一个Word插件,在其中我必须跟踪单击事件上的每个用户控件(例如选项卡,按钮等),然后编写一些基于该事件的代码,如何使用c#做到这一点?

PS:更准确地说,用户控件包括功能区和功能区上的不同控件。

Thanks in Advance

跟踪鼠标-单击MS-Word上的自定义插件

添加对Word对象库的引用。要做到这一点,请遵循以下步骤:在"项目"菜单上单击"添加引用"。在COM选项卡上,找到Microsoft Word 11.0对象库

你可以通过这个链接MSWord

我还没有尝试过ms word,我用同样的方法在outlook上工作过。

private Word.ApplicationClass oWord;
private void button1_Click(object sender, System.EventArgs e)
{
    //===== Create a new document in Word ==============
    // Create an instance of Word and make it visible.
    oWord = new Word.ApplicationClass();
    oWord.Visible = true;
    // Local declarations.
    Word._Document oDoc;
    Object oMissing = System.Reflection.Missing.Value;
    // Add a new document.
    oDoc = oWord.Documents.Add(ref oMissing,ref oMissing,
        ref oMissing,ref oMissing); // Clean document
    // Add text to the new document.
    oDoc.Content.Text = "Handle Events for Microsoft Word Using C#.";
    oDoc = null;        
    //============ Set up the event handlers ===============
    oWord.DocumentBeforeClose += 
        new Word.ApplicationEvents3_DocumentBeforeCloseEventHandler( 
        oWord_DocumentBeforeClose );  
    oWord.DocumentBeforeSave += 
        new Word.ApplicationEvents3_DocumentBeforeSaveEventHandler( 
        oWord_DocumentBeforeSave );  
    oWord.DocumentChange += 
        new Word.ApplicationEvents3_DocumentChangeEventHandler( 
        oWord_DocumentChange );
    oWord.WindowBeforeDoubleClick += 
        new Word.ApplicationEvents3_WindowBeforeDoubleClickEventHandler( 
        oWord_WindowBeforeDoubleClick );
    oWord.WindowBeforeRightClick +=
        new Word.ApplicationEvents3_WindowBeforeRightClickEventHandler( 
        oWord_WindowBeforeRightClick );
}
// The event handlers.
private void oWord_DocumentBeforeClose(Word.Document doc, ref bool Cancel)
{
    Debug.WriteLine(
        "DocumentBeforeClose ( You are closing " + doc.Name + " )");
}
private void oWord_DocumentBeforeSave(Word.Document doc, ref bool SaveAsUI, ref bool Cancel)
{
    Debug.WriteLine(
        "DocumentBeforeSave ( You are saving " + doc.Name + " )");
}
private void oWord_DocumentChange()
{
    Debug.WriteLine("DocumentChange");
}
private void oWord_WindowBeforeDoubleClick(Word.Selection sel, ref bool Cancel)
{
    Debug.WriteLine(
        "WindowBeforeDoubleClick (Selection is: " + sel.Text + " )");
}
private void oWord_WindowBeforeRightClick(Word.Selection sel, ref bool Cancel)
{
    Debug.WriteLine(
        "WindowBeforeRightClick (Selection is: " + sel.Text + " )");
}