Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my vb.net application I am trying to count lines in word document in a folder and display line count in grid using following code. and when word file is corrupted or not readable then wants to display file name of word document in another grid. after exception error gives on folling line
dgvLineCount.Rows(i).Cells(1).Value = lc
Please help

VB
Dim iPos As Integer
        Dim iPosSave As Integer



        If AxOfficeViewer1.IsOpened Then
            AxOfficeViewer1.Close()
        End If
        dgvLineCount.Rows.Clear()
        For i As Integer = 0 To lstFiles.Items.Count - 1
           
            Dim strfn As String = ""

            Dim sPath As String = lstFiles.Items(i).ToString()

            iPos = 1
            Do
                iPos = InStr(iPos, sPath, "\")
                If iPos = 0 Then
                    Exit Do
                Else

                    iPos = iPos + 1
                    iPosSave = iPos - 1
                End If
            Loop

            strfn = Trim(Mid(sPath, iPosSave + 1))
            '****************************************
            Dim lc As Integer
            Try
                AxOfficeViewer1.Open(lstFiles.Items(i).ToString())
                Dim doc As Word.Document
                doc = AxOfficeViewer1.ActiveDocument
                lc = (doc.BuiltInDocumentProperties("NUMBER OF LINES").Value)
                dgvLineCount.Rows.Add()
                dgvLineCount.Rows(i).Cells(1).Value = lc
                dgvLineCount.Rows(i).Cells(0).Value = strfn
            Catch ex As Exception
                DataGridView1.Rows.Add()
                DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(0).Value = lstFiles.Items(i).ToString()

            End Try
            AxOfficeViewer1.Close()

        Next
Posted
Updated 7-Nov-11 23:52pm
v3
Comments
Smithers-Jones 8-Nov-11 5:51am    
Added code-block.
[no name] 8-Nov-11 6:30am    
Hi,
There are two for loops (Nested Loop) so in which loop are you getting an error?

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

VB
Private Sub processFiles()
    For Each file As String In lstfiles.items
        processFile(file)
    Next
End Sub

Private Sub processFile(FilePath As String)

    'Get the filename from the fullpath
    Dim filename As String = System.IO.Path.GetFileName(FilePath)
    Dim linecount As Integer


    Try
        'Open the file
        AxOfficeViewer1.Open(filename)

        'Read the file
        Dim doc As Word.Document
        doc = AxOfficeViewer1.ActiveDocument
        linecount = (doc.BuiltInDocumentProperties("NUMBER OF LINES").Value)

        'Record the linecount
        recordLC(filename, linecount)

    Catch ex As Exception
        'Record the error in the errors table
        recordError(filename, ex.Message)

    Finally
        'Make sure the OfficeViewer is closed
        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
 
Share this answer
 
Comments
anjali2 8-Nov-11 6:52am    
Thanks it works for me
Write TRY..CATCH block in loop. after exception in CATCH give continue or Next.
 
Share this answer
 
If you don't want to go out of a loop when an Exception occurs you should simply Catch the Exception in your Loop, handle it, and continue.
For example:
VB
' A Method that takes a collection as argument.
' The IEnumerable is only for the example. In real world apps I suggest you use IEnumerable(Of T).
Private Sub DoSomething(ByVal collection As IEnumerable)
   ' Loop through the collection and keep a counter.
   Dim counter As Integer = 0
   For Each obj In collection
      Try
         ' Do some code to handle the current Object.
         ' Increment the counter. Obviously this will not happen if above code raises Exception.
         counter += 1
      Catch ex As Exception
         ' Handle your Exception
         ' Still increment your counter.
         counter += 1
      End Try
   Next ' simply continue with the next item in the collection, even if the previous raised an Exception.
   ' Do something with your counter here.
   ' You could optionally put this in a Finally block.
End Sub
For more information on proper use of Try Catch Finally I can suggest you read my own article Using Try... Catch..., Finally![^].
I am not sure what you are trying to achieve, but I think this should help you.
Good luck! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900