Link to home
Start Free TrialLog in
Avatar of Andreas Hermle
Andreas HermleFlag for Germany

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).Range.Start, _
        End:=Selection.End
                 Selection.Style = "custom paragraph style"
End Sub
Avatar of Andreas Hermle
Andreas Hermle
Flag of Germany image

ASKER

Hello,
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
ASKER CERTIFIED SOLUTION
Avatar of EE_AutoDeleter
EE_AutoDeleter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial