Table in Word doc, ConvertToText VB NET

1

Been searching for an answer to this but everybody seems to want to do the opposite!

Using VB.NET code I want to convert all the tables in an open word doc to text but when I run the following code, I get the error

"'Microsoft.Office.Interop.Word.Table'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020951-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

Have I got the code wrong or is this not possible? I'm guessing I've got it wrong!

Dim objTable As Table = objDoc.Tables

If objTable.Rows.Count = 1 Or objTable.Columns.Count = 1 Then
    objTable.ConvertToText(Separator:=vbCr)
End If
vb.net
ms-word
asked on Stack Overflow Mar 5, 2019 by Francesca Elena Pieri • edited Mar 5, 2019 by barbsan

1 Answer

0

As indicated in a comment, the Tables collection doesn't have the method to convert tables to text, only the Table object (singular) has it. This means it's necessary to iterate (loop) the items in the collection. For example:

Dim objTables as Tables = objDoc.Tables
Dim objTable As Table
For each objTable in objTables
    If objTable.Rows.Count = 1 Or objTable.Columns.Count = 1 Then
        objTable.ConvertToText(Separator:=vbCr)
    End If
Next
answered on Stack Overflow Mar 5, 2019 by Cindy Meister • edited Mar 5, 2019 by Cindy Meister

User contributions licensed under CC BY-SA 3.0