Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Table but keep Content: Word 2003/2007

Status
Not open for further replies.

frankki

Technical User
Jul 4, 2006
10
EU
Hello Champs

Is there a way to remove tables from a word document but keeping the content of each row by using VBA?

The Table isnt complext, simple rows (one coloumn) only.
Number of rows varies from 1-10 per table, multiple tables.

Problem: I cant use copy/paste plain text, as the content contains encoded URLs displayed as textlink.

Any hint?
 
Code:
Dim oTable As Word.Table
For Each oTable In ActiveDocument.Tables
    oTable.Rows.ConvertToText Separator:=wdSeparateByParagraphs, _
        NestedTables:=True
Next
Will convert every table in the document to text, with each row separated by a paragraph mark.

Hyperlinks are retained exactly as they are. They are not affected in any way.

Gerry
My paintings and sculpture
 
Thanks again for this one!
It really works incredebly fast!

Do you also know a method to delete the last two coloumns of the table prior removing the table and converting the containing text as per your above sample?
 
Yes. You are actioning each table, so simply delete the last two rows of that table. Use the count of the rows. Do it before you convert.
Code:
Dim oTable As Word.Table
Dim j As Long
For Each oTable In ActiveDocument.Tables
    j = oTable.Rows.Count
    oTable.Rows(j).Delete
    oTable.Rows(j - 1).Delete
    oTable.Rows.ConvertToText Separator:=wdSeparateByParagraphs, _
        NestedTables:=True
Next
Note that action like this is similar to actioning any Collection - you work backwards. That is, you delete the last row first.

There are, like many things in VBA, alternative ways to do this. You could select the last two rows, then delete. You could make a Range of the last two rows, then delete. However, I think using the row count is probably the easiest.

Gerry
My paintings and sculpture
 



frankki,

Again, using your macro recorder would have given you a start on this one too.

Skip,

[glasses] [red][/red]
[tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top