need field to remove the first word in a cross reference

B

bmurphy

I have a cross-reference field in my document to a bookmark that
contains several words. Is there any way to remove the first word and
display the rest?

Thanks

Brian Murphy
 
G

Graham Mayor

Not without creating another bookmark

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Stefan Blom

Alternatively, you can redefine an existing bookmark, by selecting the
"other" words and then clicking the Add button (in the Bookmark dialog box).

Note that if this is a bookmark generated by Word, you will have to display
hidden bookmarks and click the "Location" radio button. Then Word highlights
the name of the current bookmark. Click Add to redefine the bookmark.

--
Stefan Blom
Microsoft Word MVP


in message
 
B

bmurphy

I was beginning to think it would come to this.

Since I'm using my own macros to put in captions and their cross
references, I should be able to work in the bookmark that I need.

When I insert a caption, I'll enclose the part I want to use later in
a regular bookmark. I'm pretty sure that when later selecting that
caption in the Insert/Cross Reference dialog Word will create it's
hidden bookmark (if it's not there already) with mine inside, and
create a cross reference to it's hidden bookmark. Then I'll have my
macro edit the cross reference field to change the bookmark label.

Does that sound like the way to go? If you have any suggestions on
this, please post back. Right now I don't how to find the tag for
what will be my bookmark.

Cordially,

Brian
 
B

bmurphy

Thanks for the suggestion.

In my limited testing, Word doesn't give up that easy. I found that
if I redefine Word's hidden bookmark for the caption, Word creates a
new bookmark the next time I use the Cross Reference dialog to create
a reference to that caption.

Word won't use the modified bookmark, so I have to leave Word's
bookmark intact, and change the cross reference field created by Word
to point to my bookmark instead.

I just need to know how to find the tag for my bookmark that is
"inside" Word's bookmark.

Unless there is a built-in function that will do this (.Intersect ?),
I can loop through all the bookmarks in the document and check
their .Range.Start & .End properties to find the right one.

In concept this ought to work, but this is turning out to be a lot
harder than it ought to be.

Brian
 
S

Stefan Blom

You are right: my suggestion can only be used to modify an existing
bookmark; it won't change how Word creates bookmarks. For that you will have
to use a macro (which, it seems, you are already doing anyway).
 
G

Guest

Brian:

You should just replace Word's Cross-Reference command with your own. (Just
kidding. I did, but it's not trivial. But oh, so satisfying.)

Anyway... Can't you get the range of the bookmark and find the first or
second bookmark with it (that's your bookmark). Maybe easier if you use a
fixed prefix for your bookmarks. The following is pseudo-code to illustrate
the idea.

Dim objRange as Range
Dim strBookmark as String
Dim objBookmark as Bookmark

' Insert code to determine the name of Word's bookmark
' and save it in strBookmark

set objRange = Activedocument.Bookmarks(strBookmark)
For each objBookmark in objRange.Bookmarks
If Left(objBookmark.Name, 3) = "BXM" Then
' Your processing here
End If
Next objBookmark

~~~~~

This doesn't have any error handling or anything, but I hope you get the idea.

Bear
 
B

bmurphy

Here's the code I'm presently using. It's a bit spartan, but it gets
the job done. Two macros. One called from a macro that had just
inserted a caption, and the other called from a macro that had just
inserted a cross reference.

I've been working a lot with VBA in excel since 1995, but Word VBA is
entirely new to me. If you see anything I ought to be doing
differently, please let me know.

Cheers,

Brian

Sub mrfAddBookmark()
'right after creating a caption that has a label followed by a caption
number
'define a bookmark that contains just the caption number
'the bookmark ID will be mrf########## where the digits are from the
current time of day
'it doesn't matter if the caption number includes a chapter number
(maybe), and any separator is okay
'upon entering this routine the caption number should be selected
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="mrf" &
Int((Now - Int(Now)) * 10 ^ 9)
Selection.MoveRight Unit:=wdCharacter, Count:=2
End Sub

Sub mrfChangeCrossReference()
'this operates on a REF cross-reference field for a caption
'The bookmark ID in the field is one of Word's hidden bookmarks that
includes the caption label
'There should be one of my own bookmarks inside that one that excludes
the label
'This routine changes the bookmark ID in the cross-reference from
Word's to mine
Dim s$, s1$, s2$, s3$, rng As Range
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Set rng = Selection.Fields(1).Code
s = Trim(rng.Text)
s1 = Mid(s, 1, InStr(s, " "))
s = Mid(s, Len(s1) + 1)
s2 = Mid(s, 1, InStr(s, " ") - 1)
s = Mid(s, Len(s2) + 1)
s2 = ActiveDocument.Bookmarks(s2).Range.Bookmarks(1).Name
s = " " & s1 & s2 & s3 & " "
rng.Text = s
Selection.Fields(1).Update
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub
 
G

Guest

Brian:

I'm sorry, but my brain just refuses to analyze code on a Monday morning, so
I'm not going to be of any use.

However, you're working in difficult, dangerous territory when you use the
selection and the Word GUI interface the way you're doing to determine stuff.

Maybe you could use the field code and result properties with more confidence.

Use selection.paragraphs(1) to set a range, then examine range.fields to
find the REF field.

Parse out the REF field code to get the hidden bookmark name.

Use activedocument.bookmarks(name) to set another range (that's your caption
with the nested bookmarks, one of which is yours).

Examine the bookmarks in that range to find the one starting with "mrf" to
get that bookmark's name.

Set the code for the original REF field to use the mrf bookmark name rather
than the original hidden bookmark name.

That's the strategy I'd use, anyway.

Bear
 

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