MrExcel has a great macro for creating Word Docs from Excel data but he names the files sequentially. I'd like to name them with data from the Excel file for each row i.e. Range("A" & i) - how would I do that?
The macro (quite old now) is as follows:
Sub ControlWord()
' You must pick Microsoft Word 8.0 from Tools>References
' in the VB editor to execute Word commands.
' See VB Help topic "Controlling One Microsoft Office Application from Another"
' for more information.
' Originally published by www.MrExcel.com 2/28/1999
Dim appWD As Word.Application
' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application.8")
appWD.Visible = True
Sheets("Data").Select
'Find the last row with data in the database
FinalRow = Range("A9999").End(xlUp).Row
For i = 2 To FinalRow
Sheets("Data").Select
' Copy the name to cell C4
Range("A" & i).Copy Destination:=Sheets("Template").Range("C4")
' Copy data columns, transpose and paste in C10:C13
Range("B" & i & ":E" & i).Copy
Sheets("Template").Select
Range("C10").PasteSpecial Transpose:=True
' Copy the data for the new document to the clipboard
Range("A1:F15").Copy
' Tell Word to create a new document
appWD.Documents.Add
' Tell Word to paste the contents of the clipboard into the new document
appWD.Selection.Paste
' Save the new document with a sequential file name
appWD.ActiveDocument.SaveAs FileName:="File" & i
' Close this new word document
appWD.ActiveDocument.Close
Next i
' Close the Word application
appWD.Quit
End Sub
Many thanks
bch
The macro (quite old now) is as follows:
Sub ControlWord()
' You must pick Microsoft Word 8.0 from Tools>References
' in the VB editor to execute Word commands.
' See VB Help topic "Controlling One Microsoft Office Application from Another"
' for more information.
' Originally published by www.MrExcel.com 2/28/1999
Dim appWD As Word.Application
' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application.8")
appWD.Visible = True
Sheets("Data").Select
'Find the last row with data in the database
FinalRow = Range("A9999").End(xlUp).Row
For i = 2 To FinalRow
Sheets("Data").Select
' Copy the name to cell C4
Range("A" & i).Copy Destination:=Sheets("Template").Range("C4")
' Copy data columns, transpose and paste in C10:C13
Range("B" & i & ":E" & i).Copy
Sheets("Template").Select
Range("C10").PasteSpecial Transpose:=True
' Copy the data for the new document to the clipboard
Range("A1:F15").Copy
' Tell Word to create a new document
appWD.Documents.Add
' Tell Word to paste the contents of the clipboard into the new document
appWD.Selection.Paste
' Save the new document with a sequential file name
appWD.ActiveDocument.SaveAs FileName:="File" & i
' Close this new word document
appWD.ActiveDocument.Close
Next i
' Close the Word application
appWD.Quit
End Sub
Many thanks
bch