• Naming A FormField (Word 2003/sp2)

    Author
    Topic
    #439728

    I have a form with many many fields in it. These fields need to be renamed and other options set.
    The problem: I can’t set the name if I use a string variable. If I simple write the name in quotes it works – but at that rate I might as well open up each field in word.

    Sub ReSetFieldsNew()
    Const Split = "_"
    Dim strGroupName As String
    Dim IntChoices As Integer
    Dim intCounter As Integer
    Dim strFieldName As String
    Dim rngDoc As Range
    
    strGroupName = InputBox("Enter QuestionName")
    IntChoices = Selection.FormFields.Count
    
    Set rngDoc = Selection.Range
    For intCounter = 1 To IntChoices
       strFieldName = strGroupName & Split & IntChoices & Split & intCounter
       Debug.Print strFieldName
       With Selection.FormFields(intCounter)
          .Name = strFieldName ' this doesn't work - however if I use "Fred_3_1" everything is good
          .EntryMacro = "RadioCheckBox"
          .ExitMacro = "RadioCheckBox"
          .CalculateOnExit = True
          Debug.Print .Name
       End With
       rngDoc.Select
      
    Next intCounter
    End Sub
    

    I’d sure appreciate another pair of eyes, to see what I’m missing.

    Viewing 0 reply threads
    Author
    Replies
    • #1051362

      It works without problems for me (Word 2003 SP2 too) scratch

      Does the problem persist if you quit and restart Word?

      • #1051461

        I’m wondering if I’m missing a reference?

        • #1051467

          No, if you needed an extra reference, you’d have gotten a syntax error instead of a run-time error.

      • #1051462

        Yes the problem persists and continues on both my desktop and laptop machines.
        Here’s a snap of the error msg

        • #1051469

          Earlier today, I tested in Word 2003, now in Word 2002. Again without problems.

          Could you attach a small dummy document with some form fields and the code (after verifying that the error still occurs in it)?

          • #1051472

            Hey, in building my fake document I think I came partway.
            1) fails if the selected text is an entire cell.
            2) in my original document every field seems to have the same name. So they aren’t bookmarked properly.

            • #1051477

              I think you’re on to something. The code also fails if at least one of the form fields has no name. Duplicate names and blank names can easily occur if you copy and paste form fields.
              So
              a) Don’t run the code on a single entirely selected cell.
              Make sure that all form fields have unique, non-blank names before running the code.
              Not easy if you want to make the code available to end users!

            • #1051480

              So, I did wind up manually renaming fields. But at least I know why now.

            • #1080989

              I have just come across this problem. Specifically

              So
              Once you have formfields in a document with non-unique names, can you ever use .name to assign a name of a formfield?
              If no this pretty much kills copy/paste of any range with fields in it.

              Also, does this problem exist for code created fields

              I am trying to make an form with section that can expand based on data entry. Working with out field names will require some serious rethinking

              thanks

            • #1080992

              Yes, it’s a nuisance. You cannot assign a name to a nameless form field in code, only interactively. You can, however, assign a name to a form field that you create in code:

              Dim strName As String
              strName = …
              With ActiveDocument.FormFields.Add(Selection.Range, wdFieldFormTextInput)
              .Name = strName

              End With

            • #1080994

              piffle.
              I was hoping to use cut/paste to create my expanded data section. Creating the rather complex structure with code will be tedious.
              Either that or figure out how to do the whole thing without rely on names
              oh well. I guess that’s why I get the big bucks…

              Thanks for the prompt response

            • #1081000

              or I could cut/paste the structure then add the FormFields…
              just a bit slow sometimes
              shesh

            • #1081028

              You could copy the range, then rename the existing fields to unique names, then paste the materials containing the original names. Actually, does that work? I suppose it depends on whether Word is storing a copy of the original or a pointer to the original…

            • #1081035

              Problem is that if you copy/paste a form field, the copy is nameless, since you can’t have two bookmarks with the same name. It would have been nice if Microsoft assigned a name with a sequential number, as it does when you copy/paste controls from the Control Toolbox (a copy of txtInvoice becomes txtInvoice1, then txtInvoice2 etc.), but unfortunately that is not the case.

            • #1081369

              Hi Hans,

              There’s a workaround for nameless formfields here: http://support.microsoft.com/kb/211366%5B/url%5D

              Cheers,
              Paul Edstein
              [Fmr MS MVP - Word]

            • #1081381

              Ah! Good old WordBasic, thanks!

              (Why doesn’t Microsoft bother repairing VBA instead of letting us rely on a language that was retired 10 years ago?)

    Viewing 0 reply threads
    Reply To: Reply #1081000 in Naming A FormField (Word 2003/sp2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel