Word.Tips.Net WordTips (Menu Interface)

Copying and Pasting Field Codes

Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Copying and Pasting Field Codes.

Fields codes can be quite helpful in a document. At some point, you may want to share a field code with someone. Perhaps you are preparing a training document that needs to show the codes as text, or you want to e-mail the field code to someone else.

Problem is, if you simply copy and paste the field code, it isn't the actual code that is pasted; it is the result of that field code. You can, of course, display the field code (Shift+F9), position the insertion point within the braces, carefully select all the characters except the closing brace, and then copy to the Clipboard.

This approach can get tedious in a big hurry, however. A better approach is to use a macro to display the field code and stuff the desired information into the Clipboard. The following macro does just that:

Sub StuffFieldCode()
    Dim sField As String
    Dim sTextCode As String
    Dim bSFC As Boolean
    Dim MyData As DataObject
    Dim sTemp As String
    Dim J As Integer

    Application.ScreenUpdating = False

    If Selection.Fields.Count = 1 Then
        bSFC = Selection.Fields.Item(1).ShowCodes
        Selection.Fields.Item(1).ShowCodes = True
        sField = Selection.Text
        sTextCode = ""
        For J = 1 To Len(sField)
            sTemp = Mid(sField, J, 1)
            Select Case sTemp
                Case Chr(19)
                    sTemp = "{"
                Case Chr(21)
                    sTemp = "}"
                Case vbCr
                    sTemp = ""
            End Select
            sTextCode = sTextCode & sTemp
        Next J

        Set MyData = New DataObject
        MyData.SetText sTextCode
        Selection.Fields.Item(1).ShowCodes = bSFC
    End If

    Application.ScreenUpdating = True
End Sub

The macro begins by turning off the screen updating, then it checks to make sure that the selection includes only one field. (You should select the field you want before running the macro.) If it does contain a single field, then the field code for that field is displayed, assigned to a variable (sField), and then picked apart character by character. If the character being examined is the opening field brace—Chr(19)—then it is replaced with a regular opening brace. If it is a closing field brace—Chr(21)—then it is replaced with a regular closing brace. Finally, if the character is a end-of-paragraph marker (vbCr), then the character is ignored.

Finally, the PutInClipboard method is used to stuff the text version of the field code into the Clipboard. You can then use a regular paste command (Ctrl+V) to paste the field code in either a document, an e-mail, or another program.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (3844) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Copying and Pasting Field Codes.

Related Tips:

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!


Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 5+3 (To prevent automated submissions and spam.)
           Commenting Terms

Comments for this tip:

Frances Cherman    21 Jun 2016, 04:26
Peter, this script worked perfectly – even on Word 2011 for Mac!
Mats Linder    04 May 2016, 12:36
PS. Same thing with Dim MyData As DataObject.
Mats Linder    04 May 2016, 12:34

I get the following error: Compilation error. Own defined type is not defined.

This is for the line
Dim DataObj As New DataObject

I'm grateful for any help.

matt    03 Jun 2014, 09:47

Unfortunately that only works for single copy/paste efforts. I have multiple (>100) lines of text with field codes attached in the front (numbering) I would like to paste all the text into word and have it recognize the field code!
awyatt    18 Feb 2014, 09:45
Scipio: The field code text (the stuff between the field braces) is what is copied to the Clipboard by the macro. If you want to use that in a different field elsewhere, then (1) position the insertion point where you want the new field, (2) press Ctrl+F9 to insert empty field braces, and (3) press Ctrl+V to paste the contents of the Clipboard into those empty field braces.

You can then update the newly created field by pressing F9.

Scipio    18 Feb 2014, 08:21
Wyatt’s Macro works...sort of.
The macro does indeed extract the text of the field code. Upon pasting the text into another document, however, only the text is preserved. Wyatt’s tutorial here is missing the key step: how does one convert the text back into a Microsoft Word field?
Any assistance in answering this question would be immensely appreciated.
Peter Straton    21 Jan 2014, 12:11
OK, a little embarrassed that I didn't test the empty-text condition. The following line,

    DataObj.SetText ClipBoardText

should actually be:

    If ClipBoardText = vbNullString Then
        DataObj.SetText Text:=Empty 'Clear it
        DataObj.SetText Text:=ClipBoardText
    End If

Apologies for any confusion.

Peter Straton
Peter Straton    16 Jan 2014, 13:38
Hi Allen,

Thanks so much for this very useful bit of code! I've taken the liberty of futher generalizing it. Code included below...

Best regards,

Peter Straton

Sub CopyFieldCodeAsText()
    'Copies all field codes from the selected document range to the clipboard, which can then be pasted to other
    'documents or files as text.
    'Author: Peter Straton
    'Credit: Heavily leveraged from code by Allen Wyatt
    ' @ http://word.tips.net/T003844_Copying_and_Pasting_Field_Codes.html

    Dim ClipBoardText As String
    Dim CodeTxt As String
    Dim DataObj As New DataObject 'Allocate it too
    Dim EndPt As Long
    Dim FieldTxt As String
    Dim j As Integer
    Dim SaveSelection As Range
    Dim SaveShowCodes As Boolean
    Dim Separator As String
    Dim TempChr As String

    Application.ScreenUpdating = False

    Set SaveSelection = Selection.Range
    EndPt = Selection.End

    'IMPORTANT: Compound field codes are actually counted as multiple fields by Fields.Count, so the following
    'Do While condition only test whether there are any top-level fields still to be processed. That is, you
    'can't use a For loop on Fields.Count because it doesn't necessarily reflect the number of top-level fields.

    Do While Selection.Fields.Count >= 1
        'There is (still) at least one field in the currently selected range, so process it

        Selection.Fields(1).Select 'Select just the (next) field
        SaveShowCodes = Selection.Fields.Item(1).ShowCodes
        Selection.Fields.Item(1).ShowCodes = True 'So that you can...
        FieldTxt = Selection.Text '...get the field's code as text

        'Parse the field text char-by-char and replace special chars with printable chars

        CodeTxt = ""
        For j = 1 To Len(FieldTxt)
            TempChr = Mid(FieldTxt, j, 1)
            Select Case TempChr
                Case Chr(19) 'ASCII 19 (DC3) is start-field code, which displays as "{", but isn't.
                    TempChr = "{"
                Case Chr(21) 'ASCII 21 (NAK) is end-field code, which displays as "}", but isn't.
                    TempChr = "}"
                Case vbCr 'Compress the text by removing CR chars. [Is this actually of value?]
                    TempChr = ""
            End Select
            CodeTxt = CodeTxt & TempChr
        Next j

        ClipBoardText = ClipBoardText & Separator & CodeTxt
        Selection.Fields.Item(1).ShowCodes = SaveShowCodes 'Restore for this field

        Selection.Collapse wdCollapseEnd 'Move to next field, if any...
        ActiveDocument.Range(Selection.Start, EndPt).Select

        Separator = vbLf 'For multiple fields, separate their code listings on the clipboard with LF chars

    'Write the results to the clipboard. If no fields were found, still overwrite the existing clipboard data
    'with the empty string so that any subsquent Paste operation doesn't paste old data.

    DataObj.SetText ClipBoardText

    Application.ScreenUpdating = True
End Sub
Barry Karas    07 Apr 2013, 17:41
what is a field code? Please give examples.

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2017 Sharon Parq Associates, Inc.