Range.insertafter

J

Julia

Hello,

Could someone out there please tell me what the
significance of Range and InsertAfter in the following
statement? I'm soooooo good at using selection.everything
but Range puzzles me. Thanks!

oDoc.Range.InsertAfter
 
J

Jean-Guy Marcil

Julia was telling us:
Julia nous racontait que :
Hello,

Could someone out there please tell me what the
significance of Range and InsertAfter in the following
statement? I'm soooooo good at using selection.everything
but Range puzzles me. Thanks!

oDoc.Range.InsertAfter

oDOc = an object referencing (presumably) a document
Range = a range of text, since it is not defined, it defaults to the whole
document range
InsertAfter = Insert the text after the preceding range object, in this case
the whole document, so it adds text at the end, and the new range equals the
old range plus the new text.

Play with this to see how it works:

'_______________________________________
Sub test()

Dim oDoc As Document
Dim ParaRange As Range

Set oDoc = ActiveDocument

oDoc.Range.InsertAfter "123"

With oDoc
.Range(.Paragraphs(1).Range.Start, _
.Paragraphs(3).Range.End).InsertAfter "123"
End With

Set ParaRange = oDoc.Paragraphs(1).Range

ParaRange.InsertAfter "123"
ParaRange.Select

End Sub
'_______________________________________

Do it step by step (With F8 from the VBA code window) while the VBA window
is reduced to half the screen (horizontally works best) so that you can see
what each line of code does in the document, in anything.

You should really learn to work with ranges, they are way more powerful,
more stable, more flexible, faster, etc. You can access part of the document
that are otherwise difficult with the selection object (Like headers...),
you do not have to move the user selection, the screen does not flashes
about while the codes executes (and thus runs faster and more reliably),
etc. There are way more advantages than disadvantages. The biggest
disadvantage is the learning curve because the macro recorder does not use
the range object (because you have to declare a range object variable and
refer to them as in my little example).
There are only a few cases where you have to use the selection object, so
learning to use range objects will only make your code more robust and run
faster.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Julia

Thanks you are very helpful!

Jean-Guy Marcil said:
Julia was telling us:
Julia nous racontait que :


oDOc = an object referencing (presumably) a document
Range = a range of text, since it is not defined, it defaults to the whole
document range
InsertAfter = Insert the text after the preceding range object, in this case
the whole document, so it adds text at the end, and the new range equals the
old range plus the new text.

Play with this to see how it works:

'_______________________________________
Sub test()

Dim oDoc As Document
Dim ParaRange As Range

Set oDoc = ActiveDocument

oDoc.Range.InsertAfter "123"

With oDoc
.Range(.Paragraphs(1).Range.Start, _
.Paragraphs(3).Range.End).InsertAfter "123"
End With

Set ParaRange = oDoc.Paragraphs(1).Range

ParaRange.InsertAfter "123"
ParaRange.Select

End Sub
'_______________________________________

Do it step by step (With F8 from the VBA code window) while the VBA window
is reduced to half the screen (horizontally works best) so that you can see
what each line of code does in the document, in anything.

You should really learn to work with ranges, they are way more powerful,
more stable, more flexible, faster, etc. You can access part of the document
that are otherwise difficult with the selection object (Like headers...),
you do not have to move the user selection, the screen does not flashes
about while the codes executes (and thus runs faster and more reliably),
etc. There are way more advantages than disadvantages. The biggest
disadvantage is the learning curve because the macro recorder does not use
the range object (because you have to declare a range object variable and
refer to them as in my little example).
There are only a few cases where you have to use the selection object, so
learning to use range objects will only make your code more robust and run
faster.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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