KenMcCormack

From an application level add-in, can I make sure that this is attached to every document created / opened

From my add-in startup event I attach as follows:

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

this.Application.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);

}

Note the use of the ApplicationEvents4 (not supposed to be used by end user code! ). There is no event at this level for DocumentCreate.

And then in the event handler I CAST the document to the Tools namespace and attach the BeforeSave event....

// Attach beforesave to each document

void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document doc)

{

((Microsoft.Office.Tools.Word.Document)doc).BeforeSave += new SaveEventHandler(ThisAddIn_BeforeSave);

}

Nasty code for something that should be quite simple Anyone know what I'm missing

Ken



Re: Visual Studio Tools for Office Trap save event in application level add-ins

Cindy Meister

Hi Ken

If you set up a "listener" for an application-level event, it will trigger every time the application performs the action. So, DocumentOpen will trigger whenever a document is opened in Word.

Ditto for DocumentBeforeSave. You only need to create the listener once, in the Startup procedure. It will fire every time a document is saved. (Note that it will also fire when Word performs an "auto save" and there's no way to trap the difference.)

There is no DocumentCreate event, but you should have no trouble finding a DocumentNew event...






Re: Visual Studio Tools for Office Trap save event in application level add-ins

X4U

Hello Ken,

in addition to Cindy:

In Word you can have multiple documents opening an closing at any time.

So you have to take care about the diferent states of the documents.

Typically a wrapper class/object is used for this wich encapsulates the Word documets and give them a unique ID to identify the different documents.

Greets, Helmut






Re: Visual Studio Tools for Office Trap save event in application level add-ins

KenMcCormack

Thanks Cindy, esp for the heads up on AutoSave... think I will try another approach.





Re: Visual Studio Tools for Office Trap save event in application level add-ins

Cindy Meister

Which version(s) of Word are you targeting






Re: Visual Studio Tools for Office Trap save event in application level add-ins

KenMcCormack

Hi Cindy -

We're targeting Word 2007.

I think I've discovered how to do this... you can override the in-built Save action by adding a command to the ribbon Xml - Daniel Moth explains how to do this in his video on the Visual Studio 2008 ribbon designer (see last few mins of the video) http://channel9.msdn.com/Showpost.aspx postid=348631

<commands>

<command idMso="FileSave" onAction="MyFileSave" />

</commands>

That will call a method as follows in the ribbon.cs codebehind:

Public Sub MyFileSave(c As Office.IRbbonControl, ByRef cancel As Boolean)

msgbox("save command intercepted")

cancel = True

End Sub

Havent tried to do this in VS2005 yet...

BTW - that is a great video, anyone customising the Ribbon should check it out

Ken





Re: Visual Studio Tools for Office Trap save event in application level add-ins

Cindy Meister

Yep, that's where I was heading :-) But I didn't want to suggest it before finding out whether you had 2007 available.