PDA

View Full Version : Solved: Bookmark Field Range



Adamski
05-20-2011, 09:21 AM
I want to bookmark ranges. I only want to create a bookmark if one does not alreay exist for a given range. I am determining the ranges based on SEQ fields.

The problem is that bookmarking a field.range seems to create a bookmark with a range different to the field.range. I have played with MoveEnd and MoveStart but something is still wrong.

For Equations, I want include an extra field imediately before the {SEQ Equation} in the bookmark. It is the chapter number and a dash.

SomeEquation (1-1)

Bookmarked:
SomeEquation ([1-1])

I'd rather not have to rely on the brackets being there to determin the range.

At the moment, more bookmarks keep getting created for the same range.

Help most welcome,
TIA


Public Sub OnActionButton_InsertCaptionReference(control As IRibbonControl)

' MsgBox "Id : " & control.id & vbCrLf & _
' "Tag: " & control.Tag, vbInformation, "OnActionButton_InsertCaptionReference"

Dim oField As Field
On Error Resume Next
Set oField = ActiveDocument.Fields(control.Tag)
On Error GoTo 0

If Not oField Is Nothing Then

Dim sCode As String
sCode = oField.Code.Text

Dim oRange As Range
Set oRange = oField.Code.Duplicate

' Move End out of field
oRange.MoveEnd Unit:=wdCharacter, count:=1

' What sort of Caption
If InStr(sCode, "Table") <> 0 Then
oRange.StartOf Unit:=wdParagraph, Extend:=wdExtend
ElseIf InStr(sCode, "Figure") <> 0 Then
oRange.StartOf Unit:=wdParagraph, Extend:=wdExtend
ElseIf InStr(sCode, "Equation") <> 0 Then
oRange.StartOf Unit:=wdWord, Extend:=wdExtend
oRange.MoveStart Unit:=wdCharacter, count:=-1
End If

' Bookmark It
Dim oBookmark As Bookmark
Set oBookmark = GetRangeBookmark(oRange, "_CapRef")
Debug.Print "Bookmark Name: " & oBookmark.Name

' Insert Reference to Bookmark
Selection.ShrinkDiscontiguousSelection
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldRef, _
Text:=oBookmark.Name, PreserveFormatting:=False

End If

End Sub

Function GetRangeBookmark(oRange As Range, Prefix As String) As Bookmark

' Try to get existing bookmark
Dim oBookmark As Bookmark
For Each oBookmark In oRange.Bookmarks
If oBookmark.Range.IsEqual(Range:=oRange) = True Then
Set GetRangeBookmark = oBookmark
Exit For
End If
Next oBookmark

' Create bookmark if necessary
If GetRangeBookmark Is Nothing Then

Dim BookmarkName As String
BookmarkName = Prefix & DateDiff("s", 0, Now)

While ActiveDocument.Bookmarks.Exists(BookmarkName)
BookmarkName = "_CapRef" & DateDiff("s", 0, Now)
Wend
Set GetRangeBookmark = ActiveDocument.Bookmarks.Add(BookmarkName, oRange)
End If

End Function

macropod
05-20-2011, 09:13 PM
Hi Adamski,

Are you trying to bookmark the actual equations, or the captions referencing them? If it's the latter, do you have an issue with using Word's internal caption bookmarking, which can be accessed via Insert|Cross-reference?

Adamski
05-21-2011, 04:18 AM
Hi,

I just want the two fields bookmarked, not the actual equations. The equations are being put in a paragraph with a center tab marker and a right tab marker.

-> Equation -> ({nested fields {calculating chapter and dash}}{SEQ Equation})

The built in Cross-referencing tool doesn't work for this. It either bookmarks everything before and including the SEQ field, or just the close bracket after it. I would like it to just bookmark the SEQ field and the nested field immediately proceeding it. I'd rather not have to rely on detecting the brackets as the MAN may decide to change them in future.

An expand range to fully include field function is what i need really i guess.

macropod
05-21-2011, 04:48 AM
Hi Adamski,

I think you might be going about this the wrong way. If you insert the equation and caption in the normal manner, you can (with a bit of creativity)employ Word's Style Separator (ie Ctrl-Alt-Enter) to separate the elements - including the parentheses either side of the caption. You can then cross-reference the caption in the normal manner.

See attached.

Adamski
05-23-2011, 07:20 AM
Didn't think to use Style Separators - I've never really found a use for them. I also didnt realise you could use the \# switch in ref fields.

Thanks

macropod
05-23-2011, 02:53 PM
Hi Adamski,

Now that you've found a use for Style separators, can we take it that the problem is solved? If so, please mark the thread as such.

Adamski
05-25-2011, 09:02 AM
How do I do that? I can't find the Solved command anymore.

macropod
05-31-2011, 03:11 PM
I think it's under 'Thread Tools'. Since only the original poster (and probably the Admin/Mod staff) can mark a thread as solved, I can't do it for you.

Adamski
06-01-2011, 02:28 AM
Found it with Firefox! I couldn't see it because the menu dosen't get shown in Google Chrome (11.0.696.60).