Consulting

Results 1 to 8 of 8

Thread: Solved: Macro to insert AutoText won't work!

  1. #1
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location

    Solved: Macro to insert AutoText won't work!

    I have a template in my Startup folder that has an AutoText entry in it. I want to create a macro that inserts that AutoText entry into the active document and then do some updates to it.

    I tried using the following code, but I keep getting a message that the requested member of the collection does not exist!

    [vba]Sub InsertMyAutoText()
    Application.DisplayAutoCompleteTips = True
    ActiveDocument.AttachedTemplate.AutoTextEntries("My AutoText"). _
    Insert Where:=Selection.Range, RichText:=True
    End Sub
    [/vba]

    What am I doing wrong??

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Then the template is NOT the "AttachedTemplate".

    What does "then do some updates to it." mean? Updates to what??? The Autotext entry? That is what it sounds like.

    You need to make a Template object of your global template, then use that. Like this:[vba]Sub InsertMyAutoText()
    Dim myTemplate As Template
    Set myTemplate = _
    Templates("FULL path and filename")
    myTemplate.AutoTextEntries("AutoText_Name"). _
    Insert Where:=Selection.Range, RichText:=True
    End Sub[/vba]

  3. #3
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    I didn't include any of the code to update the text once I add it in as the problem was only in getting it added in.

    But here's a question.... I can be in any Word document and insert the AutoText entry manually--I even have assigned it to a Toolbar in this particular Startup template and can insert into any document using the toolbar button. So, if I can do it manually or via the toolbar to any document whether the template is attached to it or not), why can't I do it with a macro?

    And what if not all users are using XP?

  4. #4
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Also, I checked "Tools > Templates and Add-Ins" and my template is listed as loaded in the global template box. So if it's loaded already, why won't the macro work?

    Is there a way to retrieve the exact path to the Startup folder in case it's different for different users?

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    So, if I can do it manually or via the toolbar to any document whether the template is attached to it or not), why can't I do it with a macro?
    I don't understand. You can.

    A global template is NOT an attached template. Your code was:[vba]ActiveDocument.AttachedTemplate.AutoTextEntries("My AutoText")[/vba]

    So....ummmm....well, it is NOT the attached template. So if the AutoText is not in the attached template, you get an error - "the requested member of the collection does not exist".

    Makes sense to me.

    You can get it manually, because a global template gives access to it. To use Autotext by code, you need a reference to the template that holds it. How else can VBA find it? You gave .AttachedTemplate, and VBA looked there...and it was not there. So, requested member does not exist. And....it doesn't.
    Also, I checked "Tools > Templates and Add-Ins" and my template is listed as loaded in the global template box. So if it's loaded already, why won't the macro work?

    Is there a way to retrieve the exact path to the Startup folder in case it's different for different users?
    I don't think you are quite getting how a global works.

    As for the last part, there is no need to get the Startup path. If you know the name of the global, and you know it is loaded, then you can set a reference for it, and use it.

    Say your global is "CherylGlobal.dot", and the Autotext is "My AutoText". [vba]Sub UseGlobalAT()
    Dim aTemplate As Template
    Dim myTemplate As Template
    For Each aTemplate In Templates
    If aTemplate = "CherylGlobal.dot" Then
    Set myTemplate = aTemplate
    myTemplate.AutoTextEntries("My AutoText"). _
    Insert Where:=Selection.Range, RichText:=True
    Exit For
    End If
    Next
    End Sub[/vba]

    By default - look in Insert > Autotext - Word uses "look in all active templates". THIS is why you can manually insert the Autotext. Globals are active templates.....but NOT the AttachedTemplate.

    The code above does not require knowing the path to Startup. It looks in the Addins collection, and if the named template is there (which it is if it is loaded), then it makes a reference object to it (a Template object), THEN it can use the Autotext entry.

    VBA need to know the template to use. Here are its choices:

    NormalTemplate.AutotextEntries
    AttachedTemplate.AutotextEntries
    template_object.AutotextEntries

    A global is not the first, or second, possibility. So you MUST make a object reference for it.

    NOTE: you can not use a string to set a template object reference.

    Even though:[vba]Dim aTemplate As Template
    For Each aTemplate In Templates
    If aTemplate = "CherylGlobal.dot" Then
    Msgbox aTemplate
    Exit For
    End If
    Next[/vba]
    would display "CherylGlobal", you can NOT, repeat NOT, Set a template object reference by a string.[vba]Dim myTemplate As Template
    Set myTemplate = "CherylGlobal"[/vba]will not work, as it is only as string.[vba]For Each aTemplate In Templates
    If aTemplate = "CherylGlobal.dot" Then
    Set myTemplate = aTemplate[/vba]sets a reference for myTemplate AS a template object.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I hope you know that you can use Autotext anywhere...not just at the Selection. It is inserted at the given range parameter.[vba]Sub UseGlobalAT()
    Dim aTemplate As Template
    Dim myTemplate As Template
    Dim r As range
    Set r = ActiveDocument.Section(3).Headers(2).Range
    For Each aTemplate In Templates
    If aTemplate = "VBA XTools.dot" Then
    Set myTemplate = aTemplate
    myTemplate.AutoTextEntries("Logo2"). _
    Insert Where:=r, RichText:=True
    Exit For
    End If
    Next
    End Sub[/vba]This:

    1. makes a Range object of FirstPage header of Section 3
    2. checks to see if "VBA XTools.dot" is loaded
    2. if it is, inserts the Autotext "Logo2" into the Range object

    No need to use Selection. No need to View the header, yadda yadda yadda. Just bang! put the Autotext into the FirstPage Header of Section 3 of the ActiveDocument.

    This can also be used in conjunction with the sort of batch processing discussed on other threads.[vba]Sub UpdateFolderFiles()
    Dim aTemplate As Template
    Dim myTemplate As Template
    Dim r As Range
    Dim myFile
    Dim bolSet As Boolean
    Const sPath As String = "c:\yadda\"

    For Each aTemplate In Templates
    If aTemplate = "Hummmmm.dot" Then
    Set myTemplate = aTemplate
    bolSet = True
    Exit For
    End If
    Next
    ' global not found (loaded)?
    If bolSet = False Then
    MsgBox "Hey....global is NOT loaded!"
    Exit Sub
    End If
    ' otherwise myTemplate now SET and useable
    myFile = Dir(sPath & "*.doc")
    Do While myFile <> ""
    Documents.Open FileName:=sPath & myFile
    Set r = ActiveDocument.Sections(1).Footers(1).Range
    myTemplate.AutoTextEntries("Logo2"). _
    Insert Where:=r, RichText:=True
    ActiveDocument.Save wdSaveChanges
    ActiveDocument.Close
    myFile = Dir
    Loop
    End Sub[/vba]This would process all .doc files in the folder c:\yadda\, inserting the Autotext "Logo2" into the Footer of each document IF the global "Hummmmm.dot" is loaded.

  7. #7
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Awesome! It works beautifully! I learn so much from all of you!


  8. #8
    VBAX Newbie
    Joined
    Apr 2019
    Posts
    1
    Location
    Quote Originally Posted by clhare View Post
    Awesome! It works beautifully! I learn so much from all of you!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •