problem with a Word batch.

sinsys

New member
Joined
Sep 24, 2008
Messages
1
Programming Experience
1-3
Hi i'm busy with a application. This application is a batch of word documents needs to be opend and save it as a clean txt document. So I can open it in Richtextbox. But the problem is when I reach number 678 (something in this range) I get the problem that hole te application crash and vb.net gives no error. Even Try..catch..Endtry will not work.

Here's my code:
VB.NET:
  Private TextK As New TextKernel()
    Private Wordapp As New Word.Application()


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim miss As Object = System.Reflection.Missing.Value
        Dim objTrue As Object = True
        Dim objFalse As Object = False
        Dim errCount As Integer
        Dim i As Integer
        Dim doc As Word.Document
        For i = 0 To ListBox1.Items.Count - 1
            If ListBox1.Items(i).ToString.ToLower.Contains("cv") Then
                Try


                    Me.Text = "Status: " & i & "/" & ListBox1.Items.Count - 1 & " Errors : " & errCount

                    doc = Wordapp.Documents.Open(ListBox1.Items(i).ToString)
                    IO.File.Delete("c:\temp.txt")
                    doc.SaveAs("C:\temp.txt", Word.WdSaveFormat.wdFormatText)
                    doc.Close()

                    RichTextBox1.LoadFile("C:\temp.txt", RichTextBoxStreamType.PlainText)

                    Dim dat As TextKernel.GetDatafromFile = TextK.ExtractDatafromFile(ListBox1.Items(i).ToString)
                    Dim dat2 As TextKernel.GetDatafromstring = TextK.ExtractDatafromstring(RichTextBox1.Text)

                    RichTextBox2.Text = RichTextBox2.Text & dat.Voornaam & Chr(9) & dat.Tussenvoegsels & Chr(9) & dat.Achternaam _
                    & Chr(9) & dat2.Telefoon & Chr(9) & dat2.Geslacht & Chr(9) & dat2.Geboortedatum & Chr(9) & dat2.Email & Chr(10)

                    Application.DoEvents()
                Catch
                    errCount = errCount + 1
                End Try
            End If
            Application.DoEvents()
        Next
        MsgBox("Batch is compleet errors: " & errCount)
    End Sub

Is there any solution to this problem?
 
Last edited by a moderator:
VB.NET:
        Dim doc As Word.Document
<snip>
                    doc = Wordapp.Documents.Open(ListBox1.Items(i).ToString)
<snip>
                    doc.Close()
<snip>

Is there any solution to this problem?

I'm not sure if this could be the precise nature of the problem, however, i've noticed in the past and tried to help others with automation issues with Office, because the COM interfacing is not caught by the standard Garbage Collector, so each time you open a document, and then close it, the object still resides in the Marshalling Services memory, which can create a problem, i recommend after the doc.close() line to add in:
VB.NET:
COMRelease(doc)

that function is defined as:
VB.NET:
   Public Sub Free(ByRef O As Object)
      O = Nothing
      GC.Collect()
   End Sub

   Public Sub COMRelease(ByRef o As Object)
      Try
         System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
      Catch
      Finally
         Free(o)
      End Try
   End Sub

though those are jsut copy/paste from my libraries, but you probably don't need to forcibly call the GC.Collect() i just do to be sure. You might try that and see if it helps, though to be honest I can't be sure that's even the problem.
 
Back
Top