sspatel80
asked on
Run Time Error 4609 - String too long
I have written a macro where selecting a couple of checkboxes should automatically bring up text in a form field called "Description" but I keep getting an error message saying 'string too long'. What do I do to allow a long string and also what code would I need to word wrap the text in the form field?
Thank you!
Sub Medium()
If (ActiveDocument.FormFields ("Rare").C heckBox.Va lue = True And ActiveDocument.FormFields( "Medium"). CheckBox.V alue = True) Then
ActiveDocument.FormFields( "Descripti on").Resul t = "3 = Moderate risk - Department Manager attention required. The Occupational Health and Safety Coordinator must be notified within 24 hours. Relevant controls are to be reviewed and implemented within a week. Where appropriate, visual controls should be put in place to prevent any incidents from occurring."
End If
End Sub
Thank you!
Sub Medium()
If (ActiveDocument.FormFields
ActiveDocument.FormFields(
End If
End Sub
Check the properties of the form field "Description" (Right-click > Properties): is the 'Type' set to 'Regular Text' and the 'Maximum Length' to 'Unlimited'?
As to word wrapping:
Are your form elements placed inside a table? Then make sure the height of the row containing the text field is not set to a fixed value.
Are your form elements placed inside a table? Then make sure the height of the row containing the text field is not set to a fixed value.
ASKER
Type is set to Regular Text and Max length is set to Unlimited
Check out this KB article: http://support.microsoft.com/kb/163192
(You receive a "Run-time error '4609': "String too long" error message when you assign a value to a FormField object in Word)
It appears that even in most recent versions of Word the limit of a VBA string variable passed to a text field is 256 characters (=1 byte).
That is pretty ridiculous, but it is even more so that Microsoft only state this pre-historic limit but give no workaround for it whatsoever.
(You receive a "Run-time error '4609': "String too long" error message when you assign a value to a FormField object in Word)
It appears that even in most recent versions of Word the limit of a VBA string variable passed to a text field is 256 characters (=1 byte).
That is pretty ridiculous, but it is even more so that Microsoft only state this pre-historic limit but give no workaround for it whatsoever.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Lol, sorry, there is indeed a workaround demonstrated in that article.
My bad, I was reading too fast and thought it was just another proof-of-concept example.
My bad, I was reading too fast and thought it was just another proof-of-concept example.
ASKER
I did try to use the code earlier but it didn't work for me, will try again-probably doesn't help that I'm new to VB, I'm obviously missing something.
I will try it again. Thanks for your time.
I will try it again. Thanks for your time.
I must say that the "workaround" given by MS is about the most incomprehensible piece of "explanation" I have ever read in my life.
No wonder if it didn't work for you on first attempt.
Try to cross-check it with the link posted by Flyster.
And if you still get errors, post the code you have so far, and we may be able to help.
No wonder if it didn't work for you on first attempt.
Try to cross-check it with the link posted by Flyster.
And if you still get errors, post the code you have so far, and we may be able to help.
ASKER
Well this is the code that I have used butI I still get a message saying string too long.
As you can see I haven't changed much in the code except replacing Text 1 with Description. Also why is there a 'W' in the line :Selection.TypeText (String(256, "W")) when I run the code lots of W's appear in the Description form field.
Any help would be greatly appreciated! Thanks.
Sub WorkAround255Limit()
' Set Text1 form field to a unique string.
ActiveDocument.FormFields( "Descripti on").Resul t = "****"
If ActiveDocument.ProtectionT ype <> wdNoProtection Then
ActiveDocument.Unprotect
End If
Selection.GoTo what:=wdGoToBookmark, Name:="Description"
Selection.Collapse
Selection.MoveRight wdCharacter, 1
Selection.TypeText (String(256, "W"))
Selection.GoTo what:=wdGoToBookmark, Name:="Description"
' 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
As you can see I haven't changed much in the code except replacing Text 1 with Description. Also why is there a 'W' in the line :Selection.TypeText (String(256, "W")) when I run the code lots of W's appear in the Description form field.
Any help would be greatly appreciated! Thanks.
Sub WorkAround255Limit()
' Set Text1 form field to a unique string.
ActiveDocument.FormFields(
If ActiveDocument.ProtectionT
ActiveDocument.Unprotect
End If
Selection.GoTo what:=wdGoToBookmark, Name:="Description"
Selection.Collapse
Selection.MoveRight wdCharacter, 1
Selection.TypeText (String(256, "W"))
Selection.GoTo what:=wdGoToBookmark, Name:="Description"
' 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
The "String(256, "W")" command creates an example string filled with 256 W's.
You must replace it by your own string. Best would be to declare and fill that string before, so you don't mess up your code formatting.
Try:
You must replace it by your own string. Best would be to declare and fill that string before, so you don't mess up your code formatting.
Try:
Sub .... ()
Dim MyText as String
MyText = "Insert your string here"
....
Selection.TypeText (MyText)
.....
End Sub
I have no opportunity to test this right now so you will have to check whether this code works:
Sub Medium()
Dim MyText as String
MyText = "3 = Moderate risk - Department Manager attention required. The Occupational Health and Safety Coordinator must be notified within 24 hours. Relevant controls are to be reviewed and implemented within a week. Where appropriate, visual controls should be put in place to prevent any incidents from occurring."
If (ActiveDocument.FormFields("Rare").CheckBox.Value = True And ActiveDocument.FormFields("Medium").CheckBox.Value = True) Then
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("Description").Range.Fields(1).Result.Text = WyText
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both. Torimar, your code worked really well. Thanks again!
That's good to know.
So if the problem is solved, please don't forget to finalize the question. Should you not know how that is done, please refer to this Help page:
https://www.experts-exchange.com/help.jsp#hs=29&hi=407
So if the problem is solved, please don't forget to finalize the question. Should you not know how that is done, please refer to this Help page:
https://www.experts-exchange.com/help.jsp#hs=29&hi=407