Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fill Word-document fields from Access database

Status
Not open for further replies.

gransbpa

Programmer
Jun 5, 2001
98
0
6
NL
Hi there,

I have an MSAccess2000 database with client-data. From one screen, I want to print a Word(2000) document. I implemented the code which opens the document, now I want some blanks in the document to get their data out of access, e.g. a name, a street, an adress, etc. I tried mailmerge, but this does not seem to work, this only gives a one time "static" filling of data, but I want the data displayed which are currently loaded in the form from which the document is generated. Does anyone know how to do this?

Any help will be greatly appreciated.

 
I have done this ... (maybe not as elegant as I should have)

but here goes,

I have an access form, in which the user selects "criteria" for the letter. On that form are buttons for various letters that are based on that info. when a button is clicked, I have it call a make table query that has it's criteria linked to the active form. (I used to be able to just use a select query in office 2000, but mail merge in XP refuses to read a query with expressions in it), so the work around was to build a table via a make table query and then I could merge my letter to the table that is made via the make table query.

The button's code then calls an instance of Word and opens a letter that is mail merged to the table that was made via the make table query, prints it and closes the instance of word,..returning control to the form.


 
Hi gransbpa,

We are working on something like this at the moment. We open a Word Template that has Bookmarks set. Find the bookmark and paste in the data. Like this:

' Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("YourFieldName").Select
.Selection.Text = (CStr(Me!YourFieldName))

Reapeat that for every piece of data. If there is no data to paste then trap the error and continue something like this:

' If a field on the form is empty
' remove the bookmark text and continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
End If

Gallas :)

"If a job's worth doing, it's worth doing twice!"
 
Thanks EDIT, gallas,

Both solutions seem to work! I'm still playing around a little but I almost have the solution I wanted.

Many thanks!!
 
Hello, I am trying to do the same sort of thing - paste data from a Access form into a word document which has the bookmarks set except i get the following error:

"Run-time error 4248
This command is not available because no document is open"

I've tried the code from gallas and still get the same error. The M$ knowledge base didnt give me much help

Any ideas?
 
Make sure you set load the object into memory:

Dim oWordApp As Word.Application
Set oWordApp = New Word.Application

And then open the object:

oWordApp.Documents.Open App.Path & "\YourDocNameHere"

With oWordApp
.ActiveDocument.Bookmarks("bookmark1").Select
.Selection = rs.Fields("Field1")
.ActiveDocument.Bookmarks("bookmark2").Select
.Selection = rs.Fields("field2")
End With

HTH

jocasio
 
Hi Manners,

You might also like to use something like the following to find out if Word is already running. Otherwise your users could open multiple instances of Word which could confuse.

Dim appWd As Object

On Error Resume Next

Set appWd = GetObject(, "Word.Application")
If appWd Is Nothing Then
Set appWd = CreateObject("Word.Application")
End If

One other thing. You will probably prefer to use a Word template. If this is the case then the following probably suits:

With appWd

appWd.Visible = True
appWd.Documents.Add Template:= "Path\yourWordTemplateFile.dot", NewTemplate:=False, DocumentType:=0

The above code works for us, may not suit you but overcame the problems we faced with this type of op.

Regards, Gallas %-)

"If a job's worth doing, it's worth doing twice!"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top