Share via

VBA code for inserting autotext

Anonymous
2018-02-07T01:43:38+00:00

Hello, I am having problems with inserting autotext into a document.  I have about 5 separate autotext paragraphs.  I have a macro that goes to a bookmark and at the bookmark inserts autotext - nothing is being inserted at all. 

This is the code I am using. 

Sub POASattJointlyOrSeverally()

'

' POASattJointlyOrSeverally Macro

'

'

    Selection.HomeKey Unit:=wdStory

    Selection.GoTo What:=wdGoToBookmark, Name:="SubAttorneysToAct"

    With ActiveDocument.Bookmarks

        .DefaultSorting = wdSortByName

        .ShowHidden = False

    End With

    Selection.TypeText Text:="POASattJointlyOrSeverally"

    Selection.Range.InsertAutoText

    Selection.Delete Unit:=wdCharacter, Count:=1

    Selection.HomeKey Unit:=wdStory

End Sub

Thank you

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2018-02-07T05:43:07+00:00

    if you type POASattJointlyOrSeverally and then press F3, is the autotext inserted?

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2018-02-07T09:53:53+00:00

    Use the following macro to insert your autotext entry. The macro will need to know where the autotext entry is stored. The most likely place will be the document template so call it from there e.g.

    AutoTextToBM "SubAttorneysToAct", ActiveDocument.AttachedTemplate, "POASattJointlyOrSeverally"

    If the bookmark is missing from the document and/or the autotext entry is not in the named template then nothing will be inserted.

    Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)

    'strBMName is the name of the bookmark to fill

    'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate

    'strAutotext is the name of the autotext entry

    Dim oRng As Range

        On Error GoTo lbl_Exit

        With ActiveDocument

            Set oRng = .Bookmarks(strbmName).Range

            Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _

                       (Where:=oRng, RichText:=True)

            .Bookmarks.Add Name:=strbmName, Range:=oRng

        End With

    lbl_Exit:

        Exit Sub

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments