Option Explicit

Sub CreateAutoTextWithoutInsertingContentInDocument()

    '=========================
    'Macro created 2008 by Lene Fredborg, DocTools - www.thedoctools.com
    'THIS MACRO IS COPYRIGHT. YOU ARE WELCOME TO USE THE MACRO BUT YOU MUST KEEP THE LINE ABOVE.
    'YOU ARE NOT ALLOWED TO PUBLISH THE MACRO AS YOUR OWN, IN WHOLE OR IN PART.    
    '=========================
    'The macro illustrates how you can create an AutoText
    'without first inserting the contents in a document
    'You may need to include error handling
    'NOTE: YOU CAN ONLY CREATE AUTOTEXT WITH PLAIN TEXT UP TO 255 CHARACTERS USING THIS METHOD
    '=========================
    
    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
    oAutoText.Value = "ThisIsMyNewValue"
        
    'Clean up
    Set oAutoText = Nothing

End Sub

Sub EditExistingAutoTextsWithoutInsertingContentInDocument()
    
    '=========================
    'Macro created 2008 by Lene Fredborg, DocTools - www.thedoctools.com
    'THIS MACRO IS COPYRIGHT. YOU ARE WELCOME TO USE THE MACRO BUT YOU MUST KEEP THE LINE ABOVE.
    'YOU ARE NOT ALLOWED TO PUBLISH THE MACRO AS YOUR OWN, IN WHOLE OR IN PART.    
    '=========================
    'The macro illustrates how you can replace a string in existing AutoTexts
    'without first inserting the AutoTexts
    'You may need to include error handling
    'IMPORTANT: DO NOT USE FOR AUTOTEXTS WITH RICH TEXT FORMATTING - THE FORMATTING WILL BE LOST
    'YOU CAN ONLY EDIT THE FIRST 255 CHARACTERS OF AN AUTOTEXT VALUE USING THIS METHOD
    '=========================
    
    Dim oAutoText As AutoTextEntry
    Dim oTemplate As Template
    Dim strOld As String
    Dim strNew As String
    
    'Define the string to be replaced - change to the desired value
    strOld = "abc"
    'Define the replacement string - change to the desired value
    strNew = "12345"
    
    'Make sure to identify the correct template - here the attached template
    Set oTemplate = ActiveDocument.AttachedTemplate
    
    'Iterate through all AutoTexts in oTemplate and replace strings
    
    For Each oAutoText In oTemplate.AutoTextEntries
        oAutoText.Value = Replace(oAutoText.Value, strOld, strNew)
    Next oAutoText

    'Clean up
    Set oTemplate = Nothing

End Sub