how can I toggle generated text using bookmarks in VB?

G

G.O.Varney

I have the follow VB script

ActiveDocument.Bookmarks.Add("PPT").Select

If rbInstructor.Value Then
Selection.Text = "I"
Else
Selection.Text = "S"
End If

Selection.WholeStory
Selection.Fields.Update
Selection.HomeKey Unit:=wdStory

That creates a bookmark called PPT and then sets it value to either I or S.

In my word document I have create by Cntl-F9 the following

{IF PPT ="I" "0A80" "ABCD"}

When the VB script sets the bookmark to I it should Display 0A80 and
anything else should get ABCD

But this is not happening I always get ABCD

This is my first attempt at controlling text via VB and it hasn't worked.

What have I done wrong ? Have I got the concept right ?
 
P

Peter Jamieson

It won't work because when you create the bookmark, it is a "point"
bookmark. Then when you assign "I" or "S" to Selection.Text, the
bookmark does not actually cover the "I" or "S". You can see that the
bookmark is blank/null by inserting the field

{ REF PPT }
or just
{ PPT }

This example would work if you did


If rbInstructor.Value Then
Selection.Text = "I"
Else
Selection.Text = "S"
End If

Selection.Range.Bookmarks.Add("PPT").Select

Selection.WholeStory
Selection.Fields.Update
Selection.HomeKey Unit:=wdStory


I don't know what you are /actually/ trying to do, but a couple of
alternatives would be to insert the value 0A80 or ABCD directly, or
create a Document variable (Activedocument.Variables.Add ...) and use a
{ DOCVARIABLE } field to insert its value.

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
 
J

Jay Freedman

The first problem is the syntax of the field you created. In that
field, PPT is just text. If you want to use the value of the bookmark
named PPT, you have to surround it with field marks -- select the PPT
and press Ctrl+F9 again so it looks like

{IF {PPT} ="I" "0A80" "ABCD"}

The second problem is that when your macro selects the bookmark and
then assigns Selection.Text, it deletes the bookmark. Then the {PPT}
field gives the result "Error! Bookmark not defined." which will never
equal "I". To solve this, see
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm

That article also shows that you don't have to -- and shouldn't --
select the bookmark in order to assign text to it. Instead, assign to
the bookmark's Range.Text.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

G.O.Varney

Thanks Peter and Jay,

DocVariable is exactly what I needed.

My initial search in MS Office Online for "variable" headed in in the
direction of bookmarks. DocVariable was not mentioned. In fact, a search for
"Docvariable" returns nothing. What chance did I have.

My VB code now looks as follows:

If rbInstructor.Value Then
ty = "I"
Else
ty = "S"
End If

For Each aVar In ActiveDocument.Variables
If aVar.Name = "DV" Then num = aVar.Index
Next aVar

If num = 0 Then
ActiveDocument.Variables.Add Name:="DV", Value:=ty
Else
ActiveDocument.Variables(num).Value = ty
End If

So Docvariable "DV" has either an I or S assigned to it. My word document
now looks like this

{IF {DOCVARIABLE DV}="I"
"0A80"
"ABCD"}

So when DV = "I" the characters 0A80 is displayed in the document otherwise
the characters "ABCD" will be displayed.

Thanks for you help
 
J

Jay Freedman

On the theory that simpler is better...

Are the values "0A80" and "ABCD" (or whatever is really in your
document) fixed rather than variable values? If so, change your macro
code to assign those values directly to the DocVariable, and replace
the entire IF field with just the DOCVARIABLE field. That is, change
the first part of the code to

If rbInstructor.Value Then
ty = "0A80"
Else
ty = "ABCD"
End If

and then in the document put

{DOCVARIABLE DV}

Having the "I" and "S" don't add anything other than unneeded
complexity.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
P

Peter Jamieson

Hi Jay,

It's a side issue, but...
If you want to use the value of the bookmark
named PPT, you have to surround it with field marks -- select the PPT
and press Ctrl+F9 again so it looks like

This is not the case: "IF" will work with bookmark names without
surrounding them with field braces (cf. "=" fields). Arguably, using {
PPT } is potentially worse because if the first word of the value of PPT
resolves to another bookmark name, Word actually uses the value of
/that/ bookmark in the comparison, unless you also quote the
{ PPT } using "{ PPT }"

So e.g. if you have

{ SET ABC "DEF" }{ SET DEF "GHI" }
{ IF ABC = "DEF" "match" "nomatch" }
{ IF { ABC } = "DEF" "match" "nomatch" }
{ IF "{ ABC }" = "DEF" "match" "nomatch" }

you get
match
nomatch
match

change
{ IF { ABC } = "DEF" "match" "nomatch" }
to
{ IF { ABC } = "GHI" "match" "nomatch" }
and you get
match

and so on.

In the end I prefer the "{ ABC }" syntax primarily because it's less
likely to suffer from such issues.

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
 

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