Thread: [Solved] Batch create Word documents
View Single Post
 
Old 11-06-2012, 05:38 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,381
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

Try the following Excel macro. You'll need to set the source document's filename correctly in the first 'Set wdDoc' line.

As coded, the macro looks for a docx document and saves the output files in that format. If your source document is a 'doc', not 'docx', change that part of the first 'Set wdDoc' line also. And, if you want to save in the 'doc' format, change 'wdFormatXMLDocument' to 'wdFormatDocument'.
Code:
Sub Demo()
'Note: A Reference to the Word Object Model is required.
Application.ScreenUpdating = False
Dim i As Long
Dim wdApp As Word.Application
Set wdApp = Word.Application
wdApp.Visible = False
Dim wdDoc As Word.Document
'Open the source document
Set wdDoc = wdApp.Documents.Open(Filename:="C:\Users\" & Environ("UserName") & "\Documents\MyFile.docx", _
  AddToRecentFiles:=False, Visible:=False)
With ActiveSheet
  For i = 2 To .Cells.SpecialCells(xlCellTypeLastCell).Row
    Application.StatusBar = "Creating document " & i
    'Save a copy of the document with the new filename
    wdDoc.SaveAs2 Filename:=.Cells(i, 1).Value, FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
 Next
End With
'Close the document
wdDoc.Close SaveChanges:=False
'Close Word
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing
Application.StatusBar = "Done!!"
Application.ScreenUpdating = True
End Sub
The *complete* filenames ( i.e 'How to boil an egg', not just 'boil an egg') are assumed to be in column A
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote