Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Copying and Pasting Field Codes.

Copying and Pasting Field Codes

Written by Allen Wyatt (last updated April 17, 2021)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


2

Field 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 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. Then, if the character is an end-of-paragraph marker (vbCr), that 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 (13300) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word 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

Printing a Range of Pages

If your worksheet, when printed, requires more than a single page to print, you may want to only print a range of the ...

Discover More

Specifying Print Quantity in a Cell

When you print a worksheet, you can specify in the Print dialog box how many copies you want printed. If you want the ...

Discover More

Displaying a Result as Minutes and Seconds

When you use a formula to come up with a result that you want displayed as a time, it can be tricky figuring out how to ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Adding a Dynamic Total in Your Document

You can use a few bookmarks and an equation field to add a dynamic total anywhere in your document. Once in place, you ...

Discover More

Inserting a Document's Path

You can use the FILENAME field to insert a document's filename and, optionally, the path to that filename. However, if ...

Discover More

Fields Won't Update when Printing

When you print a document, Word normally performs several steps, one of which is to update any fields contained in the ...

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?

2021-05-03 23:40:43

Eshwar singh

good tip thanks sir


2021-04-19 10:06:40

Andrew

A very simple way to access the text of a field is using its ".Code" property which includes everything between the braces. Thus, you can copy the field's code by simply using Selection.Fields(1).Code.Copy. Unfortunately, I know of no "one-liner" for copying the code with the braces, but you can simplify Allen's macro quite a bit like this:

Sub StuffFieldCode()
Dim MyData As DataObject
If Selection.Fields.Count = 1 Then
Set MyData = New DataObject
MyData.SetText "{" & Selection.Fields(1).Code.Text & "}"
MyData.PutInClipboard
End If
End Sub

Andy.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu 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.