Inserting Autotext entries from a template file in the startup fol

F

frank

I'm trying to create a template file that I can distribute to other users at
my company. What they want is to be able to push a button and a section
break will be inserted at the top of the document and a form will be inserted
above the break. I have created the code to do it. I saved the form
information under an AutoText Entry inside the template file (Form.dot). I
copied the form.dot to the startup folder where the other template files are
located. I can see the autotext entry in Tools/Autocorrect/Autotext when I
launch word. The Autotext entry (the actual form) can be inserted manually,
but when I try to insert it using VBA code it can't seem to find the Autotext
entry. I think I need to make a more detailed reference to it.

With the form.dot file located in the startup folder, I launch a blank word
file and when I start the macro it says:

Runtime error: 5941
The requested member of the collection does not exist

I can see the Autotext entry when I click on Tools/Autocorrect/Autotext, but
apparently the code is not calling it out. Here is the code I am using to
retrieve the autotext entry from the form.dot file located in the startup
folder and insert it into the current cursor position:

Application.DisplayAutoCompleteTips = True
With AutoCorrect
.CorrectInitialCaps = True
.CorrectSentenceCaps = True
.CorrectDays = True
.CorrectCapsLock = True
.ReplaceText = True
.ReplaceTextFromSpellingChecker = True
.CorrectKeyboardSetting = False
End With
ActiveDocument.AttachedTemplate.AutoTextEntries("Form").Insert Where _
:=Selection.Range
 
J

Jay Freedman

Hi Frank,

The problem is a misunderstanding of what
ActiveDocument.AttachedTemplate is. That specifically represents the
template you see in the top box of the Tools > Templates & Add-ins
dialog. Initially it's the template you based the new document on, but
that can be changed. However, it isn't ever a global template in the
Startup folder, which will appear in the bottom box in that dialog.

To get hold of the global template, you have to do something like
this:

Dim temp As Template

For Each temp In Templates
If LCase(temp.Name) = "my global.dot" Then
temp.AutoTextEntries("Form").Insert Where _
:=Selection.Range
Exit For
End If
Next

Theoretically, according to the Help, you should be able to write

' Templates.Item("my global.dot").AutoTextEntries("Form").Insert _
' Where:=Selection.Range

or even

' Templates("my global.dot").AutoTextEntries("Form").Insert _
' Where:=Selection.Range

but these don't work, so you're stuck with iterating through the
Templates collection until you find the one you want.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
F

frank

First, let me thank you for the solution. It does seem like the only way is
to loop through all of the active templates (which is lame, the other coding
you mentioned should work).

Second, The solution you gave me inserts the autotext entry but it is
unformatted. The heading which was Bold and Centered in the autotext entry
is now left aligned with no bold (basically everything is Normal style).

If I manually insert the same autotext entry using
Tools/Autocorrect/Autotext, it will insert the entry with all of the correct
formatting so I know the autotext entry is correct. When I record the macro
for inserting the Autotext entry it is inserting it using the same coding you
gave me for the solution.

Recorded Macro:
ActiveDocument.AttachedTemplate.AutoTextEntries("Form").Insert
Where:=Selection.Range

Your solution:
temp.AutoTextEntries("Form").Insert Where:=Selection.Range

I can't figure that one out. There is an easy solution for me, I can just
automatically move the cursor to the top 3 lines and center/bold the heading
with an additional routine after the the autotext gets inserted, but I would
like to see if there is a way to insert the autotext entry with the
formatting in tact if possible.
 
F

frank

Actually, I figured it out. I used your solution to cycle through the
attached templates and I found on another website that you can use ",
RichText:=True" directly after the insert command to retain the formatting.

temp.AutoTextEntries("Form").Insert Where:=Selection.Range, RichText:=True

Thanks so much for everything!
 
J

Jay Freedman

The .Insert method has a second (optional) parameter, RichText -- this
is described in the VBA help topic about the method. If you omit it,
the parameter takes the default value of False, so the entry is
inserted as unformatted. If you include it and set it to True, the
entry is inserted as formatted. The statement you want, then, is

temp.AutoTextEntries("Form").Insert Where:=Selection.Range, _
RichText:=True

Just for yucks, you should read my diatribe about some of the many
things that are unexpected or just broken in the macro recorder:
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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