Moving selection

P

pvp

I am new to Word VBA. Unfortunately.

I open a doc successfully then stick in some text. I then
want to move to the end of the existing text so that I
can reset font properties and carry on, perhaps with a
new paragraph.
Reading the MS Help on selections and ranges has left me
totally confused. There must be a way to say 'go to the
end of the text I have just inserted'. Isn't there?

Thanks.
 
M

Mark Tangard

Hi Peter,

It helps to post the code you're using, even when you're
new -- or rather, *especially* when you're new. I assume
you're probably using the Selection object, which is usual
for newcomers, and I won't give you the Range lecture just
now (it's encouraging that you've read about both so you
know Ranges exist). For now, I'll bet you're using:

Selection.TypeText "like water for chocolate"

One way to get what you want is not to use the TypeText
method at all.

Selection.InsertAfter "like water for chocolate"
Selection.Collapse wdCollapseEnd

Brief global comment: If you're using VBA to build a block
of standard text, you may find it easier to store that text
(along with its fonts & other formatting) in an AutoText,
then insert the whole AutoText in your code, which takes
just a single line of code, of the form:

[template, if not Normal].AutoTextEntries("____").Insert _
Where:=Selection.Range, RichText:=True

The advantage is that you can make changes to that standard
text just by editing/redefining the AutoText, which is always
easier than swimming through the VBA that constructs it.
While it's always possible to build code that manufactures
content in a 'linear' way, it's almost always a pain in the
neck, for many reasons, not the least of which is that it's
entirely nonvisual, so you always end up leaving out spaces,
returns, etc.
 
P

pvp

Many thanks, Mark for your words of wisdom. That was the
kind of stuff I needed to get going and which is
difficult to get easily from (most) books.
pvp
-----Original Message-----
Hi Peter,

It helps to post the code you're using, even when you're
new -- or rather, *especially* when you're new. I assume
you're probably using the Selection object, which is usual
for newcomers, and I won't give you the Range lecture just
now (it's encouraging that you've read about both so you
know Ranges exist). For now, I'll bet you're using:

Selection.TypeText "like water for chocolate"

One way to get what you want is not to use the TypeText
method at all.

Selection.InsertAfter "like water for chocolate"
Selection.Collapse wdCollapseEnd

Brief global comment: If you're using VBA to build a block
of standard text, you may find it easier to store that text
(along with its fonts & other formatting) in an AutoText,
then insert the whole AutoText in your code, which takes
just a single line of code, of the form:

[template, if not Normal].AutoTextEntries ("____").Insert _
Where:=Selection.Range, RichText:=True

The advantage is that you can make changes to that standard
text just by editing/redefining the AutoText, which is always
easier than swimming through the VBA that constructs it.
While it's always possible to build code that manufactures
content in a 'linear' way, it's almost always a pain in the
neck, for many reasons, not the least of which is that it's
entirely nonvisual, so you always end up leaving out spaces,
returns, etc.

--
Mark Tangard <[email protected]>, Microsoft Word MVP
Please reply only to the newsgroup, not by private email.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters

I am new to Word VBA. Unfortunately.

I open a doc successfully then stick in some text. I then
want to move to the end of the existing text so that I
can reset font properties and carry on, perhaps with a
new paragraph.
Reading the MS Help on selections and ranges has left me
totally confused. There must be a way to say 'go to the
end of the text I have just inserted'. Isn't there?

Thanks.
.
 

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