View Single Post
  #9   Report Post  
Posted to microsoft.public.word.newusers
[email protected] bmurphy@xlrotor.com is offline
external usenet poster
 
Posts: 21
Default need field to remove the first word in a cross reference

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).Na me
s = " " & s1 & s2 & s3 & " "
rng.Text = s
Selection.Fields(1).Update
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub




On Sep 14, 10:36 am, Bear (nospam) wrote:
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