mcnamaragio

Hello,
I want to use the close event of document object but the thing is that it is still fired even when the user clicks on the close button but then chooses cancel when the prompt is displayed. The microsoft.office.tools.document class has shutdown event that I'd like to use but it is not possible to use this class from another application. What can I do should I create a component using VSTO and then use it from my app If yes how I want to open a word file from my application and then detect when it is closed. Also, the document class has got many collections like these: tables,words,paragraphs,etc. Is it possible to store all these collections in other files or in sql table and then assemble the word document using them Thanks


Re: Visual Studio Tools for Office document close event.

Cindy Meister

A common approach is to maintain collection of open documents. When the event has taken place, check the open documents against the collection. If the document is still there, the user cancelled.

To detect whether a file is actually closing, there is the DocumentBeforeClose event.

If we're talking Word 2003, then you can save ranges in their WordProcessingML (Word XML) format and put them into a document using the InsertXML method.






Re: Visual Studio Tools for Office document close event.

mcnamaragio

How can I save ranges as Word XML and how do I persist information about location and sequence of these ranges Also, when the DocumentBeforeClose event is fired, is the document already saved to disk so that if my program manipulates with it, I want to make sure that if changes where made to the file, I'm working with a file which is changed. Also, If a person makes changes to file and saves it using "save as" command, then the prompt is not displayed when closing the document and can I retrieve the path that was used during "save as" command Thanks





Re: Visual Studio Tools for Office document close event.

Cindy Meister

Use the XML property of the Range to get the XML. You have to figure out how to persist information about location and sequence. There's nothing in Word's object model that can help you with that. If this is a relational database, then you can work out a system that saves all this in a table + look-up tables.

When DocumentBeforeClose is fired it will close if there are no pending changes to be saved; if there are changes pending, the prompt to save will be displayed after the event is fired and the user can choose whether or not to save changes, or to cancel the Close action. If the user chooses to save, then the DocumentBeforeSave event will be fired, otherwise not. If the document has never been saved before, after the DocumentBeforeSave event has fired, the File/Save As dialog box will be displayed. At this point the user can still cancel (and the document will be neither saved nor closed).

If you want to control what is done during "Save As" then you'll need to use the parameters in the DocumentBeforeSave event. Set SaveUI to false. Then you're responsible for displaying the SaveAs dialog box, using the Dialogs(index).Show method. This method returns an INTEGER that indicates which button the user clicked. Cancel always = 0; you'll need to test what the other buttons return, I haven't memorized the values. You can get the filename from the Dialog box. See these messages for examples

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=445510&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1111943&SiteID=1

As your questions really aren't about the VSTO technology, please post any follow-up questions in the office.developer.automation newsgroup.






Re: Visual Studio Tools for Office document close event.

mcnamaragio

The fact is that I want to catch the time when the changed document is already saved to disk. Neither documentbeforeclose nor documentbeforesave do this. What can I do I know that I will be working with the document that already exists so if I set SaveUI to false nothing changes. I want to find what the user clicked on the prompt. And then I want some part of my to execute when the changes have been saved to disk. how can I do that thanks




Re: Visual Studio Tools for Office document close event.

Cindy Meister

The .NET Framework Library has classes you can use to "watch" the activity in a folder (when files are created or changed). I suggest you use that (you need to ask in a different forum for help with that. Best is probably the one for base class libraries).




Re: Visual Studio Tools for Office document close event.

mcnamaragio

Is there a way I can control the save changes prompt that is displayed when the documents is closing Can I show it programatically and retrieve user's action Thanks




Re: Visual Studio Tools for Office document close event.

Cindy Meister

No, you can't intercept the prompt. But you can evaluate the user's response and take action:

- If he chooses to cancel, the document is still open

- If he chooses to Save, the DocumentBeforeSave event is triggered

- If he chooses to NOT save (or the prompt wasn't displayed because there is nothing to save), then the document is closed (unless you're cancelling that action in the DocumentBeforeClose event). Remember that you can query Doc.Saved in DocumentBeforeClose to determine if there are any changes pending.