Avoiding selection object when setting a range on a table macro

A

andreas

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
 
G

Greg Maxey

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
 
A

andreas

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

--
Greg Maxey/Word MVP
See:http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.





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

Greg Maxey

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
 
A

andreas

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

--
Greg Maxey/Word MVP
See:http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.









- Zitierten Text anzeigen -

Greg,

again, great advice. Thank you very much.
Regards, Andreas
 
T

Thomas Gahler

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!



--
Thomas Gahler
MVP für WordVBA
Co-Autor von »Microsoft Word-Programmierung.
Das Handbuch« (MS Press)


- Windows XP (SP2), Office XP (SP3)
 
A

andreas

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!

--
Thomas Gahler
MVP für WordVBA
Co-Autor von »Microsoft Word-Programmierung.
Das Handbuch« (MS Press)

- 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
 

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