Search code examples
vbams-wordbookmarks

Mark inserted AutoText with bookmark VBA


I want to insert AutoText (with styles and colours) in the document en mark this range as a bookmark with Word VBA. I know how to do this with normal text, but I can't figure out how to mark the inserted Autotext. Help would be very welcome and appreciated.

Kind regards, Kem


Solution

  • When you insert AutoText, it returns a Range object representing the inserted AutoText. Assuming doc is your Document and you want to insert the AutoText item at the Selection:

    ' add AutoText item
    Dim rng As Range
    Set rng = doc.AttachedTemplate.AutoTextEntries.Item("MyAutoTextItem").Insert(Selection.Range, True)
    
    ' set it to be a Bookmark
    Dim bkmk As Bookmark
    Set bkmk = doc.Bookmarks.Add("MyBookmark", rng)
    

    ... obviously adjust "MyAutoTextItem" and "MyBookmark" for your requirements