obrien.james
08-16-2013, 05:50 AM
Hi,
I am trying to write some VBA that will connect to a database and pull down some values and insert them into the document. I have achieved this using form fields with no problems.
The code uses the field name to identify what information to select. However, I have hit a problem with form fields - you cannot put names with spaces or . in - this makes it difficult for me to select so other data from our database.
However, mailmerge fields allow me to have more complex field names (as well as have multiple copies of the same field name in the same document). However I cannot seem to edit the mail merge fields as they are read only.
I have managed to update all fields using the following code:
Dim f As Word.FieldFor Each f In ActiveDocument.Fields
If f.Type = wdFieldMergeField Then
f.Result.Text = "the text you want"
End If
Next
However I cannot just get it to update a specific field using
IF f.code = "Field Name"
Im not sure what to try next. Is there another way to update specific mail merge fields using VBA or any advice on the code that I am using?
James
I am trying to write some VBA that will connect to a database and pull down some values and insert them into the document. I have achieved this using form fields with no problems.
The code uses the field name to identify what information to select. However, I have hit a problem with form fields - you cannot put names with spaces or . in - this makes it difficult for me to select so other data from our database.
However, mailmerge fields allow me to have more complex field names (as well as have multiple copies of the same field name in the same document). However I cannot seem to edit the mail merge fields as they are read only.
I have managed to update all fields using the following code:
Dim f As Word.FieldFor Each f In ActiveDocument.Fields
If f.Type = wdFieldMergeField Then
f.Result.Text = "the text you want"
End If
Next
However I cannot just get it to update a specific field using
IF f.code = "Field Name"
Im not sure what to try next. Is there another way to update specific mail merge fields using VBA or any advice on the code that I am using?
James