Using VBA to merge complex data

J

JBob

I have a Word XP document with merge codes (converted from WP). The data
source is several tables from my database. Rather than construct a complex
query to get all of the data into a single table to use as my merge data
source, I want to use VBA to populate the document. How can I identify the
Mergefields in code to achieve this?
 
D

Doug Robbins

I would suggest that creating a query to combine the data from the tables
would be far simpler than what you are proposing.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
J

JBob

I am able to use the following construct to achieve what I need (that is,
using VBA to populate merge fields):

'
'-- replace all occurrences of Analyst (mergefield in main document)
'
sSQL = "select FirstName, LastName, FullTitle from Employees where
Initials='" & sAnalystInitials & "'"
rsADO.Open sSQL, gsConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
sAnalyst = rsADO!Firstname & " " & rsADO!lastname & ", " & rsADO!FullTitle
rsADO.Close
Dim fieldLoop As Field
For Each fieldLoop In ActiveDocument.Fields
If InStr(1, fieldLoop.Code.Text, "Analyst", 1) Then
fieldLoop.Select
Selection.TypeText sAnalyst
End If
Next fieldLoop

Some Questions:
1. Is there a better way?
2. Am I in the right newsgroup for this problem?
 

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