Hope this helps you all ... I had originally made this post in Thread702-242141 ... made a couple of revisions over the months! :)
Public Function CreateWordLetter(strDocPath As String)
'function returns nothing, but I created this as a 'function so all those macro users out there could 'use it also. :P 'if no path is passed to function, exit - no further 'need to do anything
If IsNull(strDocPath) Or strDocPath = "" Then Exit Function End If
Dim dbs As Database Dim objWord As Object Dim PrintResponse Set dbs = CurrentDb
'create reference to Word Object
Set objWord = CreateObject("Word.Application")
'Word Object is created - now let's fill it with data.
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 **
'find out if the user would like to print the document 'at this time.
PrintResponse = MsgBox("Print this document?", vbyesno) If PrintResponse = vbYes Then objWord.ActiveDocument.PrintOut Background:=False End If
'release all objects
Set objWord = nothing Set dbs = nothing
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! :)
Hope this helps - leave me a note if you have any questions regarding this FAQ.
PS: Also, if you would like the Word document to stay open for editing, comment out the Set objWord = Nothing line. By releasing the objWord variable into digital oblivion, you also release the reference to the document you have open. :)