Paragraph Styles for Text in All Cells

G

Gerald Perna

I need to insert the name of the paragraph style in front
of the text that is in the cells of all the tables in a
document.

This is how I did it for the regular text in a document:

Dim ParSty As String
ParSty = Selection.Paragraphs(1).Style
Selection.Range.InsertBefore "{" & ParSty & "}"
Do Until Selection.Paragraphs(1).Style = "END"
Selection.MoveDown Unit:=wdParagraph, Count:=1
ParSty = Selection.Paragraphs(1).Style
Selection.Range.InsertBefore "{" & ParSty & "}"
Loop

It works fine, but I'm not sure how to do the same thing
for tables. Can anyone help me?

Thanks.

Jerry
 
G

Gerald Perna

Doug:

I get a collection error on the 5th line, and a compile
error on the 6th line.

Any more help you can give would be appreciated.

Thanks.

Jerry
 
D

Denise Z

I tested the following code and it works like a charm,
although you might want to add a vbCr at the end of the
insertbefore text to set off the style name from the
original text. Don't forget to add your code to check for
the END style, or it will go one past the last cell and
tell you what that style is.

'Substitute the appropriate table number (i.e., Table 2 =
Tables(2).Range... in next statement

Set oTable = ActiveDocument.Range.Tables(1)

'For each cell in the table, (your explanation here)

For Each oCell In oTable.Range.Cells
oCell.Select
ParSty = Selection.Style
Selection.Range.InsertBefore "{" & ParSty & "}"
Next oCell

Good luck.

Denise
 
D

Doug Robbins - Word MVP

Hi Gerald,

In addition, I had my j's and k's reversed.

Here is a corrected version (once again, the fifth line has wrapped:

Dim i As Integer, j As Integer, k As Integer
For i = 1 To ActiveDocument.Tables.Count
For j = 1 To ActiveDocument.Tables(i).Rows.Count
For k = 1 To ActiveDocument.Tables(i).Columns.Count
ActiveDocument.Tables(i).Cell(j, k).Range.InsertBefore "{" &
ActiveDocument.Tables(i).Cell(j, k).Range.Paragraphs(1).Style & "}"
Next k
Next j
Next i

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

Hi Denise,

Take a large table say 20 x 20 and notice the difference in speed between
your code and the following

Dim i As Integer, j As Integer, k As Integer
For i = 1 To ActiveDocument.Tables.Count
For j = 1 To ActiveDocument.Tables(i).Rows.Count
For k = 1 To ActiveDocument.Tables(i).Columns.Count
ActiveDocument.Tables(i).Cell(j, k).Range.InsertBefore "{" &
ActiveDocument.Tables(i).Cell(j, k).Range.Paragraphs(1).Style & "}"
Next k
Next j
Next i

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
G

Gerald Perna

Denise:

Thanks a lot.

Jerry
-----Original Message-----
I tested the following code and it works like a charm,
although you might want to add a vbCr at the end of the
insertbefore text to set off the style name from the
original text. Don't forget to add your code to check for
the END style, or it will go one past the last cell and
tell you what that style is.

'Substitute the appropriate table number (i.e., Table 2 =
Tables(2).Range... in next statement

Set oTable = ActiveDocument.Range.Tables(1)

'For each cell in the table, (your explanation here)

For Each oCell In oTable.Range.Cells
oCell.Select
ParSty = Selection.Style
Selection.Range.InsertBefore "{" & ParSty & "}"
Next oCell

Good luck.

Denise

.
 

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