Results 1 to 10 of 10
  1. #1
    2 Star Lounger
    Join Date
    Mar 2002
    Location
    Germany, Germany
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Filling FormFields via Macro (Word 2000)

    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:
    <font face="Georgia"> <font color=blue>ActiveDocument.FormFields("TxtTarget"). Result = "Here comes the text."</font color=blue></font face=georgia>
    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

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Filling FormFields via Macro (Word 2000)

    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
    [MS MVP - Word]

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Filling FormFields via Macro (Word 2000)

    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.

  4. #4
    2 Star Lounger
    Join Date
    Mar 2002
    Location
    Germany, Germany
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Filling FormFields via Macro (Word 2000)

    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 <font color=blue>wdGoToBookmark</font color=blue>. 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

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Filling FormFields via Macro (Word 2000)

    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 <!profile=WendellB>WendellB<!/profile>'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.

  6. #6
    2 Star Lounger
    Join Date
    Mar 2002
    Location
    Germany, Germany
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Filling FormFields via Macro (Word 2000)

    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

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Filling FormFields via Macro (Word 2000)

    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.

  8. #8
    2 Star Lounger
    Join Date
    Mar 2002
    Location
    Germany, Germany
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Filling FormFields via Macro (Word 2000)

    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:

    <font face="Georgia">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</font face=georgia>

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

    Best regards,

    Porley

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Filling FormFields via Macro (Word 2000)

    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.

  10. #10
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Filling FormFields via Macro (Word 2000)

    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).Re sult.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 <img src=/S/flags/Netherlands.gif border=0 alt=Netherlands width=30 height=18>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •