Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Insert Fields At Run Time in VB?

0 views
Skip to first unread message

Melissa

unread,
Nov 23, 2001, 9:43:07 AM11/23/01
to
I've been reading through emails in this newsgroup but
didn't find anything where you can insert fields at run
time before merging data.

We're setting a default tempelate for the user in VB6 app,
but they can remove/add/change fields orders (we have two
listboxes lists - one with available fields and another
one with Fields on Router).

I'm thinking I could update the query based on the fields
user selected and insert them programmatically from VB and
then have it bought up for user to view the changes made.

Is this possible?

Many thanks!

Peter Jamieson

unread,
Nov 25, 2001, 5:52:57 AM11/25/01
to
> Is this possible?

If I have understood you correctly then it is possible, but I cannot give
you a complete chunk of code for the task.

You can get/set the Query via

ActiveDocument.MailMerge.DataSource.QueryString

I'm not sure that you would actually need to update the query as long as all
the fields the user needed could always be retrieved by a simple

SELECT * FROM A WHERE ...something...

If you start naming specific fields to retrieve then you will need to be
careful that your QueryString does not exceed a rather short length limit
(about 250 characters I think).

You can get the list of fields in the current data source using

For Each f In ActiveDocument.MailMerge.DataSource.DataFields
Debug.Print f.Name
Next


You could for example insert all the MERGEFIELD fields for the current data
source using

With ActiveDocument
For Each f In .MailMerge.DataSource.DataFields
Selection.Fields.Add Selection.Range, wdFieldMergeField, f.Name
Next
End With

Does that give you enough clues to get started?

Peter Jamieson
MS Word MVP

"Melissa" <mlr...@ctchome.com> wrote in message
news:a70b01c1742d$2a6d96e0$35ef2ecf@TKMSFTNGXA11...

Melissa

unread,
Nov 26, 2001, 8:54:15 AM11/26/01
to
Yes, that was helpful! I'm concerned, though - there are
many fields user could select - 35 possible fields!! That
probably would exceed 250 characters? I hope there is
another way to do this, I'll have to look into it.

But thanks so much for replying - it was enough to start
somewhere!!!

Melissa

>.
>

btconnect

unread,
Dec 3, 2001, 5:22:52 AM12/3/01
to
Hi,

i've just seen the code you give for inserting mergefields automatically
which i think is very useful!

is it also possible to instert a character return so that each merge field
appears on a separate line! Please give example.

also, i've created my own save button. Would you know how to check if a
document already exists, and if so display a message informing the user then
cancle the save event.


Thanks

Jado


Peter Jamieson

unread,
Dec 4, 2001, 3:47:55 PM12/4/01
to
Jado,

Would you mind asking these programming questions in a more general VBA
group, for example

microsoft.public.word.vba.beginners ?

The reason I ask is because I do as little programming as I possibly can, so
the material isn't at my fingertips and I do not keep tested examples to
hand. How to insert a carriage return into Word is a general Word
programming question, and to be honest, if you are programming in Word VBA I
think you need to take a little time to work out how to do basic things,
both in VBA and in the Word object model. I'm not in a good position to give
you a tutorial on how to program in Word VBA (but you may find useful
material at http://www.mvps.org/word )

If you're still having problems with mailmerge and field-oriented questions
after that, I'll do what I can.

Peter Jamieson
MS Word MVP

"btconnect" <plo...@hotmail.com> wrote in message
news:loIO7.11788$Sk6.73789@NewsReader...

0 new messages