View Single Post
  #6   Report Post  
Posted to microsoft.public.word.tables
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Need enhanced table to text conversion

I should have realised that would happen as when the table is deleted, the
number of tables in the document changes (I developed the code working on a
document that contained just one table. The following code starts with the
last table in the document and works towards the first and will overcome
that problem:

Dim i As Long, m As Long, n As Long
Dim crange As Range, target As Range
With ActiveDocument
For i = .Tables.Count To 1 Step - 1
With .Tables(i)
Set target = .Range.Duplicate
target.Collapse wdCollapseEnd
Set crange = .Cell(1, 1).Range
crange.End = crange.End - 1
target.Text = crange.Text
For m = 1 To .Columns.Count
For n = 2 To .Rows.Count
Set crange = .Cell(n, m).Range
crange.End = crange.End - 1
target.Text = target.Text & vbCr & crange.Text
Next n
Next m
.Delete
End With
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
"Walter Briscoe" wrote in message
...
In message of Sat, 6 Jun 2009
21:11:17 in microsoft.public.word.tables, Doug Robbins - Word MVP
writes

That is the sort of thing I had in mind. I start with 39 tables. It
converts the first and then fails , but leaves me with 28! It then
converts the second and fails. It then does nothing more. I am not sure
the logic is correct. The first through the loop works on Tables(1) and
the number of tables is reduced by 1. The next time, Tables(2) used to
be Tables(3), etc. I tried For i= Tables.Count to 1, but that also
failed 5941 The requested member of the collection does not exist in Set
crange = .Cell(1, 1).Range.
My suspicion is that the data is not quite what I think.

Before seeing your work, I tried:
For Each atable In ActiveDocument.Tables
If atable.Columns.Count = 4 Then
For i = 1 To 4
Selection.MoveRight Unit:=wdCell
Selection.MoveDown Unit:=wdLine, Extend:=wdExtend
Selection.Cells.Merge
Next i
End If
Next atable

That did not seem to move onto the second table.

Use a macro containing the following code:

Dim i As Long, m As Long, n As Long
Dim crange As Range, target As Range
With ActiveDocument
For i = 1 To .Tables.Count
With .Tables(i)
Set target = .Range.Duplicate
target.Collapse wdCollapseEnd
Set crange = .Cell(1, 1).Range
crange.End = crange.End - 1
target.Text = crange.Text
For m = 1 To .Columns.Count
For n = 2 To .Rows.Count
Set crange = .Cell(n, m).Range
crange.End = crange.End - 1
target.Text = target.Text & vbCr & crange.Text
Next n
Next m
.Delete
End With
Next i
End With




--
Walter Briscoe