DocumentBeforeClose

C

che

Hi there,

I am using Word 2003 and visual studio 2008 to create a document manager via
automation.

I have been able to trap the DocumentBeforeClose event. I am doing this to
save the active document as a blob file to the database before the document
is closed. The DocumentBeforeClose event is hit successfully, but when right
before my SaveBlob procedure, my entire application hangs for a couple of
seconds and I get a "Microsoft Office Word has encountered a problem and
needs to close. We are sorry for the inconvenience." error. On clicking
"Don't Send", the document I had been working on re-opens in a new instance
of Word in a completely seperate window to my application. The inner
exception information is not useful at all.

Here is a code sample of what i'm trying to do:

wdEvents = DirectCast(wd, Word.ApplicationEvents4_Event)
AddHandler wdEvents.DocumentBeforeClose, AddressOf SaveBlob

'I am clicking on the x at the document level in word to catch this event.

Private Sub SaveBlob ()
If Not (CurrentDoc) Is Nothing Then
MsgBox("Closing the Window. Your current doc will be saved. ")

'Error occurs here, before save doc is called.

SaveDoc()
CloseCurrentDoc()
End If
End Sub

Any help/suggestions would be muchly appreciated.

Thanks :)
 
C

Cindy M.

Hi =?Utf-8?B?Y2hl?=,
I have been able to trap the DocumentBeforeClose event. I am doing this to
save the active document as a blob file to the database before the document
is closed. The DocumentBeforeClose event is hit successfully, but when right
before my SaveBlob procedure, my entire application hangs for a couple of
seconds and I get a "Microsoft Office Word has encountered a problem and
needs to close. We are sorry for the inconvenience." error. On clicking
"Don't Send", the document I had been working on re-opens in a new instance
of Word in a completely seperate window to my application. The inner
exception information is not useful at all.

Here is a code sample of what i'm trying to do:

wdEvents = DirectCast(wd, Word.ApplicationEvents4_Event)
AddHandler wdEvents.DocumentBeforeClose, AddressOf SaveBlob

'I am clicking on the x at the document level in word to catch this event.

Private Sub SaveBlob ()
Well, I don't know if this is THE reason, but your method signature is
certainly incorrect for the DocumentBeforeClose event. It should have two
parameters, one of which is the document being closed.

DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
C

che

Thanks Cindy,

Could you tell me where in my code I'd put those parameters? When I add the
handler to the DocumentBeforeClose event, it won't allow me to add
parameters. So I'm not too sure where to put them. The way I am exposing the
word events is the only way I know how. If you know of a better way, I'd
really appreciate the help!
 
C

che

Not to worry Cindy,

I realised that I had not declared the word object withevents. :(

Now that I have done that, all the events are exposed in the designer for me
:)
 
C

che

Argh.

Ok, so after finding the correct event, it still throws the same error.

Private Sub wd_DocumentBeforeClose(ByVal Doc As
Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles
wd.DocumentBeforeClose
SaveDoc() 'Error occurs here
CloseCurrentDoc()
End Sub


Any ideas??
 
C

che

Hi there,

Could anyone direct me to another forum, perhaps where I could get further
assistance with this problem? I've hunted on the internet, but to no avail :(
and a deadline is looming.

TIA.
 
C

Cindy M.

Ok, so after finding the correct event, it still throws the same error.
Private Sub wd_DocumentBeforeClose(ByVal Doc As
Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles
wd.DocumentBeforeClose
SaveDoc() 'Error occurs here
CloseCurrentDoc()
End Sub
Ok, so what does SaveDoc do?

I'm pretty sure the problem is in the SaveDoc code, if you step through
from this call into that procedure you should hit the actual line causing
the problem.

Also, looking at the little bit you give us, I'm thinking you should be
setting the Cancel parameter to False at the top of this procedure to stop
Word from doing whatever it wants to do.

Also please note that there's the possibility that your event is
conflicting with some other event (possibly from an add-in).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

che

Thanks Cindy,

Previously when I debugged the SaveDoc sub, the error occurred on the call
to the SaveDoc sub itself. (Which obviously doesn't make sense.) Since last
week, I have found another strange error, which I believe to be a microsoft
bug (according to a few other forums). Some people never found a fix, and the
bug just "went away" for others. This new error seemed to have "fixed" the
previous error - i.e. the SaveDoc sub now runs until it gets to the following
line:

I close the file, save the file as a blob file (which works perfectly) and
then open it again:

myDoc = wd.Documents.Open(fileName, objMissing, False, objMissing,
objMissing, objMissing, objMissing, objMissing, objMissing, objMissing,
objMissing, True)

The error thrown has an error number of 4198, with a message of "Command
Failed". Previously, this code was working perfectly, so now i'm really not
sure if it's a ripple effect of something else I've done or if it is in fact
one of those "ghost" bugs.

Just in case - here is the whole SaveDoc() sub:

Public Sub SaveDoc()

Dim img As Byte()
myDB = New DataManip
Try
Dim fileName As String = CurrentDoc.DocumentPath &
CurrentDoc.DocumentName & CurrentDoc.Extension
'Saves to Temporay Location
myDoc.SaveAs(fileName)
myDoc.Close(False, objMissing, objMissing)
img = ConvertToByte()
myDB.UpdateDocument(CurrentDoc.DocumentID, img,
CurrentDoc.DocumentName, CurrentDoc.DocumentPath, CurrentDoc.Extension,
CurrentDoc.IsTemplate, CurrentDoc.IsInDatabase,
myDB.GetUserID(Environment.UserName))
myDoc = New Word.Document
myDoc = wd.Documents.Open(fileName, objMissing, False, objMissing,
objMissing, objMissing, objMissing, objMissing, objMissing, objMissing,
objMissing, True, objMissing, objMissing, objMissing, objMissing) 'ERROR
OCCURS ON THIS LINE

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

TIA.
 
C

Cindy M.

Hi =?Utf-8?B?Y2hl?=,
myDB.UpdateDocument(CurrentDoc.DocumentID, img,
CurrentDoc.DocumentName, CurrentDoc.DocumentPath, CurrentDoc.Extension,
CurrentDoc.IsTemplate, CurrentDoc.IsInDatabase,
myDB.GetUserID(Environment.UserName))
myDoc = New Word.Document
myDoc = wd.Documents.Open(fileName, objMissing, False, objMissing,
objMissing, objMissing, objMissing, objMissing, objMissing, objMissing,
objMissing, True, objMissing, objMissing, objMissing, objMissing) 'ERROR
OCCURS ON THIS LINE
Well, you have some odd stuff here.

1. You don't tell us what UpdateDocument does, so it's not possible to
determine whether anything here might be affecting things

2. NEVER, EVER use the "New" keyword when working with Office object models.
You canNOT instantiate internal Office classes. This is probably what's
causing the problem. To say nothing of the fact that you're already using an
object "myDoc" at the top of the procedure and are never setting it to
"null". Strike this line out of your code, completely, and see if anything
changes.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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