View Single Post
 
Old 02-04-2012, 12:09 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi ohmzoned,

Assuming all files in a given folder should have the same template, try:
Code:
Sub AttachNewTemplate()
Application.ScreenUpdating = False
Dim strFolder As String, strTmplt As String, strFile As String, wdDoc As Document
With Application.FileDialog(msoFileDialogFilePicker)
  .Filters.Clear
  .AllowMultiSelect = False
  .Filters.Add "All Word Templates", "*.dot; *.dotx; *.dotm", 1
  If .Show = -1 Then
    strTmplt = .SelectedItems(1)
  Else
    Exit Sub
  End If
End With
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
' Create a temporary Word session to do the processing
Dim wdTmp As New Word.Application
wdTmp.Visible = False
'wdTmp.DisplayAlerts = wdAlertsNone
While strFile <> ""
  Set wdDoc = wdTmp.Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  wdDoc.AttachedTemplate = strTmplt
  wdDoc.Close SaveChanges:=True
  strFile = Dir()
Wend
Set wdDoc = Nothing: Set wdTmp = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
When you run the code, simply select the template, then the folder containing the files to process. If you are getting unnecessary prompts as files are opened, try uncommenting the 'wdTmp.DisplayAlerts = wdAlertsNone' line. it may (or may not) kill the prompts. Don't do this if the files are mailmerge main documents, though, or they'll lose their data connections.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote