Question on Ranges

E

Elessvie

Dear Programmers,

I am stumped about why "oRange" is doing what it is doing and wondered if
someone could explain it to me (and/or point me in the right direction). We
are using Word 2003 and the macro is in VBA. Here is my code (it's from the
very beginning of the macro):

Dim oRange As Range
Set oRange = ActiveDocument.Tables(2).Range

With oRange
'collapse oRange to a point at the end of Table 2:
.Collapse wdCollapseEnd
'insert next-page-section-break right after Table 2:
.InsertBreak wdSectionBreakNextPage
'insert a paragraph right after the section break:
.InsertParagraphAfter
'
'If I understand correctly, the insertion point
'at this point should still be where oRange was
'collapsed--at the end of Table 2.
End With
oRange.Select
With Selection
.TypeText "TOC"
End With
'
' But here, "TOC" appears right after
' the inserted paragraph, not right
' after Table 2. Isn't oRange now still
' just a point after Table 2?

Any feedback greatly appreciated.
Thanks so much!
-Lynne
 
J

Jay Freedman

' But here, "TOC" appears right after
' the inserted paragraph, not right
' after Table 2. Isn't oRange now still
' just a point after Table 2?

No, both the .InsertBreak and the .InsertParagraphAfter affect the size and
location of oRange. To see what's going on, insert copies of the
oRange.Select statement after each of those commands, and single-step the
code by pressing F8 repeatedly. You'll see that after the .InsertBreak the
range is positioned after the break; and after the .InsertParagraph the
range includes the new paragraph mark.

If you want the range to be between the table and the break before you
insert "TOC", one way is to repeat

Set oRange = ActiveDocument.Tables(2).Range
oRange.Collapse wdCollapseEnd

at that point in the code. Another way is to declare and assign another
range as a "placeholder":

Dim oRange As Range
Dim oRangeDup As Range
....
With oRange
.Collapse wdCollapseEnd
Set oRangeDup = oRange.Duplicate
....
End With

oRangeDup.Text = "TOC"

(Assigning a string to the range's .Text accomplishes the same insertion as
selecting the range and doing .TypeText, but with the advantage that it
doesn't move the cursor.)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
E

Elessvie

Great!

Thanks, Jay,
-Lynne

Jay Freedman said:
No, both the .InsertBreak and the .InsertParagraphAfter affect the size and
location of oRange. To see what's going on, insert copies of the
oRange.Select statement after each of those commands, and single-step the
code by pressing F8 repeatedly. You'll see that after the .InsertBreak the
range is positioned after the break; and after the .InsertParagraph the
range includes the new paragraph mark.

If you want the range to be between the table and the break before you
insert "TOC", one way is to repeat

Set oRange = ActiveDocument.Tables(2).Range
oRange.Collapse wdCollapseEnd

at that point in the code. Another way is to declare and assign another
range as a "placeholder":

Dim oRange As Range
Dim oRangeDup As Range
....
With oRange
.Collapse wdCollapseEnd
Set oRangeDup = oRange.Duplicate
....
End With

oRangeDup.Text = "TOC"

(Assigning a string to the range's .Text accomplishes the same insertion as
selecting the range and doing .TypeText, but with the advantage that it
doesn't move the cursor.)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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