Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Attaching a new template (VBA/Word/2000 & 2003)

    I have a Word template that has been saved with a password. While recording a macro I attached this template to a document--being required to provide the appropriate password--then looked at the recorded code. The code did not show any indication of a password being required, and I can find no method of providing the password in code. Is it possible to have VBA supply the password while attaching a template to to a document?

    T.I.A.
    Regards
    Don

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

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

    Re: Attaching a new template (VBA/Word/2000 & 2003)

    The only way I can think of is to open the template first, then attach it to the document. Hopefully someone else will come up with a more elegant method.

    Dim doc1 As Document
    Dim doc2 As Document
    Set doc1 = ActiveDocument
    Set doc2 = Documents.Open(FileName:=Application.Options.Defau ltFilePath(wdUserTemplatesPath) & _
    "" & "MyTemplate.dot", PasswordTemplate:="secret")
    doc1.AttachedTemplate = "MyTemplate.dot"
    doc2.Close SaveChanges:=wdDoNotSaveChanges

  4. #3
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attaching a new template (VBA/Word/2000 & 2003)

    Thanks Hans
    New Text <font color=red>As tried on Word 2003 </font color=red> End of New Text
    I have incorporated your recommendation into the recorded code, as shown below. But the resulting code fails at the .AttachedTemplate = "Don.dot" command with the alert "Word cannot open this document template."

    Any thoughts on where I have gone wrong? <pre>Sub Macro6()
    '
    ' Macro6 Macro
    ' Macro recorded 2/13/2006 by
    '
    Dim oDoc1 As Document
    Dim oDoc2 As Document

    Set oDoc1 = ActiveDocument
    Set oDoc2 = Documents.Open(FileName:= _
    "Cocuments and SettingsloanerDesktopDon.dot", _
    PasswordTemplate:="Don")

    With oDoc1
    .UpdateStylesOnOpen = False
    .AttachedTemplate = "Don.dot"
    End With

    oDoc2.Close wdDoNotSaveChanges

    Set oDoc1 = Nothing
    Set oDoc2 = Nothing

    End Sub</pre>

    Regards
    Don

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

    Re: Attaching a new template (VBA/Word/2000 & 2003)

    Since the template is not in the default templates path, you should specify the full path, as in the Documents.Open line.

  6. #5
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attaching a new template (VBA/Word/2000 & 2003)

    Hans
    Thank you, not only for the solution; but also for the patience and explanation. <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>
    Regards
    Don

Posting Permissions

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