Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying out the code below:

 

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 doesn't exist.

The program manage to display the data in the console but seems to have error before it terminates..

 

Please advise..

Thank you.

Posted

First:

'this should be without the New keyword, because you'll 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 you're not 100% sure, check wdXml.Tables.Count before proceeding.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...