Search code examples
vbams-wordform-fields

VBA Word - Change Duplicated Text Form Field Name


I get an automation error when trying to write to the Name property in any document with a "Text Form Field" ("Developer>Controls>Legacy Forms") (MS Office 2013):

Sub EditCopiedFormField()
    Selection.MoveDown Unit:=wdLine, Count:=4, Extend:=wdExtend
    Selection.Copy
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.Paste
    ActiveDocument.FormFields(3).Name = "ID999"
End Sub

enter image description here

enter image description here

Is the ActiveDocument.FormFields(i).Name field writable after a Selection.Copy-Selection.Paste operation?

After copying and pasting some "Text Form Field", I am having no issues by reading the Name ("Bookmark" Field in the UI Dialog) property, but I am unable to write it (presumable because the Form Field turned duplicated after the copying), receiving the standard error:

Method 'Name' of object 'FormField' failed

For example, I have two "Text Form Fields" with the same Name: "ID001" after copying a range including them. By editing the duplicate through double clicking the form, the Name ("Bookmark") of the duplicate gets instantaneously cleared: "".

Trying to add a "Bookmark" before the Name change, noting that no "Bookmarks" are duplicated through the menu "Links>Bookmarks" in the UI:

`ActiveDocument.Bookmarks.Add("ID999")` 

do not change the situation.

What must I do to change these duplicate names?


Solution

  • Thanks to The Anchorage website, this code appears to do the trick:

    ActiveDocument.FormFields(i).Select
    With Dialogs(wdDialogFormFieldOptions)
        .Name = "ID999"
        .Execute
    End With