I have experienced that you _can_ actually create an AutoText via VBA and set
the value to whatever string you want without first inserting the string in
the document. The idea is to first create the AutoText containing whatever is
selected in the document (or you could define another range of your wish).
Then you can use the Value property of the AutoText to replace the content
with the desired string.
An example of such macro is found below. Replace the name, template and new
value as desired.
Sub CreateAutoTextViaVBA()
Dim oAutoText As AutoTextEntry
'Create an AutoText with the current selection as the content
'You could instead use any range from the document
Set oAutoText =
Templates(ActiveDocument.AttachedTemplate).AutoTextEntries _
.Add(Name:="MyName", Range:=Selection.Range)
'Now replace the content of the AutoText with the desired value
With oAutoText
.Value = "ThisIsMyNewValue"
End With
'Clean up
Set oAutoText = Nothing
End Sub
Correspondingly, you can change the contents of any existing AutoText – or
you can replace a certain string in any AutoText.
Example: you want to replace the string “abc†in all AutoText entries in a
specific template (here MyTemplate) with “12345â€. To do this:
Dim oAutoText As AutoTextEntry
For Each oAutoText In MyTemplate.AutoTextEntries
oAutoText.Value = Replace(oAutoText.Value, "abc", "12345")
Next oAutoText
(I have not included any error handling in the examples above).
--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word