Accessing paragraphs in a table cell

J

Jay

Hi,

In Word 97, I have a table cell with (for example) 6 paragraphs in it.
When I do a count of the number of paragraphs using
Selection.Paragraphs.Count, it returns 6. When I try to access each
paragraph (see below) everything works fine except for the final
paragraph (number 6 in this case), where the VBA copies and pastes the
entire contents of the cell rather than the final paragraph. Perhaps
this is because Word doesn't consider the last paragraph to be a real
paragraph, since it ends with an end-of-cell marker rather than a
paragraph marker? How can I get around this problem? Perhaps I should
be using a more direct method of moving things around than using copy
and paste? Feel free to change my code!

NumPara = Selection.Paragraphs.Count
For Para = 1 To NumPara
Selection.Tables(1).Cell(1, 1).Select
Selection.Paragraphs(Para).Range.Select
Selection.Copy
...
'code to paste the paragraph somewhere else
Next

Thanks,

Jay
 
H

Helmut Weber

Hi Jay,

how about this one?

Sub Test312()
Dim l As Long
Dim x As Long
Dim r As Range
With ActiveDocument.Tables(1).Cell(1, 1)
l = .Range.Paragraphs.Count
For x = 1 To l - 1
.Range.Paragraphs(x).Range.Select ' for testing
.Range.Paragraphs(x).Range.Copy
' paste it where you like
Next
Set r = .Range.Paragraphs(x - 1).Range
r.Start = r.End
r.End = .Range.End - 1
r.Select ' for testing
r.Copy
' paste it where You like + vbcrlf
End With
End Sub

If formatting doesn't matter,
you don't have to copy and paste anything at all.

Just set a range in the doc to the actual range.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

Tony Jollans

You can see this via the Word UI - try and select the last paragraph of a
cell and it will select the whole cell.

You have a problem trying to fix this. You can just drop the end-of-cell
mark from the range to be copied but then you will lose all the paragraph
formatting. If you need the formatting I think the only way to do it is to
insert a paragraph mark at the end of the cell, copy all but the last
paragraph, and then delete the paragraph you added - actually deleting it
will have consequences, so undoing is better.

NumPara = Selection.Paragraphs.Count
Selection.Tables(1).Cell(1, 1)..Range.InsertParagraphAfter
For Para = 1 To NumPara
Selection.Tables(1).Cell(1, 1).Select
Selection.Paragraphs(Para).Range.Select
Selection.Copy
...
'code to paste the paragraph somewhere else
Next
Selection.Document.Undo
 
J

Jay

Thanks Helmut and Tony. I don't need the formatting, so I'm using
ranges, based on Helmut's example, and it's working fine.
 

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