Launch MS Word from within Access XP

D

Deb Roberts

I'd like to be able to generate a document based on a Microsoft Word
template, from within my Access XP database.

Is there a quick, easy way to do this.

I've found the create object code, but all this does is open Word. I need
it to open a particular template.

Thanks in advance.

Deb
:)
 
C

Cheryl Fischer

There are a couple of ways to go about this. Below is some sample code
which opens a specific template and then puts data found on a form into
bookmark.

You will need to set a couple of references for this to work: In Access,
open any Module in design view. Then select Tools|References and make sure
that you have a reference to the appropriate Word library. Look for
"Microsoft Word xx.x Object Library, where xx.x is the Word version number.
For this example, you will also need a reference to DAO.

' Behind a command button on a form, insert the following code.

BEGIN CODE SAMPLE:

Dim objWord As Object

' Open Microsoft Word using automation
Set objWord = New Word.Application

objWord.Documents.Add "c:\FormLetter.dot"
objWord.Visible = True

' The following code is generated from your Access form while it is
' manipulating an instance of Word.
' So, while it looks somewhat like VBA, it's really Word VBA.

If objWord.ActiveDocument.Bookmarks.Exists("FirstName") = True Then
objWord.ActiveDocument.Bookmarks("FirstName").Range.Text = me!FirstName
End If

If objWord.ActiveDocument.Bookmarks.Exists("LastName") = True Then
objWord.ActiveDocument.Bookmarks("LastName").Range.Text = me!LastName
End If

' ... continue reading data from form and inserting into bookmark

END CODE SAMPLE

hth,
 
D

Deb Roberts

Thanks I appreciate the help

Deb

Cheryl Fischer said:
There are a couple of ways to go about this. Below is some sample code
which opens a specific template and then puts data found on a form into
bookmark.

You will need to set a couple of references for this to work: In Access,
open any Module in design view. Then select Tools|References and make sure
that you have a reference to the appropriate Word library. Look for
"Microsoft Word xx.x Object Library, where xx.x is the Word version number.
For this example, you will also need a reference to DAO.

' Behind a command button on a form, insert the following code.

BEGIN CODE SAMPLE:

Dim objWord As Object

' Open Microsoft Word using automation
Set objWord = New Word.Application

objWord.Documents.Add "c:\FormLetter.dot"
objWord.Visible = True

' The following code is generated from your Access form while it is
' manipulating an instance of Word.
' So, while it looks somewhat like VBA, it's really Word VBA.

If objWord.ActiveDocument.Bookmarks.Exists("FirstName") = True Then
objWord.ActiveDocument.Bookmarks("FirstName").Range.Text = me!FirstName
End If

If objWord.ActiveDocument.Bookmarks.Exists("LastName") = True Then
objWord.ActiveDocument.Bookmarks("LastName").Range.Text = me!LastName
End If

' ... continue reading data from form and inserting into bookmark

END CODE SAMPLE

hth,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top