Form Fields in MS Word have 255 character limit

So with my school database that generates a word document for Final Report for students, it hit a wall with narrative/comments from teachers that's over 255 characters long. Instead of complaining long winded commentaries, I just had to fix this.

The best solution page, that is thorough enough, I am pasting its content here, lest it's taken down: (I basically just used ActiveDocument.Bookmarks("Bookmark1").Range.Text Instead of .FormFields("Bookmark1").Result.

Lynn,

I think if you have a better understanding of what's happening in Word
you'll be better able to come up with an acceptable solution.

First, you are correct that the character limit for formfields is 255. In
addition, you can set the .Result property of a formfield without removing
the protection from the document using something like:

ActiveDocument.FormFields("Text1").Result = "This is a string of less than
255 characters."

This is the basic nature of forms protection: the only place you can add
content to the forms-protected sections of the document is the formfields
within the section.

However, as you have found, if the content to be added exceeds the 255
character limit, setting the .Result property of the formfield doesn't work.
In this instance, you have a few options available to you. You can:

* Use the Trim function to truncate the string at 255 characters. This is
probably not an acceptable solution since you undoubtedly want the whole
string to appear in your document.

* Use multiple formfields and the Mid function to parse the string into
255 character "chunks", which then get inserted into various formfields using
the .Result property as described above. Again, this is a less-than-optimum
solution because it's clunky and may not produce an especially attractive
result.

* Use a bookmark and something like the following to insert the text:

ActiveDocument.Bookmarks("Bookmark1").Range.Text = "A long string of more
than 255 characters."

However, as you have discovered, this method requires the section containing
the bookmark to be unprotected (for the reasons discussed above), and you
have stated that making this section unprotected is not an option.

The best solution is to unprotect the document prior to inserting the text
and then reprotecting it again afterwards. The code for doing this would look
something like:

ActiveDocument.Unprotect "password"
ActiveDocument.Bookmarks("Bookmark1").Range.Text = "A long string of more
than 255 characters."
ActiveDocument.Protect wdAllowOnlyFormFields, True, "password"

(See the Word VBA help topics on document protection for more information on
this code.)

Only one thing concerns me: You say that you are "pushing" data from Excel
into Word. This makes me think that you are driving this process from the
Excel side rather than from Word. I'm not quite sure if you will be
successful in trying to unprotect a Word document using code from within
Excel. This being the Word VBA forum, I suspect that most contributors would
take the approach of "pulling" data from Excel using code in Word - and thus
would not have to worry about this potential problem. However, some of us do
"cross over" so maybe someone else will be able to help you with this
possible sticking point. Otherwise, I can only suggest that you ask the
question in the Excel VBA forum.
--
Cheers!
Gordon

This entry was posted in Technical. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.