0
votes

OP Update:

Thanks for the code KazJaw, it prompted me to change the approach I am trying to tackle the problem with. This is my current code:

Sub Method3()
Dim intFieldCount As Integer
Dim i As Integer
Dim vSt1 As String

intFieldCount = ActiveDocument.Fields.Count

For i = 1 To intFieldCount
    ActiveDocument.Fields(i).Select 'selects the first field in the doc
    Selection.Expand
    vSt1 = Selection.Fields(1).Code
    'MsgBox vSt1
    vSt1 = Split(vSt1, " ")(2) 'Find out what the (2) does
    MsgBox vSt1
    ActiveDocument.Bookmarks(vSt1).Select  'Selects the current crossreference in the ref list
Next i
End Sub

Ok the so the Code currently finds the first field in the document, reads its field code and then jumps to the location in the document to mimic a CTRL+Click.

However, It does this for all types of fields Bookmarks, endnotes, figures, tables etc. I only want to find Reference fields. I thought I could deduce this from the field code but it turns out figures and bookmarks use the same field code layout ie.

  1. A Reference/Boookmark has a field code {REF_REF4123123214\h}
  2. A Figure cross ref has the field code {REF_REF407133655\h}

Is there an effective way to get VBA to distinguish between the two? I was thinking as reference fields in the document are written as (Reference 1) I could find the field and then string compare the word on the left to see if it says "Reference".

I was thinking of using the MoveLeft Method to do this

Selection.MoveLeft

But I can't work out how to move left 1 word from the current selection and select that word instead to do the strcomp

Or perhaps I can check the field type? with...

If Selection.Type = wdFieldRef Then
   Do Something
End If

But I am not sure which "Type" i should be looking for.

Any advice is appreciated

1
Can you boil your question down to a specific line of code that you're having trouble with?RubberDuck
Try to use this logic: ActiveDocument.Fields(1).select >> selection.MoveLeft >> selection.TypeText "001 "Kazimierz Jawor
Thanks KazJaw. The code you provided solved half the problem. That inserts the text '001' before the field and not the target the field CTRL+Clicks to. I modified it slightly to do what I need it to. I will Update the original Post with the new code, and a new issue I am having.Calico
RubberDuck, thanks for the reply. I updated the OP with a more specific problem. RegardsCalico

1 Answers

0
votes

All REF fields "reference" bookmarks. Word sets bookmarks on all objects that get a reference for a REF field: figures, headings, etc. There's no way to distinguish from the content of the field what's at the other end. You need to "inspect" that target, which you can do without actually selecting it. For example, you could check whether the first six letters are "Figure".

The code you have is inefficient - there's no need to use the Selection object to get the field code. The following is more efficient:

Sub Method3()
  Dim fld As Word.Field
  Dim rng as Word.Range
  Dim vSt1 As String

  ForEach fld in ActiveDocument.Fields
    vSt1 = fld.Code
    'MsgBox vSt1
    vSt1 = Split(vSt1, " ")(2) 'Find out what the (2) does
    MsgBox vSt1
    Set rng = ActiveDocument.Bookmarks(vSt1).Range
    If Left(rng.Text, 6) <> "Figure" Then
        rng.Select
    End If
  Next 
End Sub