Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Inserting Autotext entries from a template file in the startup fol

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
frank - 14 Feb 2007 22:40 GMT
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
Jay Freedman - 14 Feb 2007 23:10 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>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
[quoted text clipped - 30 lines]
>    ActiveDocument.AttachedTemplate.AutoTextEntries("Form").Insert Where _
>        :=Selection.Range
frank - 15 Feb 2007 01:09 GMT
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.

> Hi Frank,
>
[quoted text clipped - 72 lines]
> >    ActiveDocument.AttachedTemplate.AutoTextEntries("Form").Insert Where _
> >        :=Selection.Range
frank - 15 Feb 2007 03:36 GMT
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!

> 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
[quoted text clipped - 99 lines]
> > >    ActiveDocument.AttachedTemplate.AutoTextEntries("Form").Insert Where _
> > >        :=Selection.Range
Jay Freedman - 15 Feb 2007 03:41 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>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
[quoted text clipped - 99 lines]
>> >    ActiveDocument.AttachedTemplate.AutoTextEntries("Form").Insert Where _
>> >        :=Selection.Range
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.