T
Tony_VBACoder
With Access 2000, I am trying to output the results of a
query (multiple rows) to a Word Document that includes
Bookmarks. My Word document is one page long, with a
bunch of Bookmarks that I want to populate from my query
in Access. I am able to populate my Word document for 1
page, but how do I accomplish this when my query has many
rows and I need to create a new page within Word for each
row of data in my query? My sample function to populate
my Bookmarks is below which will produce one page:
Option Compare Database
Option Explicit
Private objWord As Word.Application
Private objDocument As Word.Document
Public Sub CreateWordLetter(sWordTemplate As String)
' Create and launch Word
Set objWord = New Word.Application
' Make Word visible
objWord.Visible = True
' Point the Document object at a new document based on the
template
Set objDocument = objWord.Documents.Add(sWordTemplate)
' Populate all the bookmarks in the document
' Loop through the query results
Set rst = CurrentDb.OpenRecordset("qryWordData")
Do While Not rst.EOF
UpdateBookmarkProc "LetterDate", rst!LetterDate
<<< update the other fields from the query >>>
rst.MoveNext
Loop
Set objDocument = Nothing
End Sub
Public Sub UpdateBookmarkProc(sBookmarkToUpdate As String,
sTextToUse As String)
Dim bmRange As Range
' Covert a Null value to empty string - if the value is
NULL it will delete the bookmark
sTextToUse = IIf(IsNull(sTextToUse), "", sTextToUse)
Set bmRange = objDocument.Bookmarks
(sBookmarkToUpdate).Range
bmRange.Text = sTextToUse
objDocument.Bookmarks.Add sBookmarkToUpdate, bmRange
End Sub
query (multiple rows) to a Word Document that includes
Bookmarks. My Word document is one page long, with a
bunch of Bookmarks that I want to populate from my query
in Access. I am able to populate my Word document for 1
page, but how do I accomplish this when my query has many
rows and I need to create a new page within Word for each
row of data in my query? My sample function to populate
my Bookmarks is below which will produce one page:
Option Compare Database
Option Explicit
Private objWord As Word.Application
Private objDocument As Word.Document
Public Sub CreateWordLetter(sWordTemplate As String)
' Create and launch Word
Set objWord = New Word.Application
' Make Word visible
objWord.Visible = True
' Point the Document object at a new document based on the
template
Set objDocument = objWord.Documents.Add(sWordTemplate)
' Populate all the bookmarks in the document
' Loop through the query results
Set rst = CurrentDb.OpenRecordset("qryWordData")
Do While Not rst.EOF
UpdateBookmarkProc "LetterDate", rst!LetterDate
<<< update the other fields from the query >>>
rst.MoveNext
Loop
Set objDocument = Nothing
End Sub
Public Sub UpdateBookmarkProc(sBookmarkToUpdate As String,
sTextToUse As String)
Dim bmRange As Range
' Covert a Null value to empty string - if the value is
NULL it will delete the bookmark
sTextToUse = IIf(IsNull(sTextToUse), "", sTextToUse)
Set bmRange = objDocument.Bookmarks
(sBookmarkToUpdate).Range
bmRange.Text = sTextToUse
objDocument.Bookmarks.Add sBookmarkToUpdate, bmRange
End Sub