• Identify the Field the Cursor Is Currently Located In

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Identify the Field the Cursor Is Currently Located In

    • This topic has 7 replies, 4 voices, and was last updated 11 years ago.
    Author
    Topic
    #494788

    I am trying to write a macro, part of which selects the Field (or Bookmark) where the insertion point is currently located. I would like to select the Field, or Bookmark (i.e. the text in the Field), do an ActiveDocument.Fields(FieldName).Unlink command, then set the font color to Red. I just can’t figure out how to get the Field Name (Bookmark Name) where the cursor is currently located. All of the fields have Bookmark names attached to them.

    Viewing 5 reply threads
    Author
    Replies
    • #1453336

      Selection.Fields(1)
      Selection.Fields(1).Range.Font etc

    • #1453338

      No, that isn’t what I need. Your code just selects Field #1 and does things with it; I am working with a document with over 100 Fields in it. I need a way to identify which particular Field the user is currently editing, either by Field Number, or Bookmark Name (all the Fields have Bookmark Names). For example, I can do the following, but it only works on Field #2. This selects the field, checks to see if the font color is red, and if not, then changes it to red, then Unlinks the field and replaces it with the text that was in the field, but I have to specify the Field number, which is unknown to the macro while the document is being edited. I need a way to identify the Field number or Bookmark name that is currently being edited. Thanks for your suggestion though.

      fieldsItem = ActiveDocument.Fields.Item(2)
      fieldStart = fieldsItem.Start
      fieldEnd = fieldsItem.End
      ActiveDocument.Range(Start:=fieldStart, End:=fieldEnd).Select
      If Selection.Font.Color wdColorRed Then
      Selection.Font.Color = wdColorRed
      End If
      ActiveDocument.Fields(2).Unlink

      • #1453382

        RandyMc: To clear up a possible misconception: Selection.Fields(1) does not mean the same thing as ActiveDocument.Fields(1). Instead, Selection.Fields(1) refers to the first field of which any part is contained in the Selection. If the Selection happens to be in the 15th field in the document, then Selection.Fields(1) and ActiveDocument.Fields(15) are the same field. Paul’s code uses the same idea, except that it mentions the .Fields(1) of a Range object.

    • #1453341

      Try:

      Code:
      Sub ColourUnlinkField()
      Application.ScreenUpdating = False
      Dim Rng As Range, bRslt As Boolean
      Set Rng = Selection.Range
      bRslt = WithInField(Rng)
      If bRslt = False Then
        MsgBox "Not in a field."
        Exit Sub
      End If
      With Rng.Duplicate
        .Start = Selection.Start
        .Fields(1).Result.Font.ColorIndex = wdRed
        .Fields.Unlink
      End With
      Rng.Select
      Application.ScreenUpdating = True
      End Sub
      
      Function WithInField(Rng As Range) As Boolean
      ' Based on code by Don Wells: http://www.eileenslounge.com/viewtopic.php?f=30&t=6622
      Dim i As Long, j As Long
      WithInField = True
      Rng.Select
      i = Selection.Start
      j = Selection.End
      With Selection
        .Fields.ToggleShowCodes
        .Fields.ToggleShowCodes
        ' Test whether the selection has moved; if not, it may already have been _
          at the start of a field, in which case, move right and test again.
        If .Start = i Then
          .MoveRight
          .Fields.ToggleShowCodes
          .Fields.ToggleShowCodes
          If .Start = i + 1 Then
            WithInField = False
          End If
        End If
      End With
      End Function

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1453390

      Paul, thanks for your help, but it isn’t quite there yet. The user wouldn’t invoke the macro unless they are in a field that needs the font color changed, so I don’t really have to check whether they are in a field or not – I just need to know which Field they are in. I am thinking maybe my best bet is to build an array containing the names of the Bookmarks, or the Field numbers, along with the range of each. I could store the Field number as an integer type and the beginning and ending positions as a Range type. (The trouble I see with this is that I would have to build the array every time the macro is invoked, as the range of the Field could/would change depending on whatever the user adds to/deletes from the document before they get to the field.) I can then search the array for the Bookmark name or Field number using the cursor position in the document to find which Field the cursor is in, i.e. check each Range in the array to see which one the current cursor position is in, then return the Bookmark name or Field number associated with that Range from the array. I would just need a function to count the characters up to and including the cursor to know where it currently is, then check to see which range of each Bookmark/Field it is in and return the name or Field number. Hmm….have to think on this some more. Thanks for you help!

    • #1453392

      Have you actually tried the code??? Sure it uses a function to test whether the selection is in a field – and tells you if it isn’t – but that’s not all it’s used for. Simply having the selection in a field isn’t enough to do anything with it – you need to get the field’s range too, and that’s another thing the function helps with. Even without the function, though, you don’t need to build an array of the document’s fields! At most you’d need:

      Code:
      Sub ColourUnlinkField()
      Application.ScreenUpdating = False
      On Error Resume Next
      Dim Rng As Range
      With ActiveDocument
      Set Rng = .Range(0, Selection.Range.End)
        With Rng
          .Fields(.Fields.Count).Result.Font.ColorIndex = wdRed
          .Fields(.Fields.Count).Unlink
        End With
      End With
      Application.ScreenUpdating = True
      End Sub

      Do note that with this code, though, if the selection isn’t in a field, the last preceding field (if there is one) will the coloured and unlinked…

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1453394

      I did try your first code and it didn’t work like I wanted. However, your second set of code seems to work perfectly! Thank you!

    Viewing 5 reply threads
    Reply To: Identify the Field the Cursor Is Currently Located In

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: