How do you copy raw Microsoft Word fields codes to another text document?
TWood's comment is correct that with the solution from integratorIT all formatting is lost. The following macro FieldToText REPLACES all fields in your document with their field codes as raw text WITHOUT touching formatting.
The second macro TextToField is for the opposite way: raw code to fields. It looks for the pattern { * }
and tries to convert that to a field. It will work for the fields' raw texts inserted from the first macro, but it may give unpredicted results if there are those combinations of curly braces and whitespace somewhere else in the text where they do not indicate a (former) field function.
Sub FieldToText()
'Selection.HomeKey Unit:=wdStory ' to start from top of document
ActiveWindow.View.ShowFieldCodes = True
Do
With Selection.Find
.Text = "^d"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found = False Then Exit Do
Selection = "{ " & Mid(Selection, 3, Len(Selection) - 2 - 2) & " }"
Selection.Move wdCharacter, 1
Loop While True
End Sub
Sub TextToField()
Dim code As String
'Selection.HomeKey Unit:=wdStory ' to start from top of document
Do
With Selection.Find
.Text = "\{ * \}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found = False Then Exit Do
code = Mid(Selection, 3, Len(Selection) - 2 - 2)
Selection.Cut
Selection.InsertAfter (code)
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.MoveRight Unit:=wdWord, Count:=1
Loop While True
End Sub
Related videos on Youtube
Faye Dyce
Updated on September 17, 2022Comments
-
Faye Dyce over 1 year
Is it possible to copy text from a Word document containing field codes, so that when pasted into another application, the FIELD CODES REMAIN AS RAW TEXT, i.e. something like
This is about whales { XE "Cetations:Whales" }. This is about dolphins { XE "Cetations:Dolphins" }.
rather than have the field codes stripped out?
-
Faye Dyce about 14 yearsPerhaps I havn't explained it very clearly. I don't want to cut and paste individual field codes one at a time, but rather the body text of entire document with the field codes kept intact...
-
ChrisF about 14 yearsyou've managed to create two accounts. E-mail [email protected] to get them merged.
-
-
integratorIT about 12 yearsIn office 2010 its ALT+F9 cant check right now its the same in previous versions.
-
TWood almost 7 yearsfor this code to run you have to add microsoft forms 2.0 reference to macro. It is located c:\windows\system32\FM20.dll. Unfortunately even though the code copies the text representing the field code all of the formatting is lost when it is copied. When you paste back it is simply text.