Hi All,
I was able to figure out the code and below is the final outcome for your reference:
Sub CreateSheets()
Dim wdApp As Object
Dim ProjectPath As String
Dim odocument As Word.Document
Dim i As Long, j As Long
Dim lFirstRow As Long, lFirstCol As Long, lLastRow As Long, lLastCol As Long
With ActiveSheet.UsedRange
lFirstRow = .Row
lFirstCol = .Column
lLastRow = .Rows(UBound(.Value)).Row
lLastCol = .Columns(UBound(.Value, 2)).Column
End With
ProjectPath = ActiveWorkbook.Path & "\"
'Open Word Instance
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = False
Dim strTemplateName As String
strTemplateName = ProjectPath & "Sample Data.dot"
For j = 4 To lLastRow
On Error Resume Next
Kill ProjectPath & "Str " & Cells(j, 3) & ".doc"
Set odocument = wdApp.Documents.Add(strTemplateName, False, , True)
With wdApp.ActiveDocument
'Prefill Data
For i = 1 To lLastCol
.FormFields("FormField" & i).Result = Cells(j, i)
Next
End With
odocument.SaveAs ProjectPath & "Str " & Cells(j, 3), wdFormatDocument97
odocument.Close
Next
'Clear
Set wdApp = Nothing
MsgBox "Created " & lLastRow - 3 & " Foundation Data Sheets"
End Sub
Bookmarks