Search code examples
templatesms-accessvbams-wordbookmarks

Add pages to a Word Document from Access VBA?


I have a .dot Word template, and I loop through the bookmarks to fill the Document.

I'm filling the template for each RecordSet's Row, but I'm creating a new/separated Word Document.

Can I Add Pages to the document or merge individual documents to open a single Word document with all pages that I need?

This is an schema of my code

Set rst = qdf.OpenRecordset()

If Not rst.EOF Then
    rst.MoveFirst
    Do While Not rst.EOF()

        Set doc = appWord.Documents.Open(DOC_PATH & DOC_NAME, , True)
        For Each bm In doc.Bookmarks
        ' Fill Bookmarks
        Next bm
        appWord.Documents.Open filename:=DOC_PATH & DOC_NAME
    Loop 
End If
rst.Close

Solution

  • What about using InsertFile to append all the documents to the first one you open? You could also insert page breaks if you want (not shown here)

    Dim W As New Word.Application
    W.Visible = True
    Dim D As Document: Set D = W.Documents.Add
    Dim R As Word.Range: Set R = D.Range
    R.InsertFile "c:\somefile.doc"