• AutoText Exists

    Author
    Topic
    #351996

    What’s the best method for checking for the existence of an autotext entry?

    Viewing 1 reply thread
    Author
    Replies
    • #510784

      Iterate thru (enumerate) the collection testing for the autotext .name in the for loop.

      • #510926

        How about showing us a code sample for this?

        • #510967

          Kevin, do you ever get the feeling you are talking to yourself?

          Use this code and modify it as many times as all of you want. Feel free if each of you want to post your answers.

          Dim Entry
          ‘Form types listing – Autofill the list with Autotexts in template
          For Each Entry In ActiveDocument.AttachedTemplate.AutoTextEntries
          Me.FormTypeLB.AddItem Entry.Name
          Next Entry

          • #510978

            Hey! I resemble that, and so do I! Thanks for helping us,
            Andrew. I think I’ll post some code too. Me, too.

            Here’s a function that tests to see if an autotext entry exists in a template. The template name and autotext piece name are parameters.

            Function AutoTextExists(aTemplate As Template, aPiece As String) As Boolean
            Dim aEntry As AutoTextEntry
            
                On Error GoTo BadName
                For Each aEntry In aTemplate.AutoTextEntries
                    If UCase(aEntry.Name) = aPiece Then
                        AutoTextExists = True
                        Exit Function
                    End If
                Next
                AutoTextExists = False
                Exit Function
            BadName:
                MsgBox "The template "+cstr(aTemplate)+ " or document text requested" + aPiece + " does not exist. ", vbCritical + vbOKOnly, "Autotext Retrieval Error"
            End Function
    • #510999

      No need loop through all AutoText entries. Try this instead:

      Function AutoTextExists(AutoTextName As String, _
      Optional InsertIfExists As Boolean) As Boolean
      Dim DOT As Template
      Dim ATX As AutoTextEntry
      ‘ Loop through all available templates

      • #511041

        Hi Don,

        That’s a good solution. A bit more elegant than mine. I especially like setting the autotext entry to an object and letting Word error if it does not exist. Thanks!

    Viewing 1 reply thread
    Reply To: AutoText Exists

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: