• Filling FormFields via Macro (Word 2000)

    Author
    Topic
    #402454

    Hello everybody!

    I have a problem using FormFields in macros, and I am not sure whether I have found a bug or am just too stupid to programme the thing properly…
    In my document I have a FormField named “TxtTarget”. I want to write some text in this field by using a macro. Therefore I use the following line:
    ActiveDocument.FormFields(“TxtTarget”).Result = “Here comes the text.”
    This works as expected, at least as long as the text has less than 256 characters. If this length is exceeded, I receive an error message (Runtime Error ‘4609’, String too long). The funny thing is, that there is no defined restriction in the FormField (no limitation for string length set). Also it is possible to write longer text in the FormField directly. The Result-Property contains the elongated string as well. Only it seems not to be possible to convince Word to set the property to a longer string via macro.

    Does anyone have an idea about this? Or a workaround?

    Any help will be appreciated!

    Many greetings, Porley

    Viewing 5 reply threads
    Author
    Replies
    • #800681

      Hi Porley,

      Possible workaround: If you’re only using the formfield to designate the location that the text is to be inserted, why not use a bookmark instead? Then all you’d need to do is to find the bookmark and replace it with the desired text.

      Cheers

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #800682

      Hi Porley,

      Possible workaround: If you’re only using the formfield to designate the location that the text is to be inserted, why not use a bookmark instead? Then all you’d need to do is to find the bookmark and replace it with the desired text.

      Cheers

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #800683

      This is a known problem. See MSKB article String Too Long Using Over 255 Characters for a description and a workaround. The String(256, “W”) part in the code must be replaced by the text you want to write in the form field.

      • #800714

        Hello Hans!

        Thank you very much for this interesting article. It works perfectly, as long as I rund the whole thing under Word. Now I had to learn, that my problem is a bit more complex. Here is the point: I want to generate an automated report based on some calculations done in an Excel workbook. So Excel should create a new word document, which is based on a named template. This template contains a FormField with the mentioned name. Now I want to fill this field from an Excel-macro. Unfortunately I did not find a way to get access on the word-constants such as wdGoToBookmark. Is there any way to realize this? I suppose there is, but I am neither used to writing programmes in Word nor to using ActiveX-Components.

        Maybe, someone could give me a hint.

        Many thanks in advance!

        Greetings, Porley

        • #800717

          In order to use Word VBA in Excel, do the following:
          – Activate the Visual Basic Editor in Excel (Alt+F11)
          – Select Tools | References…
          – Locate Microsoft Word 9.0 Object Library and tick the corresponding check box.
          – Click OK.
          Constants such as wdGoToBookmark should now be recognized.
          To learn more about controlling one Office application from another in code, see the tutorial on Automation on WendellB‘s website. There is a link in his profile; look for Support > Tutorials. If you have specific questions, don’t hesitate to post them.

          I don’t think ActiveX components are involved in this.

          • #800727

            Thanks a lot!! The tutorial is very helpful, indeed. And thank you for your support. Now I see the Word-Constants. But one open item remains: I understand it correctly, that the text is written in front of the FormField and that the FormField itself remains empty? Is there a way to fill the FormField with the string? It would be very useful, since I have to work with the property Result later on. (Or I have to find a completely different way to realize my project).

            Best regards,

            Porley

            • #800736

              No, when I try the code from the MSKB article, the text is written in the form field itself, so if I retrieve ActiveDocument.FormFields(“Text1”).Result later on, it returns the long string. Of course, you must use the actual name of the form field (which is the same as the name of the associated bookmark.) The example uses “Text1”, the default name of the first text form field in the English language version of Word.

            • #800778

              Edited by HansV – yellow text is unreadable on light backgrounds…

              Hello Hans!

              It did not work in my macro, and I do not know why. But I found another workaround. Maybe it is useful also for someone else. Remember, I need the code in Excel, and this macro is placed in a workbook. Now here is the code:

              Sub WordTest()
              ‘ Variablendeklaration
              Dim objWord As Object
              Dim strTask$

              ‘ Start der Routine
              strTask = String(260, “*”)
              Set objWord = CreateObject(“Word.Application”)
              objWord.Selection.Goto Name:=”TxtTask” ‘ Name of the FormField in Word
              Set myText = New DataObject
              myText.SetText strTask
              myText.PutInClipboard
              objWord.Selection.Paste
              Set objWord = Nothing
              End Sub

              If I use this code, I receive the wanted result.
              Thanks again for your support. It gave me the right ideas!

              Best regards,

              Porley

            • #800954

              Thanks for this tip. For anyone trying this, before you can declare a DataObject, you must have a reference to the Microsoft Forms 2.0 object library set in your project (Tools>References…). If you add a UserForm, the reference is added automatically; otherwise, you have to add it yourself.

            • #800955

              Thanks for this tip. For anyone trying this, before you can declare a DataObject, you must have a reference to the Microsoft Forms 2.0 object library set in your project (Tools>References…). If you add a UserForm, the reference is added automatically; otherwise, you have to add it yourself.

            • #800779

              Edited by HansV – yellow text is unreadable on light backgrounds…

              Hello Hans!

              It did not work in my macro, and I do not know why. But I found another workaround. Maybe it is useful also for someone else. Remember, I need the code in Excel, and this macro is placed in a workbook. Now here is the code:

              Sub WordTest()
              ‘ Variablendeklaration
              Dim objWord As Object
              Dim strTask$

              ‘ Start der Routine
              strTask = String(260, “*”)
              Set objWord = CreateObject(“Word.Application”)
              objWord.Selection.Goto Name:=”TxtTask” ‘ Name of the FormField in Word
              Set myText = New DataObject
              myText.SetText strTask
              myText.PutInClipboard
              objWord.Selection.Paste
              Set objWord = Nothing
              End Sub

              If I use this code, I receive the wanted result.
              Thanks again for your support. It gave me the right ideas!

              Best regards,

              Porley

            • #800737

              No, when I try the code from the MSKB article, the text is written in the form field itself, so if I retrieve ActiveDocument.FormFields(“Text1”).Result later on, it returns the long string. Of course, you must use the actual name of the form field (which is the same as the name of the associated bookmark.) The example uses “Text1”, the default name of the first text form field in the English language version of Word.

          • #800728

            Thanks a lot!! The tutorial is very helpful, indeed. And thank you for your support. Now I see the Word-Constants. But one open item remains: I understand it correctly, that the text is written in front of the FormField and that the FormField itself remains empty? Is there a way to fill the FormField with the string? It would be very useful, since I have to work with the property Result later on. (Or I have to find a completely different way to realize my project).

            Best regards,

            Porley

        • #800718

          In order to use Word VBA in Excel, do the following:
          – Activate the Visual Basic Editor in Excel (Alt+F11)
          – Select Tools | References…
          – Locate Microsoft Word 9.0 Object Library and tick the corresponding check box.
          – Click OK.
          Constants such as wdGoToBookmark should now be recognized.
          To learn more about controlling one Office application from another in code, see the tutorial on Automation on WendellB‘s website. There is a link in his profile; look for Support > Tutorials. If you have specific questions, don’t hesitate to post them.

          I don’t think ActiveX components are involved in this.

      • #800715

        Hello Hans!

        Thank you very much for this interesting article. It works perfectly, as long as I rund the whole thing under Word. Now I had to learn, that my problem is a bit more complex. Here is the point: I want to generate an automated report based on some calculations done in an Excel workbook. So Excel should create a new word document, which is based on a named template. This template contains a FormField with the mentioned name. Now I want to fill this field from an Excel-macro. Unfortunately I did not find a way to get access on the word-constants such as wdGoToBookmark. Is there any way to realize this? I suppose there is, but I am neither used to writing programmes in Word nor to using ActiveX-Components.

        Maybe, someone could give me a hint.

        Many thanks in advance!

        Greetings, Porley

    • #800684

      This is a known problem. See MSKB article String Too Long Using Over 255 Characters for a description and a workaround. The String(256, “W”) part in the code must be replaced by the text you want to write in the form field.

    • #801412

      There’s another workaround that works fine in Word 97, XP and 2003 (don’t have 2000):

      ActiveDocument.Bookmarks(“TxtTarget”).Range.Fields(1).Result.Text = “Here comes the text.”

      Sometimes you cannot select the formfield with ActiveDocument.FormFields(“TxtTarget”).Select.
      If the formfield is of Type wdFieldFormTextInput then this always does the trick:

      ActiveDocument.Bookmarks(“TxtTarget”).Range.Fields(1).Result.Select

      I found this workarounds once on the mvps site .

      To make these work nice, I’ve put them in “wrappers”

      Public Sub SetValueFF(vName As Variant, sValue As String)
      If ActiveDocument.Bookmarks.Exists(vName) Then
      ActiveDocument.Bookmarks(vName).Range.Fields(1).Result.Text = sValue
      End If
      End Sub

      Public Sub SelectFF(vName As Variant)
      With ActiveDocument
      If .Bookmarks.Exists(vName) Then
      If .FormFields(vName).Type = wdFieldFormTextInput Then
      .Bookmarks(vName).Range.Fields(1).Result.Select
      Else
      .FormFields(vName).Select
      End If
      End If
      End With
      End Sub

      Regards
      JanB Netherlands

    • #801413

      There’s another workaround that works fine in Word 97, XP and 2003 (don’t have 2000):

      ActiveDocument.Bookmarks(“TxtTarget”).Range.Fields(1).Result.Text = “Here comes the text.”

      Sometimes you cannot select the formfield with ActiveDocument.FormFields(“TxtTarget”).Select.
      If the formfield is of Type wdFieldFormTextInput then this always does the trick:

      ActiveDocument.Bookmarks(“TxtTarget”).Range.Fields(1).Result.Select

      I found this workarounds once on the mvps site .

      To make these work nice, I’ve put them in “wrappers”

      Public Sub SetValueFF(vName As Variant, sValue As String)
      If ActiveDocument.Bookmarks.Exists(vName) Then
      ActiveDocument.Bookmarks(vName).Range.Fields(1).Result.Text = sValue
      End If
      End Sub

      Public Sub SelectFF(vName As Variant)
      With ActiveDocument
      If .Bookmarks.Exists(vName) Then
      If .FormFields(vName).Type = wdFieldFormTextInput Then
      .Bookmarks(vName).Range.Fields(1).Result.Select
      Else
      .FormFields(vName).Select
      End If
      End If
      End With
      End Sub

      Regards
      JanB Netherlands

    Viewing 5 reply threads
    Reply To: Filling FormFields via Macro (Word 2000)

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

    Your information: