I want to create a template in Word, that I can then use Access to merge data into. The Access data has various levels of grouping. Within each grouping, there are subqueries that may also have grouping. Because there is grouping, certain pages will need to be repeated.
Example: I have a query that prints the details of a classroom. There are many students in each classroom, as well. So, I would like to have a DOTM template that groups each student by class. Then prints the first class details, then the students, then moving on the next class, then students, etc.
The Access tables/queries, etc. are not an issue. It's creating a template, then merging it, that I'm having issues with. Right now, I just have a simple template (Dotm file). The file has some boilerplate stuff, and some bookmarks. I then use this code to interact with the dotm file:
Dim objWord As Word.Application
Dim PauseTime, Start, Timer As Integer
Dim wrkCurrent As DAO.Workspace
Set objWord = CreateObject("Word.Application")
objWord.Visible = False 'True is visible
Dim sql As String
sql = "SELECT * FROM tbl_School" 'ex query that produces more that one record
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set wrkCurrent = DBEngine.Workspaces(0)
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(sql)
objWord.Documents.Add ("C:\test\Test.dotm")
'this template has one page, with one bookmark, School_Name. What I want
' to happen is that, for every record, create a new page, with this
'bookmark filled in.
If (Not rst.EOF) Then
With rst
Do Until .EOF
objWord.ActiveDocument.Bookmarks("School_Name").Select
objWord.Selection.Text = rst!school_name
.MoveNext
Loop
End With
End If
objWord.ActiveDocument.SaveAs ("C:\test\MyNewDocument.docx")
objWord.PrintOut
objWord.Quit
Set objWord = Nothing
The problem is that this only prints the first one, then errors. How would I do the grouping? FYI, I know I can do this in reports, but I must allow the report to be exported to Word, retaining images, graphics, etc. which the export loses.