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