Solved

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

Posted on 2011-02-16
3
294 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:ianLMurdoch
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 50 total points
ID: 34905871
If necessary, set the Word Option in the 'Popular' set to show the Developer tab.

In the Controls group of that tab, use the drop-down of the lower right-hand button to show 'Legacy Forms'. Clicking the top left-hand button will put a forms-type textbox at the selection point.

To name the field. right-click on it and select properties. Change its bookmark name to 'fldCountry' or whatever is appropriate.

Don't forget to protect the document for filling in forms.

If you uncomment your code, you would have to find  set the doc object, so:

Set doc = oApp.Documents.Open( FileName:=filepath)
0
 
LVL 15

Expert Comment

by:cquinn
ID: 34905916
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
0
 

Author Closing Comment

by:ianLMurdoch
ID: 34906095
Thanks worked like a dream, I could not find that bit of information anywhere.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now