Table in Word

DreamKid

Member
Joined
Apr 12, 2004
Messages
17
Im trying out the code below:

Code:
Dim waXml As New Word.Application
        Dim wdXml As New Word.Document

        Try
            wdXml = waXml.Documents.Open(strDocPath)

            Dim tblXml As Word.Table
            Dim rowXml As Word.Row
            Dim cllXml As Word.Cell
            Dim prXml As Word.Paragraph

            tblXml = wdXml.Tables.Item(1)
            For Each cllXml In tblXml.Range.Cells
                For Each prXml In cllXml.Range.Paragraphs
                    Console.WriteLine(prXml.Range.Text.ToString)
                Next
            Next

            tblXml = wdXml.Tables.Item(2)
            For Each cllXml In tblXml.Range.Cells
                For Each prXml In cllXml.Range.Paragraphs
                    Console.WriteLine(prXml.Range.Text.ToString)
                Next
            Next
        Catch ex As Exception
            MsgBox("Error: " & ex.ToString)
        Finally
            wdXml.Close()
            waXml.Quit()
        End Try

But an error occurs at:
tblXml = wdXml.Tables.Item(1)
Which said that my request collection doesnt exist.
The program manage to display the data in the console but seems to have error before it terminates..

Please advise..
Thank you.
 
First:
Code:
this should be without the New keyword, because youll be opening an existing doc:
Dim wdXml As New Word.Document
should be
Dim wdXml As Word.Document
Second: are you sure that the document has two tables? If youre not 100% sure, check wdXml.Tables.Count before proceeding.
 
Back
Top