[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Run Time Error 4609 - String too long

Posted on 2010-01-04
14
Medium Priority
?
1,068 Views
Last Modified: 2013-11-25
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").CheckBox.Value = True And ActiveDocument.FormFields("Medium").CheckBox.Value = True) Then
ActiveDocument.FormFields("Description").Result = "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
0
Comment
Question by:sspatel80
  • 9
  • 4
14 Comments
 
LVL 35

Expert Comment

by:torimar
ID: 26177583
Check the properties of the form field "Description" (Right-click > Properties): is the 'Type' set to 'Regular Text' and the 'Maximum Length' to 'Unlimited'?
0
 
LVL 35

Expert Comment

by:torimar
ID: 26177610
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.
0
 

Author Comment

by:sspatel80
ID: 26177612
Type is set to Regular Text and Max length is set to Unlimited
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 35

Expert Comment

by:torimar
ID: 26177676
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.

0
 
LVL 22

Assisted Solution

by:Flyster
Flyster earned 400 total points
ID: 26177706
Here's another work-around you can try:

http://word.mvps.org/FAQs/MacrosVBA/SetLongFmFldResult.htm

Flyster
0
 
LVL 35

Expert Comment

by:torimar
ID: 26177708
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.
0
 

Author Comment

by:sspatel80
ID: 26177710
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.
0
 
LVL 35

Expert Comment

by:torimar
ID: 26177734
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.
0
 

Author Comment

by:sspatel80
ID: 26177760
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("Description").Result = "****"
      If ActiveDocument.ProtectionType <> 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
0
 
LVL 35

Expert Comment

by:torimar
ID: 26177847
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:

Sub .... ()
   Dim MyText as String
   MyText = "Insert your string here"

   ....
   Selection.TypeText (MyText)
   .....
End Sub

Open in new window

0
 
LVL 35

Expert Comment

by:torimar
ID: 26177885
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

Open in new window

0
 
LVL 35

Accepted Solution

by:
torimar earned 1600 total points
ID: 26177913
Corrected version:
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 = MyText
       ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
   End If
End Sub

Open in new window

0
 

Author Comment

by:sspatel80
ID: 26186549
Thank you both.  Torimar, your code worked really well.  Thanks again!
0
 
LVL 35

Expert Comment

by:torimar
ID: 26186657
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:
http://www.experts-exchange.com/help.jsp#hs=29&hi=407
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question