locked
Using VSTO C# to Listen for Document New Events RRS feed

  • Question

  • I've been looking through the answers here and am unsure if what I am looking to do is possible. I hope someone here can give me a solid "no" or point me through a walk-though I can use to learn the  process.

    I have written a C# Word COM Add-in in VS 2010 that modifies the ribbon and includes a few routines to help people clean up documents. I would like to add a listener to trap the DocumentNew event, logging the attached template of the new file. I have code to identify the attached template and log its use, but I'm having a hard time trapping the Document New event.

    Is trapping the DocumentNew event possible using a COM Add-in? If so, how?

    Many thanks.

    Tuesday, January 24, 2012 4:46 PM

Answers

  • Thanks. This is actually the nudge I was looking for. This is what I'm using:

     

     

     	private void ThisAddIn_Startup(object sender, System.EventArgs e)
    { Microsoft.Office.Interop.Word.ApplicationEvents2_Event wdEvents2 = (Microsoft.Office.Interop.Word.ApplicationEvents2_Event)this.Application; wdEvents2.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(wdEvents2_NewDocument); } void wdEvents2_NewDocument(Word.Document Doc) { MessageBox.Show("New Document Fires.", "New Document", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { ; }

    Does exactly what I need it to do.

     

    • Marked as answer by Calvin_Gao Thursday, February 9, 2012 8:56 AM
    Wednesday, January 25, 2012 5:48 PM
  • Hi Rex

    Is this a COM add-in or a VSTO add-in?

    The DocumentNew event is for a specific document, so is really only relevant with VBA in a document or a VSTO document customization.

    For application-level, look at the NewDocument event. If this is a COM Add-in and not a VSTO Add-in then, as I recall, you won't may not see the event in Intellisense, but it's there and does work.


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Calvin_Gao Wednesday, January 25, 2012 9:16 AM
    • Marked as answer by Calvin_Gao Thursday, February 9, 2012 8:56 AM
    Wednesday, January 25, 2012 6:03 AM

All replies

  • Hi Rex

    Is this a COM add-in or a VSTO add-in?

    The DocumentNew event is for a specific document, so is really only relevant with VBA in a document or a VSTO document customization.

    For application-level, look at the NewDocument event. If this is a COM Add-in and not a VSTO Add-in then, as I recall, you won't may not see the event in Intellisense, but it's there and does work.


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Calvin_Gao Wednesday, January 25, 2012 9:16 AM
    • Marked as answer by Calvin_Gao Thursday, February 9, 2012 8:56 AM
    Wednesday, January 25, 2012 6:03 AM
  • Thanks. This is actually the nudge I was looking for. This is what I'm using:

     

     

     	private void ThisAddIn_Startup(object sender, System.EventArgs e)
    { Microsoft.Office.Interop.Word.ApplicationEvents2_Event wdEvents2 = (Microsoft.Office.Interop.Word.ApplicationEvents2_Event)this.Application; wdEvents2.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(wdEvents2_NewDocument); } void wdEvents2_NewDocument(Word.Document Doc) { MessageBox.Show("New Document Fires.", "New Document", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { ; }

    Does exactly what I need it to do.

     

    • Marked as answer by Calvin_Gao Thursday, February 9, 2012 8:56 AM
    Wednesday, January 25, 2012 5:48 PM