Word 2007 Templates collection

T

TFTAJLLYMXZP

Hello,

Probably I am missing something basic, but I have a template loaded
globally, am unable to get VBA to recognize that it is part of the
Templates collection. Anyone else running across this problem? It
worked fine with Word 2003.

e.g.

DIm oTmp As Template
Set oTmp = Templates("T:\Autotext\SharedAutotext.dotm")

where 'SharedAutotext.dotm' appears in the loaded Add-In list.

Thanks for any insight you can provide.
 
T

TFTAJLLYMXZP

There has been no response to my original post, so is no-one else
experiencing similar problems referencing loaded global templates by
name using the Templates collection object?

I have verified that the template is, in fact, loaded, and have tried
using both its FullName property and its Name property as displayed in
the Watch pane of the VBA Editor. The folder it resides in is also a
Trusted Location, so that isn't the problem, either.

Really, any clues at all would be appreciated!

Thanks.

Terry
 
G

Gordon Bentley-Mix

Terry,

Please keep in mind that these newsgroups are run by volunteers and NOT paid
employees of MSFT. As such, we answer questions when we have time to do so.
At the moment, several members of the MVP ranks are at the MVP Global Summit
in Redmond and thus not monitoring the newsgroups very much - if at all.

Your issue is one that I may have encountered before, but unfortunately I
just don't have the time to look into it right now - paid work and family
commitments are always first in the queue. If I get a chance later this
evening, I'll look through my past projects and see what I can find.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
G

Gordon Bentley-Mix

Terry,

You're very lucky today. I just happened to open something that contained
something that should work. Try this:

Sub FindMyTemplate()
Dim myTemplate As Template
Dim i As Integer
For i = 1 To Templates.Count
If Templates(i).Name = "SharedAutotext.dotm" Then
Set myTemplate = Templates(i)
Exit For
End If
Next i
'Do whatever it is that I need to do with myTemplate
'and make sure there is error handling in case the
'template's not found in the collection
End Sub

OR

Sub FindMyTemplate2()
Dim myTemplate As Template
Dim LookAtThisTemplate As Template
For Each LookAtThisTemplate In Application.Templates
If LookAtThisTemplate.Name = "SharedAutotext.dotm" Then
Set myTemplate = LookAtThisTemplate
Exit For
End If
Next LookAtThisTemplate
'Do whatever it is that I need to do with myTemplate
'and make sure there is error handling in case the
'template's not found in the collection
End Sub

I don't know why your original construction won't work. It seems to me that
it should but I (dimly) recall fighting this beast before, and the only way
to get it to work was to look through the Templates collection one template
at a time. I've run into this a few other places as well.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
G

Greg Maxey

Terry/Gordon,

I have a globally loaded template AddIn named workdocuments. If I try to
use the name only as shown in Test1 I do get an error. If I use the
fullname (test2) then it works fine. I can also use a for each statement
(test 3):

Sub Test1()
Dim oTmp As Template
On Error Resume Next
Set oTmp = Templates("workdocuments.dotm")
If Err.Number <> 0 Then MsgBox Err.Number & " " & Err.Description
End Sub

Sub Test2()
Dim oTmp As Template
On Error Resume Next
Set oTmp = Templates("F:\My Documents\Word\Startup\workdocuments.dotm")
If Err.Number <> 0 Then
MsgBox Err.Number & " " & Err.Description
Else
MsgBox oTmp.Name & " -- " & oTmp.FullName
End If
End Sub

Sub Test3()
Dim oTmp As Template
Dim bSet As Boolean
bSet = False
For Each oTmp In Templates
If oTmp.Name = "workdocuments.dotm" Then
bSet = True
Exit For
End If
Next
If bSet Then
With oTmp
MsgBox .BuildingBlockEntries.Count
End With
End If
End Sub
 
T

TFTAJLLYMXZP

Gordon,

I didn't intend to come across as a complainer, simply trying to
verify if I was the only one experiencing the problem (due to some
bonehead oversight on my part). My apologies to you and anyone else
who might have been put off a bit.

Terry
 
T

TFTAJLLYMXZP

Lookas like a promising bit of code, Gordon. I'll give it a spin.
Thanks for your time.
 
T

TFTAJLLYMXZP

Thanks, Greg.


Terry/Gordon,

I have a globally loaded template AddIn named workdocuments.  If I try to
use the name only as shown in Test1 I do get an error.  If I use the
fullname (test2) then it works fine.  I can also use a for each statement
(test 3):

Sub Test1()
Dim oTmp As Template
On Error Resume Next
Set oTmp = Templates("workdocuments.dotm")
If Err.Number <> 0 Then MsgBox Err.Number & " " & Err.Description
End Sub

Sub Test2()
Dim oTmp As Template
On Error Resume Next
Set oTmp = Templates("F:\My Documents\Word\Startup\workdocuments.dotm")
If Err.Number <> 0 Then
  MsgBox Err.Number & " " & Err.Description
Else
  MsgBox oTmp.Name & " -- " & oTmp.FullName
End If
End Sub

Sub Test3()
Dim oTmp As Template
Dim bSet As Boolean
bSet = False
For Each oTmp In Templates
  If oTmp.Name = "workdocuments.dotm" Then
    bSet = True
    Exit For
  End If
Next
If bSet Then
  With oTmp
    MsgBox .BuildingBlockEntries.Count
  End With
End If
End Sub









--
Greg Maxey -  Word MVP

My web sitehttp://gregmaxey.mvps.org
Word MVP web sitehttp://word.mvps.org- Hide quoted text -

- Show quoted text -
 

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