#1
|
|||
|
|||
Adding a border to every nth row of a word table
Hi All,
I found some very helpful code on this forum to edit add borders to a table in word via vba. Now, I'd like to tweak that code to add a horizontal border to only every nth row. If possible, i'd like to even specify at which row after the top border to start counting. Here is my code: Code:
Sub modifyactivetable() Dim oTbl With ActiveDocument Set oTbl = Selection.Tables(1) With oTbl .Borders(wdBorderTop).LineStyle = wdLineStyleSingle .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle .Borders(wdBorderRight).LineStyle = wdLineStyleSingle .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle .Borders(wdBorderVertical).LineStyle = wdLineStyleSingle '.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleSingle '.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleSingle End With End With Set oTbl = Nothing End Sub |
#2
|
||||
|
||||
Try:
Code:
Sub ModifyActiveTable() Dim i As Long With Selection If .Information(wdWithInTable) = False Then Exit Sub With .Tables(1) 'Kill any existing borders .Borders(wdBorderTop).LineStyle = wdLineStyleNone .Borders(wdBorderLeft).LineStyle = wdLineStyleNone .Borders(wdBorderBottom).LineStyle = wdLineStyleNone .Borders(wdBorderRight).LineStyle = wdLineStyleNone .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone .Borders(wdBorderVertical).LineStyle = wdLineStyleNone For i = 1 To .Rows.Count 'Restore borders to every 2nd row If i Mod 2 = 0 Then With .Rows(i).Cells .Borders(wdBorderTop).LineStyle = wdLineStyleSingle .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle .Borders(wdBorderRight).LineStyle = wdLineStyleSingle .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle .Borders(wdBorderVertical).LineStyle = wdLineStyleSingle End With End If Next End With End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
That's pretty much perfect. Is there a way to have the loop reset at rows which contain a certain string?
For example, have something like: Code:
For i = 1 To .Rows.Count 'Restore borders to every 2nd row If i Mod 2 = 0 and rowdoesnotcontain Then With .Rows(i).Cells .Borders(wdBorderTop).LineStyle = wdLineStyleSingle .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle .Borders(wdBorderRight).LineStyle = wdLineStyleSingle .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle .Borders(wdBorderVertical).LineStyle = wdLineStyleSingle else With .Rows(i).Cells .Borders(wdBorderTop).LineStyle = wdLineStyleSingle .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle .Borders(wdBorderRight).LineStyle = wdLineStyleSingle .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle .Borders(wdBorderVertical).LineStyle = wdLineStyleSingle End With End If Next End With End With End Sub |
#4
|
|||
|
|||
So I' think i've gotten quite close:
Code:
Option Explicit Sub parenrows() Dim i As Long Dim c As Object With Selection If .Information(wdWithInTable) = False Then Exit Sub With .Tables(1) 'Kill any existing borders .Borders(wdBorderTop).LineStyle = wdLineStyleNone .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle .Borders(wdBorderBottom).LineStyle = wdLineStyleNone .Borders(wdBorderRight).LineStyle = wdLineStyleSingle .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone .Borders(wdBorderVertical).LineStyle = wdLineStyleNone For i = 1 To .Rows.Count For Each c In .Rows(i).Cells ' Restore borders to every 2nd row If InStr(c.Range.Cells, "(") > 0 Then .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle End If Next c Next i End With End With End Sub Basically, i want to add a bottom line to any row with an open parentheses. Thanks! Last edited by hummus_it; 10-04-2013 at 01:59 PM. |
#5
|
||||
|
||||
Can you attach a document to a post with some representative data, showing what you're trying to achieve? You do this via the paperclip symbol on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Aligning Page Border with Table border without losing formatting :mad: | l39linden | Word Tables | 5 | 10-04-2013 02:06 AM |
Excel vba adding field in word table/shape in a header | Hdr | Excel | 1 | 02-04-2013 04:40 PM |
"First column" in word table page break border | mj1856 | Word Tables | 1 | 04-25-2012 03:21 AM |
adding rows to word table | hklein | Word VBA | 4 | 07-18-2011 12:21 AM |
Table Border | The Ink Monitor | Word Tables | 4 | 03-03-2011 09:17 AM |