跟踪鼠标 - 单击MS-Word以获得自定义的插件

时间:2016-06-30 04:44:37

标签: c# visual-studio ms-word mouseevent

我正在开发一个Word附加组件,我必须在每个用户控件上跟踪单击事件(例如Tabs,Buttons,.etc),然后根据它编写一些代码事件,如何使用C#?

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

先谢谢

1 个答案:

答案 0 :(得分:0)

添加对Word对象库的引用。为此,请按照下列步骤操作: 在“项目”菜单上,单击“添加引用”。 在COM选项卡上,找到Microsoft Word 11.0对象库

供参考您可以浏览此链接MSWord

我没有尝试使用ms语言我以同样的方式使用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 + " )");

}