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 / January 2008

Tip: Looking for answers? Try searching our database.

SetMyTemplate problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kurt - 18 Jan 2008 15:40 GMT
I have a some autotexts stored in a template which is currently saved in
c:\word startup\firm.dot.  Some of the currently language is:
If frmreplacepi.txtattyinitials = "agq" Then
  Set MyTemplate = Templates("c:\word startup\firm.dot")
MyTemplate.AutoTextEntries("/agqpi").Insert _
   Where:=Selection.Range, RichText:=True

I need to change the location of the firm.dot to C:\Documents and
Settings\USERNAME\Application Data\Microsoft\Word\startup, and then adjust
the language to read:
If frmreplacepi.txtattyinitials = "agq" Then
  Set MyTemplate = Templates("C:\Documents and
Settings\USERNAME\Application Data\Microsoft\Word\startup\firm.dot")
MyTemplate.AutoTextEntries("/agqpi").Insert _
   Where:=Selection.Range, RichText:=True

What do I use instead of "username" in this path so the macro works on every
pc, WITHOUT having to add the name of each person to this path?  I've tried
%username%, etc, but no luck so far.

THANKS SO MUCH!

Kurt
Shauna Kelly - 19 Jan 2008 02:48 GMT
Hi Kurt

Let's back up a few steps here.

I assume that you want to deploy firm.dot as an add-in to all your
users. And you want its AutoTexts to be available all the time to all
users. If that's the case, then put firm.dot into each user's Word
Startup folder. The Word Startup folder is whatever is displayed at
Tools > Templates and Add-ins > Startup.

When Word starts, it will load any .dot file it finds in the Word
Startup folder as an add-in.

In most organizations, you'll want to standardize the location of that
folder. By default, it is at C:\Documents and
Settings\<username>\Application Data\Microsoft\Word\STARTUP. The only
good reasons I've ever found to change from the default is if you want
to use roaming profiles and/or provide external access to the corporate
system (eg using Citrix Presentation Server or Terminal Server).

But once Word starts, it doesn't matter where the file is located. What
matters is whether the file concerned is loaded.

If your file is loaded, then you can just use
   Templates("firm.dot").AutoTextEntries("MyAutoText").Insert ... etc

This works because the Templates collection consists of (a) the
templates to which all currently-open documents are attached and (b) any
.dot files loaded as an add-in (ie as a so-called global template).

Of course you'll need some error checking around that because (a) the
file might not exist or (b) the file might not be loaded or (c) the file
might not contain an AutoText named "MyAutoText".

Hope this helps. If you need more details, let us know.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

>I have a some autotexts stored in a template which is currently saved
>in
[quoted text clipped - 23 lines]
>
> Kurt
kurt - 21 Jan 2008 18:22 GMT
Hi Shauna,

Thanks for the reply.  I tried what you suggested and I still get the
message, "The requested member of the collection does not exist."  I have
loaded the firm.dot in the correct default word startup directory, and Word
is pointing to this directory in the locations under tools/options.

My syntax now reads what you suggested:
If frmreplacepi.txtattyinitials = "agq" Then
  Templates("firm.dot").AutoTextEntries("/agqpi").Insert _
   Where:=Selection.Range, RichText:=True

I also tried this:
 If frmreplacepi.txtattyinitials = "agq" Then
  Set MyTemplate = Templates("firm.dot")
MyTemplate.AutoTextEntries("/agqpi").Insert _
   Where:=Selection.Range, RichText:=True

That doesn't work either.

Any help?  THANKS SO VERY MUCH!!

Kurt

> Hi Kurt
>
[quoted text clipped - 62 lines]
> >
> > Kurt
Shauna Kelly - 22 Jan 2008 03:43 GMT
Hi Kurt

What you have is a debugging problem. That is, you have to work out
where, exactly, the problem is occurring. Or in other words, you need to
be able to interpret the error message and find out which member of what
collection does not exist. You can largely achieve this by (1) clicking
on the first line of your code and pressing F9, which will put a break
point there, then (2) running the code and (3) when you get to that
break point, press F8 to go through the code line by line. When the
error message appears, you'll know which line you were on, and can thus
make some reasonable guesses as to the problem.

You might also consider writing code that is fireproof. Something like
this:

Sub Test1()

Dim atInitials As Word.AutoTextEntry
Dim sInitials As String
Dim tplFirm As Word.Template

   If Not frmreplacepi Is Nothing Then

       'Get the initials from the form
       On Error Resume Next
       sInitials = frmreplacepi.txtattyinitials
       On Error GoTo 0

       If sInitials = "agq" Then

           'Get a reference to the template
           On Error Resume Next
           Set tplFirm = Templates("firm.dot")
           On Error GoTo 0

           If Not tplFirm Is Nothing Then

               'Get a reference to the AutoText
               On Error Resume Next
               Set atInitials = tplFirm.AutoTextEntries("/agqpi")
               On Error GoTo 0

               If Not atInitials Is Nothing Then
                   'Insert the AutoText
                   atInitials.Insert Where:=Selection.Range,
RichText:=True
               Else
                   MsgBox "There is no AutoText named '/agqpi' in
firm.dot"
               End If
           Else
               MsgBox "Word can't find a template named firm.dot"
           End If

       Else
           MsgBox "Initials do not equal agq"
       End If

   Else
       MsgBox "frmreplacepi does not exist, so there is not much we can
do today"
   End If

End Sub

I'm not suggesting that good code should end up with very deeply-nested
If / Else / Endif statements. But good code does need error checking.

Hope this helps.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

> Hi Shauna,
>
[quoted text clipped - 99 lines]
>> >
>> > Kurt
 
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.