Close event

A

Alex

In http://support.microsoft.com/?kbid=824022 Microsoft says:

+-------------------------------------------------------------------------------+
| Event | Description |
+-------------------------------------------------------------------------------+
| Close | Occurs when a document is closed. |
+-------------------------------------------------------------------------------+

However, I cannot find the Close event.

Is it a documentation error or am I missing something?


Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi Alex,

The Close Event is for Document Object.
So you may try to get the Document reference first and then add event for
it.
Public WithEvents doc As Microsoft.Office.Interop.Word.Document
Private Sub doc_Close() Handles doc.Close

End Sub

For C#, it seems that the Intellisense did not reflect the wrap correctly,
from the Object Browser, the Document Object will derive from public
abstract interface _Document and public abstract interface
DocumentEvents2_Event, the event is declare in the public abstract
interface DocumentEvents2_Event, so we can use the code below to attach the
event.

private void button1_Click(object sender, System.EventArgs e)
{
Word.Document doc = new Word.DocumentClass();
Word.DocumentEvents2_Event docEvent =doc as Word.DocumentEvents2_Event;
if (docEvent!=null)
docEvent.Close+=new
Microsoft.Office.Interop.Word.DocumentEvents2_CloseEventHandler(docEvent_Clo
se);
}

private void docEvent_Close()
{

}


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex

Hello Peter, thanks for your answer.

"Peter Huang" said:
Hi Alex,

The Close Event is for Document Object.
For C#, it seems that the Intellisense did not reflect the wrap correctly,
from the Object Browser, the Document Object will derive from public
abstract interface _Document and public abstract interface
DocumentEvents2_Event, the event is declare in the public abstract
interface DocumentEvents2_Event, so we can use the code below to attach the
event.

I am using the Word XP PIAs and there is no DocumentEvents2_Event.
There's DocumentEvents_Event though.

DocumentEvents_Event also contains New and Open events.
Can you tell me how are they used and how they differ from the DocumentOpen and NewDocument events in ApplicationEvents3_Event?


Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi

DocumentEvents_Event is at the Document level, but the ApplicationEvents3
is at the Application(the whole Word Application) level.
Here is a scenario we will use the DocumentEvents_Event.

Using Events with the Document Object
See AlsoSpecificsThe Document object supports several events that enable
you to respond to the state of a document. You write procedures to respond
to these events in the class module named "ThisDocument." Use the following
steps to create an event procedure.

Under your Normal project or document project in the Project Explorer
window, double-click ThisDocument. (In Folder view, ThisDocument is located
in the Microsoft Word Objects folder.)
Select Document from the Object drop-down list box.
Select an event from the Procedure drop-down list box.
An empty subroutine is added to the class module.

Add the Visual Basic instructions you want to run when the event occurs.
The following example shows a New event procedure in the Normal project
that will run when a new document based on the Normal template is created.

Private Sub Document_New()
MsgBox "New document was created"
End Sub
The following example shows a Close event procedure in a document project
that runs only when that document is closed.

Private Sub Document_Close()
MsgBox "Closing the document"
End Sub
Unlike auto macros, event procedures in the Normal template don't have a
global scope. For example, event procedures in the Normal template only
occur if the attached template is the Normal template.

If an auto macro exists in a document and the attached template, only the
auto macro stored in the document will execute. If an event procedure for a
document event exists in a document and its attached template, both event
procedures will run.

But the ApplicationEvents3 event is different, it will occur whenever a
document is opened or newed. And we common handle the Document event with
the application level in C# or other Addin/Automation code.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
E

Evan Stone

Thanks, Peter! This is EXACTLY what I just happened to be looking for right
now (funny how that works out), and it works great (with some minor
modifications to work with the version of the interface I'm using, which
you'll see below)!

However, the event seems to be triggered by the *invoking* of the document
closure, not the _actual_ close of the document itself. The problem I'm
running into is that if I handle the close event and null out my reference
to the document object, like so:

...

// hook up the close event
wordDocumentEvent = wordDocument as Word.DocumentEvents_Event;
if (wordDocumentEvent!=null)
{
wordDocumentEvent.Close += new
Word.DocumentEvents_CloseEventHandler(
HandleDocumentCloseEvent);
}

...

// handle the Document object's close event
private void HandleDocumentCloseEvent()
{
wordDocument = null;
}


....and the user clicks *cancel* at the dialog produced when the user clicks
the close button, then I've dumped my reference and can't perform any
actions on it if I need to. So I was just wondering.... Is there another
event that is exposed that I can prevent this situation, or am I perhaps
looking at the problem incorrectly?

Thanks!

evan stone | software engineer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top