Link to home
Create AccountLog in
Avatar of ianLMurdoch
ianLMurdochFlag for Luxembourg

asked on

Open word document from inside a database and populate word doc with some data

Hi
I have a customer management database I have written in Access 2007.  I would like to be able to click on a button when in the customer contact form to open a word document template and populate the document with the name and address details of the customer.

I have created the button, with a clik event to look for and open the relevent word document.  This works but I am not sure how to setup my word document with fields that access can pass data to.

Anyone have this working or know how to set u p the word document for update ?

Below is my VBA code for the open word document button (test code no real document or fields to be updated are listed )


Dim filepath As String
Dim oApp As Object

'Path to the word documentto use
filepath = "c:\text.doc"
'
If IsNull(filepath) Or filepath = "" Then
        MsgBox "Document not found - please check"
Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True

'Open the Document
oApp.Documents.Open FileName:=filepath
' specify the fields to be updated (commented out here)
' with doc '.FormFields("fldCountry").Result = Me!Country
'.Visible = True
'.Activate
'End With
End If
'
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The easiest way is to set bookmarks in the word document, then populate these in your routine

Another way is to write the data to a file and use this as the source data for a mailmerge
Avatar of ianLMurdoch

ASKER

Thanks worked like a dream, I could not find that bit of information anywhere.