Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Referencing a template (VBA-Word 2003)

    I doubt if this can be done, but it's worth a shot. Is it possible to create a reference (as template) to a .DOT template which is not contained within the Templates collection? i.e. just a template file "somewhere" on disk?

    Alan

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Referencing a template (VBA-Word 2003)

    Do you mean a reference as in Tools | References?

    You can click Browse in the References dialog to set a reference to a template (.dot file) anywhere, provided it has a unique project name (you can't have two references with the same project name). The default project name of a new document is "Project" and that of a new template is "TemplateProject", so if that name hasn't been changed, this could be a problem.

    Once you set the reference, the template becomes part of the Templates collection, as you can verify in Word itself in Tools | Templates and Add-Ins...

    Or did you mean something quite different?

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a template (VBA-Word 2003)

    You've probably confirmed my suspicions, but opened up a possibility too.
    I'd like to be able to write something to the effect of:
    <code>
    Dim tpl as Template
    Set tpl = <some filename>.dot
    </code>
    But I think it only possible to use Set tpl = Templates(index)

    But If I could programatically add a (project) reference as you describe, then I presume that reference would become available within VBA code via its addition to the Templates collection?

    Alan

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Referencing a template (VBA-Word 2003)

    You can use the AddIns collection to add a template that is not attached to a document.:

    Dim tpl As AddIn
    Set tpl = Application.AddIns.Add("C:MyFilesSomeTemplate.dot" )

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a template (VBA-Word 2003)

    Is it possible to access the addin template's properties, like variables and autotext collections? I can't readily see how.

    Alan

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Referencing a template (VBA-Word 2003)

    After adding the AddIn, it is has also become a member of the Templates collection, so you can do something like this:

    Dim tpl As AddIn
    Dim tpl2 As Template
    Set tpl = AddIns.Add("C:MyFilesSomeTemplate.dot")
    For Each tpl2 In Templates
    If tpl2.Name = "SomeTemplate.dot" Then
    Exit For
    End If
    Next tpl2

    You can now use tpl2 to access the AutoText entries etc.

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a template (VBA-Word 2003)

    Thanks Hans. That looks rather clever. If I understand it correctly, it assigns a (template) reference to tpl2 by making it leave the For loop once it's taken on the name of the required member... already now present in the collection after adding it as an addin. Is that correct?

    Alan

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Referencing a template (VBA-Word 2003)

    Yes, that's it.

    (For some reason unknown to me it is not possible to refer to a member of the Templates collection by name:

    Set tpl2 = Templates("SomeTemplate.dot")

    will generate an error message. So you have to loop through the Templates collection until you've found the one you're searching for.)

  9. #9
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a template (VBA-Word 2003)

    Very cunning. <img src=/S/sneaky.gif border=0 alt=sneaky width=15 height=15> I wonder if any of the other built-in collections require the same sort of coercing to give up their "secrets"?

    Alan

  10. #10
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Referencing a template (VBA-Word 2003)

    FWIW, you can directly refer to a member of the Templates collection by name, but you have to supply the full path (not just the filename) -- e.g.:

    Set tpl2 = Templates("C:ProgDWordStartupSomeTemplate.dot")

  11. #11
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a template (VBA-Word 2003)

    Thanks Steve. That seems to work very nicely:
    <code>
    Dim tpl1 As AddIn
    Dim tpl2 As Template

    Set tpl1 = Application.AddIns.Add(Me.ListBox1.Value)
    Set tpl2 = Templates(tpl1.path & "" & tpl1.Name)
    </code>

    The only thing I'm having a problem with now is how to prevent anything belonging to the added template from running automatically when the template is added e.g. AutoOpen, running SQL updates etc. Again, I can't see any options to avoid this, but maybe there's a way?

    Alan

  12. #12
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Referencing a template (VBA-Word 2003)

    Your follow-up question on AutoOpen, etc. is over my head, but maybe the Reference approach (rather than the AddIn approach) would avoid that.

    Here's a MSFT article that discusses those 2 approaches (among others).

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Referencing a template (VBA-Word 2003)

    The old WordBasic instruction DisableAutoMacros can be used to prevent automatic macros, including AutoOpen, from running.

    WordBasic.DisableAutoMacros 1
    Set ...
    WordBasic.DisableAutoMacros 0

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a template (VBA-Word 2003)

    >I wonder if any of the other built-in collections require the same sort of coercing to give up their "secrets"?
    Funny you should ask. Here's a bit of WIP I wrote yesterday, on List templates (List Gallery, Numbered Lists, etc.)
    Let’s refer to a specific List Gallery (by the Word constant “wdOutlineNumberGallery”) and let’s refer to a specific List Template (3) within that List Gallery
    <pre>Sub TESTListTemplates2()
    Dim lstG As ListGallery
    Set lstG = Application.ListGalleries(wdOutlineNumberGallery)
    Debug.Print "Name: " & vbTab & "*" & lstG.ListTemplates(3).Name & "*"
    End Sub</pre>

    Here is what I see:
    <pre>Name: *Greaves2*</pre>

    So far so good. Based on my previous example, I expected to see “Greaves2” as the name of the 3rd List Template in the List Gallery known as wdOutlineNumberGallery.
    Since the List Template has a name, you’d expect to be able to refer to it by its name:
    <pre>Sub TESTListTemplates3()
    Dim lstG As ListGallery
    Set lstG = Application.ListGalleries(wdOutlineNumberGallery)
    Debug.Print "Name: " & vbTab & "*" & lstG.ListTemplates("Greaves2").Name & "*"
    End Sub</pre>

    Nope! This macro generates a run-time error 5941 – The requested member of the collection does not exist.
    Can we locate the List Template within the List Gallery?
    <pre>Public Function lngListTemplateIndex(lstG As ListGallery, strName As String) As Long
    lngListTemplateIndex = -1 ' default result is failure
    Dim lng As Long
    For lng = 1 To lstG.ListTemplates.Count
    If lstG.ListTemplates(lng).Name = strName Then
    lngListTemplateIndex = lng
    Exit For
    Else
    End If
    Next lng
    End Function
    Sub TESTlngListTemplateIndex()
    Dim lstG As ListGallery
    Set lstG = Application.ListGalleries(wdOutlineNumberGallery)
    Debug.Print lngListTemplateIndex(lstG, "Greaves2")
    End Sub</pre>

    Yes!
    <pre> 3 </pre>

    How Odd! Yet it’s not the first time I’ve met a collection that permits indexing by number but not by name.

  15. #15
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a template (VBA-Word 2003)

    That seems to work Hans. <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>
    Getting greedier, is there any way to disable auto messages offering to update links etc?

    Alan
    Attached Images Attached Images

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •