code to convert all tables to text

J

Jack Sons

Hi all,

I tried to construct code to convert all tables in a document to text. See
the code below.
It does not work.

What is wrong and why? What should the code be?

Your help will be appreciated.

Jack Sons
The Netherlands
---------------------------------------------------------------------------------------------------------------------
Sub ConvertTablesToText()

Dim oShp As Table
Dim i As Integer
For Each oShp In ActiveDocument.Tables
If oShp.Type = msoTable Then oShp.Selection.Rows.ConvertToText
Separator:=wdSeparateByTabs, NestedTables:= True
Next oShp
For i = ActiveDocument.Tables.Count To 1 Step -1
With ActiveDocument.Tables(i)
.Borders.Enable = False
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
.Delete
End With
Next


End Sub
 
J

Jay Freedman

Hi Jack,

You seem to be under the mistaken impression that tables are somehow
related to Shape objects. They are not. In particular, a Table object
doesn't have a .Type property, so the expression in your code

If oShp.Type = msoTable Then

should cause a compiler error that prevents the macro from even
starting.

The code should be just this simple:

Sub demo()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
oTbl.ConvertToText _
Separator:=wdSeparateByTabs
Next
End Sub

You don't need the optional NestedTables parameters of the
ConvertToText method, because the value True is the default (look up
the ConvertToText method in the VBA help). The rest of the macro is
unnecessary because ConvertToText automatically removes any borders or
shading that had been applied to the table or its contents.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
J

Jack Sons

Jay,

A thousand thanks, not only for the code, but especially for your
explanation.

Jack.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top