Some Word Programming Questions

N

Noor

Hi Everyone.

I have few questions

1) Can we have fields in ms word document where we can populate data
from database depending on conditions.

e.g., I want to have wizard like thing in word and populate those fields
depending on what the user select at runtime. Plz tell me how can i achieve
this functionality. I know i can open database connection in and selects the
data but how can i know where to populate the data. I mean is there a
label/textbox like mechanism from which i can know what to update.


2) Can I include or exclude text that's already in the document on the
basis of checks. e.g. i have 3 people resume in my template and i show user
to select if he wants to include all the resume or not. if he say 2 then
include two resume and if he selects three then include three resume.


Plz .. provide me answers to these questions.

Noor
 
P

Perry

1) Can we have fields in ms word document where we can populate data
from database depending on conditions.

Certainly, Dropdown formfields.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbawd10/htm
l/woobjdropdown.asp

Here's an example of code populating this dropdown formfield against
certain conditions.
Situation:
Database in "c:\temp\db1.mdb" containing 2 tables:
- Table1 - with fruits
- Table2 - with other consumables

== begin code (standard module)
Public Const DBProv As String = "Microsoft.Jet.OLEDB.4.0"

Sub PopulateDropDown()
Dim fdd As FormField

Dim con As New ADODB.Connection
Dim rst As New ADODB.Recordset

'unprotect doc to access field properties
ActiveDocument.Unprotect

'set object variable pointing to dropdown
Set fdd = ActiveDocument.FormFields(1)
'clear previous listentries of formfield
fdd.DropDown.ListEntries.Clear

'open database connection
With con
.Provider = DBProv
.ConnectionString = "data source = c:\temp\db1.mdb"
.Open
End With

'dialog to select the table in question
If MsgBox("Do you want to load fruits?", vbQuestion + vbYesNo) _
= vbYes Then

Set rst = con.Execute("select * from table1")
Else
Set rst = con.Execute("select * from table2")
End If

rst.MoveFirst

'populate the formfield
Do Until rst.EOF
fdd.DropDown.ListEntries.Add rst(0)
rst.MoveNext
Loop

'reprotect document
ActiveDocument.Protect wdAllowOnlyFormFields

'close database connection
con.Close

'clear object variables (in this example actually obsolete)
Set con = Nothing
Set rst = Nothing

End Sub
== end code
2) Can I include or exclude text that's already in the document on the
basis of checks.

Sure, you might want to consider using autotext entries for this purpose.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/ht
ml/output/f1/d4/s5a904.asp

Krgrds,
Perry
 
J

Jezebel

A simple way to put data into your document if you're dealing with single
items of data (like one record from your database) is to use
DocumentProperties.

Set up your template with CustomDocumentProperties for each item of
information: File > Properties > Custom. Insert a dummy value for each one.
(eg ClientName = [ClientName])

In the body of the document where you want the information to appear insert
a DocProperty field. (eg {DocProperty ClientName }.

In your code set the property value:

ActiveDocument.CustomDocumentProperties("ClientName") = rs("ClientName")

Then update fields in the body of the document.
 

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