Field.Code property (Word)
Returns a Range object that represents a field's code. Read/write.
Syntax
expression. Code
expression A variable that represents a 'Field' object.
Remarks
A field's code is everything that's enclosed by the field characters ({ }) including the leading space and trailing space characters. You can access a field's code without changing the view from field results.
Example
This example displays the field code for each field in the active document.
Dim fieldLoop As Field
For Each fieldLoop In ActiveDocument.Fields
MsgBox Chr(34) & fieldLoop.Code.Text & Chr(34)
Next fieldLoop
This example changes the field code for the first field in the active document to CREATEDATE.
Dim rngTemp As Range
Set rngTemp = ActiveDocument.Fields(1).Code
rngTemp.Text = " CREATEDATE "
ActiveDocument.Fields(1).Update
This example determines whether the active document includes a mail merge field named "Title."
Dim fieldLoop As Field
For Each fieldLoop In ActiveDocument.MailMerge.Fields
If InStr(1, fieldLoop.Code.Text, "Title", 1) Then
MsgBox "A Title merge field is in this document"
End If
Next fieldLoop
See also
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for