Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 Bookmard (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.

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 so I can feed that number into the macro to work with. The person doing the editing simply clicks on an icon that runs the macro that takes the current Field they are working on, and changes the font color to red.

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
Posted
Updated 16-May-14 21:13pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900