Inserting AutoText Entries from another Template

S

Stuart Troy

I have created a template containing AutoText entries.

How can I create a new blank document based upon Normal.DOT and insert the
AutoText entries from my custom template - without referencing a filename
path to my custom template, as its location is likely to change.
 
J

Jonathan West

Stuart Troy said:
I have created a template containing AutoText entries.

How can I create a new blank document based upon Normal.DOT and insert the
AutoText entries from my custom template - without referencing a filename
path to my custom template, as its location is likely to change.

If you put your custom template in the Startup folder, it will be loaded as
an add-in and its autotext entries will be available to the user.

You can reference a member of the add-ins collection by means of its
filename, without needing the full pathname.
 
S

Stuart Troy

Thanks Jonathon,

I moved the custom template to Word's startup folder;
C:\Documents and Settings\UsernameApplication Data\Microsoft\Word\STARTUP

But when I run this code, it produces an error;

VBA Code;
Documents.Add DocumentType:=wdNewBlankDocument
ActiveDocument.AttachedTemplate.AutoTextEntries("AutoText_Name"). Insert
Where:=Selection.Range, RichText:=False

Resulting Error;
"The requested member of the collection does not exist"
 
J

Jonathan West

Stuart Troy said:
Thanks Jonathon,

I moved the custom template to Word's startup folder;
C:\Documents and Settings\UsernameApplication Data\Microsoft\Word\STARTUP

But when I run this code, it produces an error;

VBA Code;
Documents.Add DocumentType:=wdNewBlankDocument
ActiveDocument.AttachedTemplate.AutoTextEntries("AutoText_Name"). Insert
Where:=Selection.Range, RichText:=False

Resulting Error;
"The requested member of the collection does not exist"

That is because you are specifying that thst autotext entry must come from
the ActiveDocument.AttachedTemplate. It isn't there, it is in the add-in.
Suppose your addin id called MyAddin.dot. You need to identidy the aadd-in
and then use that as the source of the Autotext entry. Something like this

Dim oTemplate as Template
For each oTemplate in Templates
If oTemplate.Name = "MyAddin.dot" Then Exit For
Next oTemplate
oTemplate.AutoTextEntries("AutoText_Name"). Insert Where:=Selection.Range,
RichText:=False
 
J

Jean-Guy Marcil

Stuart Troy said:
Thanks Jonathon,

I moved the custom template to Word's startup folder;
C:\Documents and Settings\UsernameApplication Data\Microsoft\Word\STARTUP

But when I run this code, it produces an error;

VBA Code;
Documents.Add DocumentType:=wdNewBlankDocument
ActiveDocument.AttachedTemplate.AutoTextEntries("AutoText_Name"). Insert
Where:=Selection.Range, RichText:=False

Resulting Error;
"The requested member of the collection does not exist"

As an alternative to Jonathan's suggestion, you can also do it without even
referencing any template. As long as it is indeed in the Startup folder and
that the AutoText names from that template are unique, you can use:


Dim rngAutoText As Range
Const strAutoTextName As String = "AutoText_Entry_Name"

Set rngAutoText = Selection.Range

With rngAutoText
'In case selection is not an insertion point...
.Collapse wdCollapseStart
.InsertAfter strAutoTextName
.InsertAutoText
End With


If you cannot guarantee unique names, then it is better to use Jonathan's
code. If different templates have Autotext entries with the same name, you
will pull the right one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top