SetMyTemplate problem

K

kurt

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
 
S

Shauna Kelly

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
 
K

kurt

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
 
S

Shauna Kelly

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
 

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