0
votes

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
2
All the copy paste I found were like "Word to another word" or "Word to excel". - Weedoze
It would help if you showed the portion of your code where you are accessing the Word document. - TheEngineer

2 Answers

0
votes

Actually, I don't think it's possible to copy/paste the table. Someone said that I'll be maybe easier to create a new table in HTML and add it under the first one.

Source = "<table style=""display:inline;border-collapse:collapse;font-size:1em;width:100%"" border=""1""><tbody>"

        First line of the table (ID and Title)
        Source = Source & "<tr><td style=""vertical-align:top"" class=""ms-rtetablecells""><div>(Story ID) - Title:</div></td>"
        Source = Source & "<td style=""vertical-align:top"" class=""ms-rtetablecells"" colspan=2><div>" & "testing cell" & "</div></td></tr>"
        Source = Source & "</tbody></table>"

So now I have my table (still need to fill it but that's ok).

But I still have a problem. How can I paste it just under the template table ?

0
votes

The only solution I found was to create a new table on html then write this table in a TextFile and finally copy this file in my word template.