Written by Allen Wyatt (last updated October 6, 2018)
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:
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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If you enter your address into Word, you can insert that address anywhere you want in a document by using a single field. ...
Discover MoreOne of the pieces of information tracked by Word is when a document was first created. Here's how you can access that ...
Discover MoreTrack Changes is a great feature for keeping track of what gets changed in a document. There are some things (such as ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-03-08 11:05:31
chrissy
I would love to use macros, but i've never actually had one that worked. I just tried to copy/paste this into a new macro and i get an error message.
2021-08-19 22:42:45
sunny
How to copy field code formula as follows:
{QUOTE
{SET Delay -1}
{SET m{=MOD({DATE \@ MM}+Delay+11,12)+1}}
{SET y{=INT({DATE \@ yyyy}+(Delay+{DATE \@ M}-1)/12)}}
"{m}-{y}" \@ "MMM yyyy" \*Upper}
is there another way other than Ctrl-F9 for every set of braces ?
Thanks Ahead!
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments