Word 2007 script to apply style to text in column

A

a453

Hi all

I am looking for some way to automatically apply a specific style to the
text in a specific column in multiple tables of my document.

Example: the vba code should apply a style I created, called SKIP, to
all cells in column no 3 in all tables in my word 2007 document called
TEST.DOC

Any help is very appreciated thanks.
 
D

Doug Robbins - Word MVP

Dim i As Long, j As Long
With ActiveDocument
For i = 1 To .Tables.Count
With .Tables(i).Columns(3)
For j = 1 To .Cells.Count
.Cells(i).Range.Style = "Skip"
Next j
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
 
S

Stefan Blom

Hmm, if the style should be applied to *all* cells in column 3 for all tables,
the following should do the job:

Dim i As Long
Dim t As Table
Dim RowCount As Long
For Each t In ActiveDocument.Tables

RowCount = t.Rows.Count

For i = 1 To RowCount
t.Cell(i, 3).Range.Style = "SKIP"
Next i

Next t
 
A

a453

Doug Robbins - Word MVP wrote:

Hello thanks a lot for the code, I get an error when I try to run it ,
Word says something like "your tables have different number of columns".

I guess the script would run fine if the number of columsn was always
the same for all the tables in the document, but it is not?
 
G

Graham Mayor

There is a typo in Doug's code,
.Cells(i).Range.Style = "Skip"
should have read
.Cells(j).Range.Style = "Skip"

To ensure that yor tables have at least three columns you can add a
condition

Dim i As Long, j As Long
With ActiveDocument
For i = 1 To .Tables.Count
If .Tables(i).Columns.Count > 2 Then
With .Tables(i).Columns(3)
For j = 1 To .Cells.Count
.Cells(j).Range.Style = "Skip"
Next j
End With
End If
Next i
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Stefan Blom

Thanks for clarifying this. I looked at Doug's code and couldn't tell why it
didn't work as advertised.
 
D

Doug Robbins - Word MVP

Thanks, Graham.

--
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
 

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