Controls>Legacy Forms") (MS O..." /> Controls>Legacy Forms") (MS O..." /> Controls>Legacy Forms") (MS O..."/>

VBA Word - Change Duplicated Text Form Field Name

1.1k views Asked by At

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?

2

There are 2 answers

0
Brethlosze On BEST ANSWER

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

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

I wasn't able to replicate the difficulty you are having and suggest the following test.

Private Sub ListFields()

    Dim Fld As FormField
    Dim i As Integer

    With ActiveDocument
        For Each Fld In .FormFields
            i = i + 1
            Debug.Print i, Fld.Name, Fld.Result
        Next Fld
    End With
End Sub

Running this code will print a list of your FormFields to the VB Editor's Immediate Window, giving the index, name and result. Address the fields whose names you wish to change by their index number. You should be able to make all changes you wish.