Does a paragraph know it is in a table?

D

DaleH

I have some code that iterates through the paragraphs in the paragraph
collection of the ActiveDocument and sets the LeftIndent for the paragraph
based on the paragraph above it (actually the TextPosition property of a List
item above it).

Everything is fine until I hit a paragraph that is actually embedded in a
table cell instead of being free standing. The result is that the LeftIndent
for the text does get placed where I want it (inside the cell), but the table
that it lives inside is still flush left and I'd like to move it over too.
This is complicated when there are two columns in this table... my code finds
the paragraph in the second column as well and sets its indent relative to
the left edge of the 2nd column, which I don't want to do at all.

So tables and paragraphs intermingle, but the paragraph collection doesn't
seem to know about the tables and the table collection doesn't know about
paragraphs.

Here's a snip of code (see embedded comments):

intOutlineLevel = 0
intTextPositionAbove = 0

For Each para In ActiveDocument.Paragraphs
If Mid(para.Style, 1, 9) = "MM Topic " Then
intOutlineLevel = para.OutlineLevel
intTextPositionAbove = para.LeftIndent
para.Range.ListFormat.ApplyListTemplate _
ListTemplate:=ltTemp, ContinuePreviousList:=True
Else
If (para.Style = "Normal" Or Mid(para.Style, 1, 3) = "MM ") Then
' If this para is in a table, I'd like to indent the table
instead of the paragraph
' If this para is in the second column of a table I don't want
to indent at all!
If (para.LeftIndent - intTextPositionAbove) <= 0 Then
para.LeftIndent = InchesToPoints(0.25 * intOutlineLevel)
Else
para.LeftIndent = InchesToPoints(0.25 * intOutlineLevel) + _
(para.LeftIndent - intTextPositionAbove)
End If
End If
End If
Next para

Thanks in advance for your ideas...

Dale
 
J

Jonathan West

Hi Dale

You can find out whether a paragraph is in a table by this method

For Each para In ActiveDocument.Paragraphs
If para.Range.Information(wdWithinTable) Then
'paragraph is in a table
Else
'it isn't
End If
Next para

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

DaleH

Thank-you Jonathan! This is the clue I needed (I was avoiding the Range
object... guess it's time to learn). Here is the final code snip. I was
able to use para.Range.Tables.Rows.LeftIndent to set the final properties.
Very cool.

Dale

intOutlineLevel = 0
intTextPositionAbove = 0

For Each para In ActiveDocument.Paragraphs
para.KeepWithNext = False
If Mid(para.Style, 1, 9) = "MM Topic " Then
intOutlineLevel = para.OutlineLevel
intTextPositionAbove = para.LeftIndent
para.Range.ListFormat.ApplyListTemplate _
ListTemplate:=ltTemp, ContinuePreviousList:=True
Else
If (para.Style = "Normal" Or Mid(para.Style, 1, 3) = "MM ") Then
If para.Range.Information(wdWithInTable) Then
For Each paraTable In para.Range.Tables
For Each paraRow In paraTable.Rows
paraRow.LeftIndent = InchesToPoints(0.25 *
intOutlineLevel)
Next paraRow
Next paraTable
Else
If (para.LeftIndent - intTextPositionAbove) <= 0 Then
para.LeftIndent = InchesToPoints(0.25 * intOutlineLevel)
Else
para.LeftIndent = InchesToPoints(0.25 * intOutlineLevel)
+ _
(para.LeftIndent - intTextPositionAbove)
End If
End If
End If
End If

Next para
 

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