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

Re: Question on Ranges by Jay

Jay
Mon May 05 11:58:00 PDT 2008

> ' 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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Elessvie wrote:
> 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



Re: Question on Ranges by Elessvie

Elessvie
Mon May 05 12:14:01 PDT 2008

Great!

Thanks, Jay,
-Lynne

"Jay Freedman" wrote:

> > ' 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 FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> all may benefit.
>
> Elessvie wrote:
> > 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
>
>
>