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.

Copying and Pasting Field Codes

Written by Allen Wyatt (last updated June 1, 2024)
This tip applies to Word 97, 2000, 2002, and 2003


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
        MyData.PutInClipboard
        
        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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Displaying Blanks when Summing to Zero

If you use fields to sum the information in your table columns, you might want to display a blank when the sum is zero. ...

Discover More

Doubling Your Money

Make your money last longer by using your head when printing labels. Here's a great example of how you can double the ...

Discover More

Altering the Displayed Format of Numbers to the Nearest 100

Want information in a worksheet to be formatted and displayed as rounded to a power of ten? You may be out of luck, ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (menu)

Inserting the Total Number of Pages in Your Document

Word keeps track of many statistics for each of your documents. One statistic is the total number of pages in the printed ...

Discover More

Age Calculation with Fields

People don't normally think of using fields to do any calculations. Even so, you can use fields to perform a simple ...

Discover More

Tools to View Field Codes

Fields can be used to add all sorts of dynamic data to your documents. Viewing the field codes, at times, is desirable. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.