0

I am populating values into DocVariable fields using Access VBA throughout documents. This part is mostly working. The only part that is not working is when the DocVariable fields are placed inside of textboxes.

Legend a. DOC VARIABLE EXAMPLE: {DOCVARIABLE "Department_Name" * MERGEFORMAT}*** b. TempVars!Depart_Name.Value Is a textstring c. MergeDoc is a word template document

I create the word instance and then provide the template to the instance

  1. Set oword = CreateObject("Word.Application")
  2. Set wrd1 = oword.Documents.Add(MergeDoc)

Now I populate the DocVariable with the value

  1. wrd1.Variables("Depart_Name").Value = Nz(TempVars!Depart_Name.Value, " ")

This all works until I try to populate the DocVariable when it is contained within the textbox. Nothing happens. It is like it does not see the DocVariable at all when it is located within the textbox.

Additionally, When I perform the below, It behaves the same way as when populating the values into DocVariable fields when within a textbox.

Dim fld As Word.Field
For Each fld In wrd1.Fields
    If fld.Type = wdFieldDocVariable Then
       fld.Unlink
    End If
Next

It is like it does not know the fields are present Joe

macropod
  • 12,757
  • 2
  • 9
  • 21
  • You need to loop through the Shapes collection so as to find the textboxes and process each of them individually. See, for example: https://stackoverflow.com/questions/65986920/attempting-to-delete-a-page-in-microsoft-word-vba-that-contains-specific-text – macropod Feb 01 '21 at 21:59
  • Isn't this just a restatement of https://stackoverflow.com/questions/65963390/replacing-strings-with-docvariables-located-within-textboxes – Rich Michaels Feb 02 '21 at 00:32
  • Does this answer your question? [Replacing strings with docvariables located within textboxes](https://stackoverflow.com/questions/65963390/replacing-strings-with-docvariables-located-within-textboxes) – Rich Michaels Feb 02 '21 at 00:33
  • I am successfully replacing strings with variables without an issue at this point. What I'm not able to get working using the .variable of a document to populate the value into the field. This command places the value of a string to be stored by the variable. wrd1.Variables("Depart_Name").Value = "SomeTextValue" – Joseph Cameron Feb 02 '21 at 05:22
  • After you have populated the data in the document variables you need to update the fields in the document so that they display the value. You can find code to update all fields, including those in text boxes, in this question https://stackoverflow.com/questions/33733113/macro-to-update-all-fields-in-a-word-document – Timothy Rylatt Feb 02 '21 at 12:09

0 Answers0