I'm working on an access application. At the end of the application, I need to fill a word template with data from an Access table.
My word template have a table which will be filled with a row from the table.
The problem is that I need a table per row. So I need to duplicate the only table of my word template.
How can I copy the table and paste it juste under ?
Here my current code which can fill the first table
Dim appWord As Word.Application
Dim appWord2 As Word.Application
Dim doc As Word.Document
Dim template As Word.Document
Dim db As DAO.Database
Dim rs As DAO.Recordset
On Error Resume Next
Err.Clear
Set appWord = CreateObject("Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set appWord2 = CreateObject("Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord2 = New Word.Application
End If
Set db = CurrentDb()
'The final selected Building Blocks are in the table ConsultingOffer
Set rs = db.OpenRecordset("ConsultingOffer")
'Open the Word template
Set template = appWord.Documents.Open("C:\WordForms\Template.docx", , True)
'Open a new Word document which is a copy of the template
Set doc = appWord2.Documents.Add(template.FullName)
Dim iTable As Integer
iTable = 2
'Loop on all records to populate the word
Do While Not rs.EOF
With doc
.SelectContentControlsByTitle("Title").Item(1).Range.Text = rs.Fields("ModuleName")
.Tables(iTable).Cell(1, 2).Range.Text = rs.Fields("Comments")
.Tables(iTable).Cell(2, 3).Range.Text = rs.Fields("DeliverablesDescription")
.Tables(iTable).Cell(5, 2).Range.Text = rs.Fields("EffortRequired") & " man-days"
rs.MoveNext
End With
Loop
'Save our final document as "Proposal" + date
doc.SaveAs2 ("C:\WordForms\Proposal_" & Format(Now(), "ddmmmyyyy_h\hmm_ss") & ".docx")
'Open Word Application with our final document
appWord2.Visible = True
'Quit the word template
appWord.Quit True
Set template = Nothing
Set doc = Nothing
Set appWord = Nothing
Set appWord2 = Nothing