Basia:
I hope the following function helps you out. You will need a reference to the Microsoft Word 8.0 or 9.0 Library, depending on your version of Word. Also, your Word document will have to have bookmarks at the insertion points - more on bookmarks to follow.
Function CreateWordLetter(strDocPath As String)
Dim dbs As Database, rst As Recordset, objWord As Object
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset (<enter table name here>)
If rst.Recordcount = 0 Then
'no records, no need to continue
Exit Function
End if
'create reference to Word Object
Set objWord = CreateObject("Word.Application"
'Word Object is created - now let's fill it with data.
While Not rst.EOF
With objWord
.Visible = True
.Documents.Open(strDocPath)
'move to each bookmark, and insert correct text.
.ActiveDocument.Bookmarks("<bookmark name>".Select
.Selection.Text=(Cstr(Forms!<form name>!<field
name>))
.ActiveDocument.Bookmarks.Add Name:=<bookmark name>,
Range = Selection.Range
**continue the ActiveDocument and Selection statements
for each bookmark that you have on the Word Document **
End With
'Print the document in the foreground, and then close
the document without saving changes.
objWord.ActiveDocument.PrintOut Background:=False
rst.MoveNext
Wend
'release all objects
set objWord = nothing
set dbs = nothing
set rst = nothing
End Function
Ok, now the function is written - however, this is only half the battle. Now we have to create/edit the document you are using so the above code actually works!
1) Open your document. If you do not have a current
document that you are not using, create a new one.
2) In the places where the values will change based on
information in the record, create a bookmark and name
it appropriately. i.e. If you have a field in Access
called FirstName, then make the bookmark name "First
Name", etc etc
3) After creating / editing your document, save it.
Remember the file name.
4) Back to Access! Create a button on a form somewhere,
and name it appropriately (ie cmdMerge, cmdMakeLetter,
etc)
5) In the On_Click event of your button, use an event
procedure and add the following line of code:
CreateWordLetter "<path to your document>"
6) Watch everything kick into action!
HTH
Greg Tammi, ATS Alarm Supervisor