Copy Autotext Using VBA from One Template to Another

M

mrb712

I would like to copy the autotext entries from one template to another
(and many others) using a VBA script. I found one in this group, but it
appears to need some tweaking that I'm not familiar with, and I'm
hoping to pick some of your brains for help. Below is the code as I
have it now:

Sub CopyAutoText()
Dim aEntry As AutoTextEntry
Dim OldTemplate As String
Dim TempDoc As Document

OldTemplate = "Z:\Templates\new.dot"
Set TempDoc = Documents.Add(Template:=OldTemplate, Visible:=False)

For Each aEntry In TempDoc.AttachedTemplate.AutoTextEntries
Application.OrganizerCopy
Source:=TempDoc.AttachedTemplate.FullName, _
Destination:=NormalTemplate.FullName, _
Name:=aEntry.Name, _
Object:=wdOrganizerObjectAutoText
Next aEntry

TempDoc.Close SaveChanges:=wdDoNotSaveChanges
Set TempDoc = Nothing
End Sub

The template "new.dot" is a blank template in which I store styles and
autotext to be copied into multiple content templates. I would like to
know if there is a way to make the code copy the autotext from
"new.dot" to the currently opened document. That's all. I'll save the
..dot and close it myself.

I'm using the copystylesfromtemplate code from MS Word's help file, and
I would like to use the script above to copy the AutoText entries as
well. I have literally hundreds of templates to maintain, and I would
like to automate this process as much as possible. I don't have a batch
script to perform this function, so I have to do this to each template
individually.

Thank you for any help,
mrb
 
B

Birgit

Hi mrb!
Just a question about your approach of maintaining the autotexts: Do you
want the same autotexts in every template? This seems insane to me (unless
you have a good reason...). If you want the same autotexts to be available
on every computer, you could also put the autotexts in a global template on
each computer and distribute at log-on.

Cheers,
Birgit
 
M

mrb712

Hi Birgit,

I have four autotexts that I use in content templates. These are
headers and footers that are applied through macros -- one set of
portrait headers/footers, one set of landscape headers/footers. In the
past, I have manually copied the four autotexts from one central
template to the dozens of content templates that we use regularly. And
I agree with you: it is insane. So that's why I'm looking for a better
way to manage these autotexts.

The four autotexts are part of the global template, which is
distributed to the authors. Is that all they need? For some reason, I
always thought that the autotexts had to be part of the content
template that the author uses. If I'm wrong, please tell me and save me
the headache of this tedium...and I will owe you big time.

Otherwise, a VBA solution seems more practical.

Thanks,
Michael
 
B

Birgit

Hi Michael,
let me get this straight: you can use any autotext from a currently loaded
template. this can be normal.dot, any global template or the attached
template of your current document.

Inserting by macro from an attached template is easy as you can use

ActiveDocument.AttachedTemplate

For normal.dot there is something similar which I don't recall because I
never use it (and would not recommend).

For a global template you have to do a little more work:
Cycle through the collection of templates and find the one with the name you
need.

Function FindTemplate(bSuccess As Boolean) As Template
Dim myTemplate As Template

For Each myTemplate In Application.Templates
If LCase(myTemplate.Name) = "myGlobalTemplateName.dot" Then
Set FindTemplate = myTemplate
bSuccess = True
Exit Function
End If
Next
bSuccess = False

End Function

Once found you can put it into a template object

Set myTemplate = FindTemplate(bSuccess)
If bSuccess = False Then Exit Sub

and do something like

FoundTemplate.AutoTextEntries("myAutotext").Insert WHERE:=myRange,
RichText:=True

Hope this helps,
Birgit
 

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