Microsoft Word - Word Automation FormFields Name Unspecified error

Asked By Peter Jones on 31-Jan-12 09:26 AM
Hi,

I'm working with MS Word 2007 and using COM automation.  I have been given a series of documents containing formfields and I want to cycle through the formfields collection and give each formfield a bookmark name, however, when I try to assign the Name I get an "Unspecified Error":

for example:
     FormFields.Item(i).Name := 'FieldName_' + IntToStr(i);

I don't see the same problem when working with documents from other sources, just one particular set of documents.

Cheers

Pete
D Company replied to Peter Jones on 31-Jan-12 11:18 AM
hello friend.

Check the calculate on exit check box of the form field to update the REF field on exit.The REF field will reproduce whatever is in the bookmark. If you want it to reproduce only the content of the form field, use the bookmark name from the field properties.


or go through this post

Sample

and here is best way from scratch

hope this will help you!!

Regards
D
Anoop S replied to Peter Jones on 31-Jan-12 01:00 PM
Change code to this and try

ActiveDocument.FormFields.Item(1).Name = "FieldName_" + IntToStr(i);
Pat Hartman replied to Peter Jones on 31-Jan-12 02:54 PM
Bookmarks are limited to 20 characters in length and may only contain letters, numbers, and the underscore "_".  You are already using 10 characters with your prefix.  I would get rid of the prefix.  What is it doing for you?
Jitendra Faye replied to Peter Jones on 31-Jan-12 10:53 PM
You can looping to FormFileds using this VBA.

Dim iIndex As Integer

Dim vName As Variant

Dim sResult As String

Dim oFormfield As FormField

Dim i As Integer

Dim myrange As Range

For Each oFormfield In ActiveDocument.Tables(1).Range.FormFields

If oFormfield.Type = wdFieldFormDropDown Then

vName = oFormfield.Name

sResult = oFormfield.Result

If sResult = "Production" Then

oFormfield.Select

i = Selection.Cells(1).RowIndex

Set myrange = ActiveDocument.Tables(1).Cell(i, 2).Range

myrange.End = myrange.End - 1

MsgBox myrange

End If

End If

Next

Set oFormfield = Nothing

 

End Sub



TRy this and let me know.
Peter Jones replied to Anoop S on 01-Feb-12 06:53 AM
Thanks for the responses,

but FormFields in my code is an object reference to the ActiveDocument.FormFields collection,
the length of the Field name is not an issue.  The only way I could solve it was by using the FormFields.Item(i).Select, and then inserting a new field with the FormFields.Add() function.  The newly inserted fields behave normally when I assign the Name variable.

Cheers

Pete