Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    May 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Identify the Field the Cursor Is Currently Located In

    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.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,514
    Thanks
    3
    Thanked 143 Times in 136 Posts
    Selection.Fields(1)
    Selection.Fields(1).Range.Font etc
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    New Lounger
    Join Date
    May 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,894
    Thanks
    0
    Thanked 188 Times in 172 Posts
    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
    Last edited by macropod; 2014-05-17 at 16:53. Reason: Added code that makes use of the WithInField result!
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. The Following User Says Thank You to macropod For This Useful Post:

    RandyMc (2014-05-17)

  6. #5
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    196
    Thanks
    4
    Thanked 46 Times in 38 Posts
    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.

  7. The Following User Says Thank You to jjfreedman For This Useful Post:

    RandyMc (2014-05-17)

  8. #6
    New Lounger
    Join Date
    May 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Lightbulb

    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!

  9. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,894
    Thanks
    0
    Thanked 188 Times in 172 Posts
    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
    [MS MVP - Word]

  10. The Following User Says Thank You to macropod For This Useful Post:

    RandyMc (2014-05-17)

  11. #8
    New Lounger
    Join Date
    May 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •