How to place variable in word document
I have a word document that I use repeatedly but need to change one phrase that appears appears more than once in the document. Is there a way to make this phrase a variable and update it once instead of searching through the document each time to update it?
I expect you want to be using Mapped Content Controls.
There is a set of these mislabeled as Document Properties
under the Quick Parts
drop-down on the Insert tab
. These are Mapped Content Controls that mostly correspond to advanced Document Properties. Here is my page on inserting and using those. This is not the only way to do what you want but is the most efficient in .docx format documents.
This and other methods of replicating data are given in Greg Maxey's page Repeating Data. This is the page linked by David Postill in his comment. The mapped content controls, though, unlike other methods, simply require a change made in one place for that change to be reproduced throughout a document.
Any of the general-use content controls can be mapped but those in the Document Property list are plain text except for one which is a date picker.
Once one of these is inserted in a document, the name and prompt for it can be changed and then it can be copied to other places in the document where you want the information.
Note that these are set so a change made to the control anywhere in the document changes the information in all copies. Other methods for replicating data require more attention to update the copies as in updating fields.
Content Controls are most easily created/modified in the Windows versions. On the Mac they are more difficult to set up but can be done. You can copy them from a Windows-created document or create them using vba. Here is Word MVP John Korchok's page on how to do these on a Mac.
So one way is to "place the variable" using easy-to-identify text in the document and save the document as a template. Then use find and replace to find the "variable" and replace with the phrase.
I usually do something like {BOOKMARK_PHRASE_ONE}
where the phrase would normally go. And a find-and-replace operation with "replace all" works in one fell swoop.
For complicated business documents, I would use a macro (again saved in a template file) that can handle these things and then call a function in the macro to replace multiple things. I cannot post a whole process here, but what I do is design VBA Forms to collect the information I need, and then use a find-and-replace procedure wrapped in a function that can be called repeatedly.
Basically: you take the document, you type {BOOKMARK_PHRASE_ONE}
in one or multiple places, you set up a macro to do the heavy lifting, and then save it as a template. Subsequently opening the template file opens as a copy, and then you run the macro which sets your text. The copies get saved as normal documents, when you need to modify the business process (boiler-plate text, VBA etc.), you "save as template" again.
The VBA might look roughly like this:
Const txtFind_PHRASE_ONE = "{BOOKMARK_PHRASE_ONE}"
Const txtReplace_PHRASE_ONE = "This would be phrase one."
Const txtFind_PHRASE_TWO = "{BOOKMARK_PHRASE_TWO}"
Const txtReplace_PHRASE_TWO = "This would be phrase two."
sub replacePhrases()
replacePhrase txtFind_PHRASE_ONE, txtReplace_PHRASE_ONE
replacePhrase txtFind_PHRASE_TWO, txtReplace_PHRASE_TWO
end sub
Private sub replacePhrase(txtFind as string, txtReplace as string)
With Selection.Find
.Text = txtFind
.Replacement.Text = txtReplace
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
end sub
The const
declarations for the phrases at the top would normally be variables that I fill using a form. The Sub named replacePhrases
would be visible in the "Macros" dialog and would be the macro I would run to start the process. The Sub named replacePhrase
is marked private so it will not show up in the macro dialog; you pass pairs of find + replace text to it.