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
Greg Maxey - 03 Apr 2007 04:14 GMT
Andreas,
Again, not saying it is an improvement:
Sub Scratchmacro()
Dim oRng As Word.Range
Dim oTbl As Word.Table
Set oTbl = Selection.Tables(1)
Set oRng = oTbl.Range
oRng.Start = oTbl.Rows(2).Range.Start
oRng.Style = "body text"
Set oTbl = Nothing
Set oRng = Nothing
End Sub

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> Hello,
> below macro applies a custom paragraph style from the second row to
[quoted text clipped - 9 lines]
> Selection.Style = "custom paragraph style"
> End Sub
andreas - 03 Apr 2007 04:35 GMT
> Andreas,
>
[quoted text clipped - 31 lines]
>
> - Zitierten Text anzeigen -
Greg:
of course it is an improvement, since I really can learn from the way
you are coding. Thank you very much.
I also tried myself improving this code and came up with this a couple
of minutes ago. But I am sure yours is more sophisticated. Again
thank you very much.
Regards, Andreas
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
Greg Maxey - 03 Apr 2007 04:50 GMT
Andreas,
Sub ApplyStyleSecondToLastRow()
Dim tbl As Word.Table
'Dim row As Word.row
Dim rng As Word.Range
'This line would not necessarily process the selected table
Set tbl = ActiveDocument.Tables(1)
'This sets the rng.Start and rng.End to the .Start and .End of the table
Set rng = tbl.Range
'There is no reason to do to each row what you can do to a whole range
'For Each row In tbl.Rows
'oRng.End is already equal to the tbl.Range.End so just change the start
rng.Start = tbl.Rows(2).Range.Start ', tbl.Rows.Last.Range.End
rng.Style = "Dipl_Tbl_TK"
'Next row
End Sub

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
>> Andreas,
>>
[quoted text clipped - 54 lines]
> Next row
> End Sub
andreas - 03 Apr 2007 11:46 GMT
> Andreas,
>
[quoted text clipped - 79 lines]
>
> - Zitierten Text anzeigen -
Greg,
again, great advice. Thank you very much.
Regards, Andreas
Thomas Gahler - 03 Apr 2007 13:17 GMT
Hi andreas
Why do you first ask here in englisch
and one hour later in the german ng the same?
We don't like multimple questions!

Signature
Thomas Gahler
MVP für WordVBA
Co-Autor von »Microsoft Word-Programmierung.
Das Handbuch« (MS Press)
- Windows XP (SP2), Office XP (SP3)
andreas - 07 Apr 2007 06:00 GMT
> Hi andreas
>
[quoted text clipped - 10 lines]
>
> - Windows XP (SP2), Office XP (SP3)
Thomas,
did not know that these newsgroups are interconnected/the same. This
one is only in English and the one you are often commenting is purely
in German. I have assumed all along that they are totally different
newsgroups.
I am sorry about that. Will of course consider this fact in the
future.
Regards, Andreas
Klaus Linke - 08 Apr 2007 21:11 GMT
If the macro works using the Selection, there may not be much point to
convert it to Ranges.
First, the Selection is much more powerful in tables. Say you can select a
column, but not define a Range for it.
Second, in some cases a macro can actually be much faster working with the
Selection. An article by Dave Rado on the www.word.mvps.org site on
performance and tables has numbers to prove that:
http://word.mvps.org/FAQs/TblsFldsFms/FastTables.htm
Regards,
Klaus
> Hello,
> below macro applies a custom paragraph style from the second row to
[quoted text clipped - 9 lines]
> Selection.Style = "custom paragraph style"
> End Sub
andreas - 10 Apr 2007 12:03 GMT
> If the macro works using the Selection, there may not be much point to
> convert it to Ranges.
[quoted text clipped - 22 lines]
>
> - Zitierten Text anzeigen -
Klaus,
good comment. Thank you!
Regards, Andreas