Insert AutoText, then return cursor to where it was.

M

Mark

Hi,

Could someone please give me the code to insert an AutoText entry and
then return the cursor to the position it was in before the AutoText
was entered?

Thanks
 
M

Mark Tangard

Hi Mark,

In the simplest case you'd just need to set a range object to the Selection,
then insert the AutoText, then re-select the range:

Dim r as Range
Set r = Selection.Range
[template].AutoTextEntries("AutoTextName").Insert Where:=Selection.Range, _
RichText:=True
r.Select

There could be complications with aardvark AutoTexts, e.g., graphics, or if your
selection was something other than text, so if that's likely you'd want to code
around that, but the above is the basic idea.
 
M

Mark

Thanks Mark,

When I use this code, it selects the *entire* AutoText entry, so if
the user begins typing, it's deleted.

Is there a way to "deselect" it after the code is run, and just leave
the cursor at the beginning of the entry?
 
M

Mark Tangard

Mark,

The basic code I gave assumed you start with nothing selected. (When run that
way, nothing is selected when the macro finishes.) I gather from your post that
you'll have text selected before running the macro. In that case you'd want to
collapse the selection at the end:

Selection.Collapse wdCollapseStart

This assumes you want the initially selected text to be *replaced* by the
AutoText. If instead you want that text to remain, then disregard the line
above and collapse the selection *before* the AutoText is inserted. You'll also
need to decide whether you want it collapsed to its start (as above) or to its
end (wdCollapseEnd).

One important tip: *Please* don't snip the prior post in a thread if you're
asking a follow-up question on it. Leave the entire thread quoted intact.
Those of us who roam the newsgroups answering questions may respond to dozens of
posts per day, so it's rarely possible for us to call up an individual post up
from memory -- even if the poster *does* have the same first name. ;)
 

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