Setting a Range

E

Elaine J.

Can someone tell me how to set a range for the last section of a document?
Or as an alternative how to set the range to the section where the cursor is?

Specifically what I need to do is set the last section as a range so I can
make some modifications. First I need to delete the last two rows from a
table in that section (there are two tables, and I need to delete rows 3 and
4 from the second table). Then I need to search for a text string, and
insert some text in front of the original text.

Thanks for any help.
 
H

Helmut Weber

Hi Elaine,

like that:

Sub Scratchmacro()
Dim rTmp As Range
With ActiveDocument
Set rTmp = .Sections(.Sections.Count).Range
rTmp.Tables(2).Rows(4).Delete
rTmp.Tables(2).Rows(3).Delete
With rTmp.Find
.Text = "SomeText"
If .Execute Then
rTmp.InsertBefore "SomeMoreText"
End If
End With
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
E

Elaine J.

Helmut, thank you so much. I understand all of it except this line of code:

Set rTmp = .Sections(.Sections.Count).Range

What does that do? Is it the last section of the document? Or do I have to
have my cursor in that section first?

Thanks again for your help.
 
D

Doug Robbins - Word MVP

Sections.Count will give the number of sections in the document, so
Sections(Section.Count) refers to the last section in the document. The
cursor does not have to be in that section.

As an example, if there were 5 sections in the document, Sections.Count
would return 5 so that Section(Sections.Count) would be the equivalent of
Sections(5)

If you wanted the Section in which the cursor is located,
Selection.Sections(1) would give you the section in which the start of the
text selected by the cursor is located.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

Elaine J.

Thank you so much. The code works perfectly. I just wanted to be sure I
understood what it was doing.

Elaine
 

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