简体   繁体   中英

Change value of Word MailMerge field using VBA

I have a document with MailMerge fields, however I do not want to use the whole datasource->mailmerge idea. Instead I am presenting a UserForm on Autonew() and asking the user to type the data into the fields.

The reason is because this document is to "merged" with one row of data so it is a waste of time asking the user to do the whole data source thing for one row.

I have a working solution using DocVariables which I'm sure most people will say is the correct way to do it, however this client likes the idea of "seeing" the mailmerge variable (such as <<1>>) in the source document. They know they can use Alt-F9 to display codes to see the DocVariables, but they insist on wanting to use MergeFields.

Using DocVariables I do it using the following. This works so I know I have the right idea and that the rest of my code works fine.

ActiveDocument.Variables("varSurame").Value = .txtSurname

However, I cannot workout how to do the same thing with merge fields. I want to do something like the below (but there is no "value" property to set).

ActiveDocument.MailMerge.Fields("Surname").value = .txtSurname

The .text property is readonly so I cannot use that.

The following renders "Bookmark not defined" errors.

ActiveDocument.MailMerge.Fields("Surname").Code.Text = .txtSurname

Any ideas on how I can programmatically change the value of a mail merge field without using a datasource.

Something like:

Dim f as Word.Field

For Each f in ActiveDocument.Fields

  If f.Type = wdFieldMergeField Then

    ' you will either need to extract the name of the field from f.code here
    ' (roughly speaking, you should expect to find
    ' MERGEFIELD<white space><optional double quote>fieldname<optional double quote><white space><possible switches>
    ' or you could iterate through a list of field names and use instr to look for each name
    ' then

    f.Result.Text = "the text you want"
  End If
Next ' f

I had a Word Document that I wanted to have DocVariable fields that I could modify from Access via VBA. I too wanted to "see" the mail merge fields. If I created the document with DocVariable fields to start with (eg,, Insert>>Quick Parts>>Field and the insert a DocVariable field), they were either invisible or displayed the entire long field code. So suppose I have fields named FNAME and DONATION in a letter thanking someone for their donation as the document.

The final document should look like:

...Thank you Bob for your donation of $100. It will...

With the DocVariable fields, it either looked like this:

...Thank you for your donation of . It will...

or like this:

...Thank you { DOCVARIABLE "FNAME"} for your donation of { DOCVARIABLE "DONATION"}. It will...

or possibly even longer if the DocVariable fields have things like `* MERGEFORMAT' in them. It might be silly, but I definitely understand the preference for "seeing" the mail merge field, so that it looks like this

...Thank you «FNAME» for your donation of «DONATION». It will...

The way I got it to work was this:

  1. Create the document with mail merge fields. You might have to make a fake Excel file or database to connect to in order to add the fields. After doing this, you'll "see" the mail merge fields in the document (eg, «FNAME» ).
  2. Toggle the field codes (alt-F9) so you see { MERGEFIELD "FNAME"} .
  3. For each mail merge field, replace the "MERGEFIELD" with "DOCVARIABLE". Just type right over it so that it looks like `{ DOCVARIABLE "FNAME"} .
  4. Toggle the field codes again (alt-F9). It will look like a mail merge field (eg, `«FNAME»') but you can use it in VBA like a DocVariable field.

Note that you can actually just insert one mail merge field and then copy/paste it, changing the name for different fields. There must be something hidden in the mail merge field that gets copied when you do this that doesn't exist if you just insert a DocVariable field, but I haven't been able to figure out what it is. So this is definitely a workaround, but it worked for me. It gives you the best of both worlds, "seeing" the mail merge field but getting to use it like a DocVariable field from VBA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM