Word Crashes after Catching DocumentBeforeSave Event

H

Haim

I have an application which opens a mailmerge document, merges the
document and saves the document to a database. It catches word's
DocumentBeforeSave event saves the file and then copies the file to the
database. Everything works as should but after saveing the document to
the database, word crashes. The code is vb.net using the officeXP
interops on a win2000 machine and office XP. (I've tried with office
2003 and it also crashes). Below is the code:

Private Sub myapp_ApplicationEvents2_Event_DocumentBeforeSave(ByVal Doc
As Microsoft.Office.Interop.Word.Document, ByRef SaveAsUI As Boolean,
ByRef Cancel As Boolean) Handles myapp.DocumentBeforeSave
Try
Dim intresp As DialogResult

intresp = MessageBox.Show("Do you want to save this
document as correspondence?", "Warning", MessageBoxButtons.YesNo)
If intresp = DialogResult.Yes Then
saveflag = doctype.Nondatabase
Me.saveasCorrespondence(True) 'See subroutine below


saveflag = doctype.correspondence
Cancel = True
Catch mye As Exception
MessageBox.Show(mye.Message)
End Try
End Sub

Public Sub saveasCorrespondence(ByVal keepopen As Boolean)

Try
Me.g_myapp.ActiveDocument.Save()


'g_myapp is a class variable which is the word application. It will
open a new application if necessary
Dim mydoc As Word.DocumentClass = Me.g_myapp.ActiveDocument
Dim docname As String = mydoc.FullName
mydoc.Close()
'takes file and puts it in database, works fine
Dim myfs As New IO.FileStream(docname, FileMode.Open)
Dim myarray(myfs.Length) As Byte
myfs.Read(myarray, 0, myfs.Length)
myfs.Close()
Dim myrow As CorrespDS.CorrespondenceRow =
Me.CorrespDS1.Correspondence.Rows(0)
myrow.BeginEdit()
myrow.Documentfield = myarray
myrow.Appname = "Microsoft Word"
myrow.username = System.Environment.UserName
myrow.EndEdit()
Me.SDA_Correspondence.Update(Me.CorrespDS1.Correspondence)
If keepopen Then
'g_myapp.Documents.Open(docname)

End If
Catch mye As Exception
Messagebox.show (mye.message)
Finally


End Try

End Sub
 
P

Peter Huang

Hi Haim,

Which code line cause the word crash?
You may try to run the code in debug mode and use the F10 to run the code
line by line to locate the error.
Also it seems that the program procedure is somewhat strange.

When word document is opened there will be a reference pointed to it, [note
it as doc]
after we click the save button, the
ApplicationEvents2_Event_DocumentBeforeSave event will be fired and now the
file is still in the memory.
And you will call the save and then close the document, but in the
meanwhile the doc should not be closed, because the DocumentBeforeSave
means now the document is alive which may cause the crash.

So I think you may try not to close the document in the
ApplicationEvents2_Event_DocumentBeforeSave event to see if the problem
persists.


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.
 
H

Haim

Dear Peter,

Thank you for your answer. The code runs to completion, the crash
occurs in word after the code has completed. The document is saved in
the database. I tried it without the document.close line but it had no
effect on the error.

Haim
 
H

Haim

Peter,

In looking further into the problem I see you are correct. The closing
of the document is apparently what caused the crash. In the following code
Dim docname As String = mydoc.FullName
mydoc.Close()
Dim myfs As New IO.FileStream(docname, FileMode.Open)
I had to close the file inorder to open it with the file stream. Even
if I reopen the file after disposing of the filestream I still get the
crash. The following code seems to have worked around the problem:

Dim docname As String = mydoc.FullName
'mydoc.Close()
'try to keep open
Me.g_myapp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
mydoc.SaveAs("c:\Temp\Temp.doc")
mydoc.SaveAs(docname)
Dim myfs As New IO.FileStream("c:\Temp\Temp.doc",
FileMode.Open, FileAccess.Read, FileShare.Read)
Is there a more elegant way to get the filestream directly from memory
instead of having to read it from the harddisk?

Haim
 
P

Peter Huang

Hi Haim,

I think so far we have no other method, because the word OM did not provide
the method that allowed us to save the word content into stream or memory.
Anyway, if you have any concern on your workaround, please feel free to let
me know.

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.
 
H

Haim

Peter,

Thank you again. So far everybody is happy with it and I haven't found
any problems.

Haim
 
P

Peter Huang

Hi Haim,

I am glad that problem has been resolved.

Cheers!

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.
 

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