Merging data with MS Word documents

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, another newbie challenge here.... I'm working with MS access prof 2003
and I'm working on a customer database. What i want to do is to make a word
button in the from (thats no problem) that automaticly fills in a word
template (in this case a contract) with the access data of the record that im
working in.

any ideas.....?? Hope so, thanks in advance....

Vincent
 
Hi Vincent,

Typically you'd put a Word bookmark at each point in the contract
where you need to insert text from Access, then set a reference to the
Word object model and write VBA along these lines (this is air code).

I'll assume that all the data you need is displayed in textboxes on
your form, and that the textboxes have regular names (e.g. the name of
the party of the first part is in the textbox txtParty1).

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim strTemplate as String
Dim strFileSpec As String


'Grab an instance of Word if one is running
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If oWord Is Nothing Then
Set oWord = CreateObject("Word.Application")
End If
On Error GoTo 0

'Create new document from template
strTemplate = "MyTemplate.dot"
Set oDoc = oWord.Documents.Add(strTemplate)

'Poke data values into document at bookmark locations
With oDoc
.Bookmarks("Party1").Range.Text = Me.txtParty1.Value
.Bookmarks("Party2").Range.Text = Me.txtParty2.Value
'and so on
End With

'Save and close document
strFileSpec = "D:\Folder\My Contract.doc"
oDoc.SaveAs(strFilespec)
oDoc.Close

That's the general idea. If you want to let the user choose the name
and location of the Word file, see
http://www.mvps.org/access/api/api0001.htm

There's useful information on making Word do what you want at
http://word.mvps.org
 
Back
Top