How to add mergefield in VB Code

  • Thread starter Thread starter chetna via AccessMonster.com
  • Start date Start date
C

chetna via AccessMonster.com

I have to add one merge field from word in VB code.

As when the letter opens it ask for one of the option from user and when user
click one, it show some text and some database field based on the option
selected.

I used

ActiveDocument.MailMerge.DataSource..DataFields.Item("refno").Value

line which is not working.

Can anybody help please.

Thanks in advance
 
Hum, if you use my Super Easy word merge, then you can place un-bound text
boxes on the screen, fill them out, and when you press the button to merge,
those un-bound text boxes ARE AVAILABLE as a merge field.

The code to merge the ONE record you are viewing in the current form?

ONE LINE OF code!!! That line of code is:

MergeSingleWord

The above command will merge the current record.

You can find my help code routines here:
http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html
 
Hi Albert

First of I appretiate you by giving me the quick reply.

What I am doing is, I am using a userform for input from user. For example,
user choose 28th option which is having its own conditions. On selecting this
option some text should appear on the mailmerged letter.

As it is under some condition I am using the following code:

Case 28

text_value = text_value & Chr$(13) & Chr$(13) _
& "I have received confirmation from HM Revenue & Customs that
" _
& ActiveDocument.MailMerge.DataSource.DataFields.Item(0).Value

End Select
End If
Next i

ActiveDocument.Bookmarks("Inserttext").Range.InsertAfter text_value

Now I am able to insert text but in between that text I need to add some data
from datasource and I am needing this help urgently but not able to find any.

Can you please help?


:
 
First, my exmaples don't use bookmarsks.

The reason for this is that bookmarks are HARD to use. Furhter, when I am
paid by a client to do work, they consider IT VERY DISHSONTS that they have
to call me in, and new code has to be written for each new word documne (can
you iamonage if you had to pay micsfot for each new letter you write?). So,
as a general rule, I tend to advoied book marks. My existing solution allows
you to build a wrod merge doucment FOR ANY FORM, and not have to write new
code, or, at the very least not HAVE TO HARD CODE FIELD NAMES in your code.

So, my soltion does not work well (in fact at all) for bookmarks. Worse, is
if you place bookmarks in the tample, and exeucte a standard word merge
(which my code does), then the book marks are GONE!

So, what we need to do is DUMP the book marks. (or, simply dump the use of
my solton).

However, I don't thnk book marks are needed..

your merge buttion code cousl simple doe the folwlign:

1 - run your "case 28" code as you ahve below, and when done, simply place
that resuting desired text into unbound text box on your form, and THEN
execute my merge....

so, somting like:

In your word docuemnt, you would place the two merge fields

<CaseText> <NameOfFirstMergeField>

Then, run your code like:

Case 28

me.CaseText = text_value & Chr$(13) & Chr$(13) _
& "I have received confirmation from HM Revenue & Customs
that


likey the code of
me.CaseText = text_value

would be run at the end of the above select case. (in other words, simply
write your code to STUFF into a un-und text box that you need. You then
place this text box into the word document as mergefield. Try placing a
un-bound text box on your form. Now, use my system, and go "modify
template"...you will see the un-bound text box appear as a merge field.

After all your code runs to setup the unbound text box, the last line to
launch the merge would be:

MergeSingleWord

So, simply build up your "special" text into a unbound text box, and then
use that un-bound text box in the word merge...

(note with my merge system, both bound, and un-bound text boxes are
available as a merge. This means that for each form you use with my merge
system, you don't have write new code that involves book marks. Of course,
for your special text you have as above, you do need some code, but at least
the same merge library can be used over and over)

So, don't use a book mark. Simply have a un-bound text box on your form. You
can concatenates the text either by code, or simply place the two merge
fields (the un-bound text box) and the other field beside each other in the
word document, and it will concatenate for you.
 

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

Back
Top