1
votes

Im using access to edit Word documents, and im using this code (example) to fill formfields in the document:

.FormFields("txtProsjektNavn").result = DLookup("txt", dbName, "key = 
'byggNavn'")
.FormFields("txtProsjektNr").result = DLookup("value1", dbName, "key = 
'prosjektNr'")
.FormFields("txtOverblikk").result = DLookup("txt", dbName, "key = 
'txtOverblikk'")
.FormFields("txtByggEier").result = DLookup("txt", dbName, "key = 
'byggEier'")

This is all working fine and dandy, however. Some of these values needs to be repeated several times in the document. Ive figured that giving 2 formfields the same bookmark name in the word document is a no-no. So instead of having to create lots of formfields with different names, i attempted to create REF fields that refered to the already made form fields, however, they do not update when access updates the formfields themselves.

Does anyone have a solution to this or do I simply need to make lots of formfields with different names, like: text1, text2, text3 and then have access fill them all with the same value.

2

2 Answers

1
votes

You have already filled in your required form fields. If you want to repeat these values you can use cross-reference

Once you set all the fields following the steps below, then you can auto update fields using ActiveDocument.Fields.Update after populating your form fields.

Insert -> Cross-Reference(Under Links)

enter image description here

Select Reference type as Bookmark and Insert

enter image description here

Use ActiveDocument.Fields.Update in vba to update fields.

enter image description here

0
votes

I tried the suggested solution but my problem was that when i updated all the fields in the document the Form-fields reset to blank. I worked around this by looping through the cross-reference fields only, and hitting update on them individually.

    docField as Word.Field

    For Each docField In doc.Fields
    If docField.Type = wdFieldRef Then
        docField.Update
    End If
    Next docField