Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Microsoft: Access Forms FAQ

How to

Opening a Word Document and filling it with data from an Access Form by grtammi
Posted: 26 Aug 02

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
       'move to each bookmark, and insert correct text.
       .ActiveDocument.Bookmarks("<bookmark name>".Select
       .Selection.Text=(Cstr(Forms!<form name>!<field
       .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

   '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

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,

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. :)

Back to Microsoft: Access Forms FAQ Index
Back to Microsoft: Access Forms Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close