Option Explicit
Sub DoMerge()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim sh As Worksheet
Dim strEmployee As String
Dim r As Integer
Dim rw As Word.Row
Set sh = ActiveSheet
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
r = 1
Do
If sh.Cells(r, 1).Value <> "" Then
If strEmployee <> "" Then
wdDoc.SaveAs "C:\MyFolder\" & strEmployee & ".doc"
wdDoc.Close wdDoNotSaveChanges
End If
Set wdDoc = wdApp.Documents.Add("C:\MyTemplatePath\SampleLetterEmployee.dot")
strEmployee = sh.Cells(r, 1).Value & " " & sh.Cells(r, 2).Value
wdDoc.Bookmarks("Fullname").Range.Text = strEmployee
wdDoc.Bookmarks("Firstname").Range.Text = sh.Cells(r, 1).Value
End If
Set rw = wdDoc.Tables(1).Rows.Add
rw.Range.Bold = False
rw.Cells(1).Range.Text = sh.Cells(r, 3).Value
rw.Cells(2).Range.Text = sh.Cells(r, 4).Value
r = r + 1
Loop Until sh.Cells(r, 3).Value = ""
wdDoc.SaveAs "C:\MyFolder\" & strEmployee & ".doc"
wdDoc.Close wdDoNotSaveChanges
End Sub
|