I would break up your code a bit, and use a
For Each
loop, also you can remove some of the code by using methods available in the
System.IO
namespace
Private Sub processFiles()
For Each file As String In lstfiles.items
processFile(file)
Next
End Sub
Private Sub processFile(FilePath As String)
Dim filename As String = System.IO.Path.GetFileName(FilePath)
Dim linecount As Integer
Try
AxOfficeViewer1.Open(filename)
Dim doc As Word.Document
doc = AxOfficeViewer1.ActiveDocument
linecount = (doc.BuiltInDocumentProperties("NUMBER OF LINES").Value)
recordLC(filename, linecount)
Catch ex As Exception
recordError(filename, ex.Message)
Finally
If AxOfficeViewer1.IsOpened Then
AxOfficeViewer1.Close()
End If
End Try
End Sub
Private Sub recordLC(filename As String, linecount As Integer)
dgvLinecount.Rows.Add({filename, linecount})
End Sub
Private Sub recordError(filename As String, message As String)
dgvErrors.Rows.Add({filename, message})
End Sub