PDA

View Full Version : Solved: Macro to insert AutoText won't work!



clhare
07-03-2007, 12:48 PM
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!

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


What am I doing wrong??:help

fumei
07-03-2007, 01:34 PM
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: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

clhare
07-03-2007, 04:12 PM
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?

clhare
07-03-2007, 04:39 PM
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?

fumei
07-04-2007, 10:51 AM
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:ActiveDocument.AttachedTemplate.AutoTextEntries("My AutoText")

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". 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

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:Dim aTemplate As Template
For Each aTemplate In Templates
If aTemplate = "CherylGlobal.dot" Then
Msgbox aTemplate
Exit For
End If
Next
would display "CherylGlobal", you can NOT, repeat NOT, Set a template object reference by a string.Dim myTemplate As Template
Set myTemplate = "CherylGlobal"will not work, as it is only as string.For Each aTemplate In Templates
If aTemplate = "CherylGlobal.dot" Then
Set myTemplate = aTemplatesets a reference for myTemplate AS a template object.

fumei
07-04-2007, 11:28 AM
I hope you know that you can use Autotext anywhere...not just at the Selection. It is inserted at the given range parameter.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 SubThis:

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.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 SubThis 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.

clhare
07-06-2007, 05:29 AM
Awesome! It works beautifully! I learn so much from all of you!

:bow:

Sam747
04-18-2019, 10:46 AM
Awesome! It works beautifully! I learn so much from all of you!

:bow: