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, 1 month 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 LoungerViewing 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
-
The 16-billion-record data breach that no one’s ever heard of
by
Alex5723
1 hour, 33 minutes ago -
Weasel Words Rule Too Many Data Breach Notifications
by
Nibbled To Death By Ducks
2 hours, 9 minutes ago -
Windows Command Prompt and Powershell will not open as Administrator
by
Gordski
5 hours, 24 minutes ago -
Intel Management Engine (Intel ME) Security Issue
by
PL1
8 hours, 20 minutes ago -
Old Geek Forced to Update. Buy a Win 11 PC? Yikes! How do I cope?
by
RonE22
2 hours, 31 minutes ago -
National scam day
by
Susan Bradley
1 hour, 34 minutes ago -
macOS Tahoe 26 the end of the road for Intel Macs, OCLP, Hackintosh
by
Alex5723
1 day, 1 hour ago -
Cyberattack on some Washington Post journalists’ email accounts
by
Bob99
1 day, 3 hours ago -
Tools to support internet discussions
by
Kathy Stevens
1 day, 10 hours ago -
How get Group Policy to allow specific Driver to download?
by
Tex265
18 hours, 18 minutes ago -
AI is good sometimes
by
Susan Bradley
1 day, 10 hours ago -
Mozilla quietly tests Perplexity AI as a New Firefox Search Option
by
Alex5723
1 day ago -
Perplexity Pro free for 12 mos for Samsung Galaxy phones
by
Patricia Grace
2 days, 10 hours ago -
June KB5060842 update broke DHCP server service
by
Alex5723
2 days, 9 hours ago -
AMD Ryzen™ Chipset Driver Release Notes 7.06.02.123
by
Alex5723
2 days, 13 hours ago -
Excessive security alerts
by
WSSebastian42
1 day, 4 hours ago -
* CrystalDiskMark may shorten SSD/USB Memory life
by
Alex5723
2 days, 22 hours ago -
Ben’s excellent adventure with Linux
by
Ben Myers
15 hours, 52 minutes ago -
Seconds are back in Windows 10!
by
Susan Bradley
2 days, 9 hours ago -
WebBrowserPassView — Take inventory of your stored passwords
by
Deanna McElveen
1 day, 3 hours ago -
OS news from WWDC 2025
by
Will Fastie
13 hours, 36 minutes ago -
Need help with graphics…
by
WSBatBytes
1 day, 18 hours ago -
AMD : Out of Bounds (OOB) read vulnerability in TPM 2.0 CVE-2025-2884
by
Alex5723
3 days, 14 hours ago -
Totally remove or disable BitLocker
by
CWBillow
2 days, 13 hours ago -
Windows 10 gets 6 years of ESU?
by
n0ads
2 days, 16 hours ago -
Apple, Google stores still offer China-based VPNs, report says
by
Nibbled To Death By Ducks
4 days ago -
Search Forums only bring up my posts?
by
Deo
5 hours, 52 minutes ago -
Windows Spotlight broken on Enterprise and Pro for Workstations?
by
steeviebops
2 hours, 26 minutes ago -
Denmark wants to dump Microsoft for Linux + LibreOffice
by
Alex5723
16 hours, 49 minutes ago -
How to get Microsoft Defender to honor Group Policy Setting
by
Ralph
9 hours, 49 minutes 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.