When you try to assign the value of a variable containing more than 255 characters to a FormField object, you receive the following error message:
Run-time error '4609': "String too long"
Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.
Sub StringTooLongError()
Dim sMyVar As String
sMyVar = String(256, "x")
ActiveDocument.FormFields(1).Result = sMyVar
End Sub
Sub WorkAround255Limit()
' Set Text1 form field to a unique string.
ActiveDocument.FormFields("text1").Result = "****"
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
Selection.GoTo What:=wdGoToBookmark, Name:="Text1"
Selection.Collapse
Selection.MoveRight wdCharacter, 1
Selection.TypeText (String(256, "W"))
Selection.GoTo what:=wdGoToBookmark, Name:="Text1"
' Remove unique characters from Text1 form field.
With Selection.Find
.Execute findtext:="*", replacewith:="", replace:=wdReplaceAll
End With
ActiveDocument.Protect Password:="", NoReset:=True, Type:= _
wdAllowOnlyFormFields
End Sub
Keywords: wordcon, offcon, kbdtacode, kbprb, kboffice2003yes, kbsweptsoltax, kb, kbmacro, kbcode, kboffice12yes, kbfreshness2006, kbprogramming