Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    Oct 2002
    Location
    England
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Attached Document (Word 97)

    Does anyone know of a quick way I could change the attached template on a bunch of Word documents?

    I've found the VBA property 'ActiveDocument.AttachedTemplate = "c:somefile.dot"', but I don't know how I could apply this to a number of documents.

    Any help gratefully received [img]/forums/images/smilies/smile.gif[/img]

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

    Re: Attached Document (Word 97)

    You'll have to be more specific about "a bunch of documents". You can create a loop to set the template for all documents in a folder, or for all documents in a set of folders, or for documents with a pattern in the file name, etc.

    If it's just a number of individual documents, you'll have to open each of them individually, set the template, then close and save them.

  3. #3
    New Lounger
    Join Date
    Oct 2002
    Location
    England
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attached Document (Word 97)

    Sorry I was initally vague because it'd be no biggie to move the documents around to make the process easier. I'm really talking about ALL word documents in a directory, and all it's sub directories. But there's not too many directories if I need to run it multiple times (once for each directory).

    Thanks

    Chris

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

    Re: Attached Document (Word 97)

    The following macro should do what you want. I recommend making a backup copy of the folder(s) involved before trying it, just in case something goes wrong.

    Replace the two constants at the beginning of the macro by the appropriate values.

    Sub SetTemplate()
    ' The path of the folder that contains the documents
    Const strPath As String = "C:MyWordDocs"

    ' The complete path and file name of the template
    Const strTemplate As String = "C:MyTemplatesAnyTemplate.dot"

    ' Variables used in the loop
    Dim i As Long
    Dim oDoc As Document

    On Error GoTo ErrHandler

    With Application.FileSearch
    ' Set up new search criteria
    .NewSearch
    ' Look for word documents
    .FileType = msoFileTypeWordDocuments
    ' Search in strPath
    .LookIn = strPath
    ' Also search subfolders
    .SearchSubFolders = True
    ' Run the search
    .Execute
    ' Loop through all found files
    For i = 1 To .FoundFiles.Count
    ' Open document
    Set oDoc = Documents.Open(FileName:=.FoundFiles(i))
    ' Set template to strTemplate
    oDoc.AttachedTemplate = strTemplate
    ' Close and save
    oDoc.Close SaveChanges:=True
    Next i
    End With

    ExitHandler:
    ' Release object memory
    Set oDoc = Nothing
    Exit Sub

    ErrHandler:
    ' Display error message
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  5. #5
    New Lounger
    Join Date
    Oct 2002
    Location
    England
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attached Document (Word 97)

    Thanks, I see what your doing there, and only the other day I was playing with FileSearch in an Access db, so I dunno why it didn't come to me! It's just been one of those weeks.

    Thanks for your help

    Chris

  6. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Attached Document (Word 97)

    Note that Hans' code will attach the template but not import the styles from that template. This was not requested in your original request but may have been expected. If you need that to happen also then there is one line you will need to add after the template is attached and before the file is closed. The extra line is...
    ActiveDocument.UpdateStyles
    Andrew Lockton, Chrysalis Design, Melbourne Australia

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

    Re: Attached Document (Word 97)

    Thanks, that is a useful addition.

Posting Permissions

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