Sending data from Access to Word

G

Guest

I need to send certain data from an open form in Access to Word. I need Word
to open a new document based on a custom template, and insert the proper
information at bookmarked fields. The user will then email the Word document
back and forth with other users for additional data. Using the following
code, the debugger stops me on line 2 and says the user defined function is
not recognized. I'm lost, I'm severely VBA challenged, and I have just two
days to get this running before I go out for surgery. Any help would be
appreciated. Alternatively, send alcohol...

Thanks!

Public Sub PrintToWord()
Dim wd As Word.Application
Dim myDoc As Word.Document

Set wd = New Word.Application
wd.Documents.Add “J:\Forms Designers\Conversions and updates\Forms HC-HD\2
Draft\HD AppointmentDB.dot†' The name and path
Set myDoc = wd.ActiveDocument
With wd.Selection
.GoTo wdGoToBookmark, Name:=â€Client†' The name of the bookmark
‘.Font.Bold = True ' You can choose if bold
‘.Font.Underline = False ' or underline
.TypeText Text:=me.ClientID ' insert values in field
End With
wd.Visible = True
 
J

John Nurick

Hi Helen,

It's usually better to avoid using the Selection object when automating
Word. Try something like

Set wd = New Word.Application
Set myDoc = wd.Documents.Add "blah blah.dot"

With MyDoc.Bookmarks
myDoc.Bookmarks("Client").Range.Text = Me.ClientID.Value
...

End With

myDoc.Save

Also, remember that if you replace the text of a bookmark, either as
above or by selecting it and using TypeText, you destroy the bookmark.
There are techniques for saving the location of the bookmark first and
re-creating it later. An alternative is to use formfields where you want
to put the data: these are more permanent.

For more on Word, see http://word.mvps.org
 
D

Douglas J. Steele

It looks as though you haven't set a reference to Word in your Access
application.

With any code module open, select Tools | References from the menu bar.
Scroll through the list of available references until you find the one for
Word (It'll be something like "Microsoft Word x.0 Object Library") and
select it.

Alternatively, change your code from the Early Binding you're currently
using to Late Binding:

Public Sub PrintToWord()
Dim wd As Object
Dim myDoc As Object

Set wd = CreateObject("Word.Application")
wd.Documents.Add "J:\Forms Designers\Conversions and updates\Forms HC-HD\2
Draft\HD AppointmentDB.dot" ' The name and path
Set myDoc = wd.ActiveDocument
With wd.Selection
.GoTo wdGoToBookmark, Name:="Client" ' The name of the bookmark
'.Font.Bold = True ' You can choose if bold
'.Font.Underline = False ' or underline
.TypeText Text:=me.ClientID ' insert values in field
End With
wd.Visible = True

The latter approach is better if your application is going to be used by
multiple users, and you're not sure which version of Word they'll have
installed.
 
G

Guest

Thanks, Doug and John. I'll try these and see if I can get it working. I'm
just so lost with even VBA basics. :)
 

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