Select method: where's my Selection object?

M

Mark

This is a simple question. Please enlighten. I am using JScript to do
some Word automation. As far as the Word object model documentation is
concerned, unlike a Bookmark object, the Field object does not have a
Range property. You must select a Field object using the Select
method, then work with some resulting Selection to do things like
insert text into the document before or after the field.

The msdn doc is kind of baffling. It says the Select method "Selects
the specified object. Note After using this method, you can use the
Selection property to return a Selection object. For more information,
see Working with the Selection object." But the Selection property of
what object? How do you get from the Select method to a Selection you
can work with? None of the code samples or other doc clarify this.

Here is the chunk of code in question. Note in JScript you must use
Enumerations to handle Collections.

var fields = new Enumerator(workingdoc.Fields);

....

for (; !fields.atEnd(); fields.moveNext())
{
var thisfield = fields.item();
thisfield.Select();
var selectedfield = thisfield.Selection;
selectedfield.InsertAfter("huh");
}

When I run a script including this, I can see in my active, visible
document that the field is indeed selected on the screen
(thisfield.Select() works fine). However, I get an error saying
"'selectedfield' is null or not an object", so no text is inserted.

What's the easy thing I'm missing here?

Thank you.
 
J

Jezebel

The Selection object is a property of the Application.

But, if you're working from code, there is nothing you can do with the
Selection object that you can't do better with range objects. It's a little
confusing at first, but quite a few Word objects have Range properties that
are called something else. In the case of a field, the .Code and .Result
properties are both Range objects.
 
M

Mark Tangard

Hi Mark,

You actually don't need to select the field. The Field object
uses the .Result property where you'd ordinarily expect to use
..Range; the logic to that stems from the fact that a field may
be showing or not showing its codes. The .Result property in
fact returns a range. (To return the content of the result,
you'd want [field].Result.Text, but that's not evidently what
you're aiming for here.)

So at first glance it may look like you could use (note: my
JScript background is absolute zero):

thisfield.Result.InsertAfter("huh");

in place of your last 3 lines below. HOWEVER, what this does is
add "huh" to the result itself, that is, it makes the "huh" PART
of the result (so any subsequent field updating would trash the
added characters), rather than appending it after the field.

So what you want is probably:

Dim r As Range
Set r = thisfield.Result
r.MoveEnd wdCharacter, 1
r.InsertAfter("huh");

Hope this helps a little.
 

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