Form freezes when looping through Word files to read tables
I need to read the tables contained in some Word files. I wrote the following code in vb.net:
Imports Microsoft.Office.Interop
....
Dim oWord As New Word.Application
Dim doc As Word.Document
Dim WordFile As String
Dim ListStr as List(Of(String, String))
oWord.Visible = False
oWord.ScreenUpdating = False
WordFile = Dir(.SelectedPath & "\*.docx")
Do While WordFile <> ""
doc = oWord.Documents.Open(FileName:= .SelectedPath & "\" & WordFile, ConfirmConversions:=False)
doc.ActiveWindow.Visible = False
numTab = doc.ActiveWindow.Document.Tables.Count
ListStr.Clear()
For i = 1 To numTab
Me.Label2.Text = "Table " & Trim(Str(i)) & " of " & Trim(Str(numTab))
For j = 2 To doc.ActiveWindow.Document.Tables(Conta1).Rows.Count
ListStr.Add((doc.ActiveWindow.Document.Tables(i).Cell(j, 0).Range.Text.ToString, doc.ActiveWindow.Document.Tables(i).Cell(j, 1).Range.Text.ToString))
Next j
Next i
......
doc.Close(False)
WordFile = Dir()
Loop
oWord.Application.Quit(False)
oWord = Nothing
Unfortunately the code is very slow at reading the tables and after a while the form freezes.