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.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
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.
Viewing 5 reply threadsAuthorReplies-
Andrew Lockton
AskWoody_MVP -
WSRandyMc
AskWoody LoungerMay 17, 2014 at 2:07 am #1453338No, 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 -
mvpjjf
AskWoody PlusMay 17, 2014 at 4:11 pm #1453382RandyMc: 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.
-
-
macropod
AskWoody_MVPMay 17, 2014 at 3:46 am #1453341Try:
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] -
WSRandyMc
AskWoody LoungerMay 17, 2014 at 5:17 pm #1453390Paul, 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!
-
macropod
AskWoody_MVPMay 17, 2014 at 5:31 pm #1453392Have 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] -
WSRandyMc
AskWoody Lounger
Viewing 5 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Woody
by
Scott
7 hours, 32 minutes ago -
24H2 has suppressed my favoured spider
by
Davidhs
7 hours, 33 minutes ago -
GeForce RTX 5060 in certain motherboards could experience blank screens
by
Alex5723
21 hours, 44 minutes ago -
MS Office 365 Home on MAC
by
MickIver
15 hours, 33 minutes ago -
Google’s Veo3 video generator. Before you ask: yes, everything is AI here
by
Alex5723
1 day, 11 hours ago -
Flash Drive Eject Error for Still In Use
by
J9438
1 day, 13 hours ago -
Windows 11 Insider Preview build 27863 released to Canary
by
joep517
2 days, 6 hours ago -
Windows 11 Insider Preview build 26120.4161 (24H2) released to BETA
by
joep517
2 days, 6 hours ago -
AI model turns to blackmail when engineers try to take it offline
by
Cybertooth
1 day, 10 hours ago -
Migrate off MS365 to Apple Products
by
dmt_3904
1 day, 11 hours ago -
Login screen icon
by
CWBillow
1 day, 1 hour ago -
AI coming to everything
by
Susan Bradley
10 hours, 50 minutes ago -
Mozilla : Pocket shuts down July 8, 2025, Fakespot shuts down on July 1, 2025
by
Alex5723
2 days, 22 hours ago -
No Screen TurnOff???
by
CWBillow
2 days, 22 hours ago -
Identify a dynamic range to then be used in another formula
by
BigDaddy07
2 days, 23 hours ago -
InfoStealer Malware Data Breach Exposed 184 Million Logins and Passwords
by
Alex5723
3 days, 10 hours ago -
How well does your browser block trackers?
by
n0ads
2 days, 21 hours ago -
You can’t handle me
by
Susan Bradley
15 minutes ago -
Chrome Can Now Change Your Weak Passwords for You
by
Alex5723
2 days, 13 hours ago -
Microsoft: Over 394,000 Windows PCs infected by Lumma malware, affects Chrome..
by
Alex5723
3 days, 22 hours ago -
Signal vs Microsoft’s Recall ; By Default, Signal Doesn’t Recall
by
Alex5723
3 days, 1 hour ago -
Internet Archive : This is where all of The Internet is stored
by
Alex5723
3 days, 22 hours ago -
iPhone 7 Plus and the iPhone 8 on Vantage list
by
Alex5723
3 days, 22 hours ago -
Lumma malware takedown
by
EyesOnWindows
3 days, 10 hours ago -
“kill switches” found in Chinese made power inverters
by
Alex5723
4 days, 7 hours ago -
Windows 11 – InControl vs pausing Windows updates
by
Kathy Stevens
4 days, 7 hours ago -
Meet Gemini in Chrome
by
Alex5723
4 days, 11 hours ago -
DuckDuckGo’s Duck.ai added GPT-4o mini
by
Alex5723
4 days, 11 hours ago -
Trump signs Take It Down Act
by
Alex5723
4 days, 19 hours ago -
Do you have a maintenance window?
by
Susan Bradley
3 days ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.