Andreas Hermle
asked on
Avoiding selection object when setting a range on a table macro
Hello,
below macro applies a custom paragraph style from the second row to the last row of a selected table. Can this code be improved, e.g. by not using the selection object when setting the range?
Help is appreciated. Thank you very much in advance.
Sub ApplyStyleSecondToLastRow( )
Selection.Tables(1).Select
Selection.SetRange _
Start:=Selection.Rows(2).R ange.Start , _
End:=Selection.End
Selection.Style = "custom paragraph style"
End Sub
below macro applies a custom paragraph style from the second row to the last row of a selected table. Can this code be improved, e.g. by not using the selection object when setting the range?
Help is appreciated. Thank you very much in advance.
Sub ApplyStyleSecondToLastRow(
Selection.Tables(1).Select
Selection.SetRange _
Start:=Selection.Rows(2).R
End:=Selection.End
Selection.Style = "custom paragraph style"
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
found out myself. I guess this is the best way forward:
Sub ApplyStyleSecondToLastRow(
Dim tbl As Word.Table
Dim row As Word.row
Dim rng As Word.Range
Set tbl = ActiveDocument.Tables(1)
Set rng = tbl.Range
For Each row In tbl.Rows
rng.SetRange tbl.Rows(2).Range.Start, tbl.Rows.Last.Range.End
rng.Style = "Dipl_Tbl_TK"
Next row
End Sub