Josh045
Josh045

Reputation: 25

(Vba) Ms Word: Work around the 255 characters limit

I'm new to programming and I'm trying to copy the content of a form field to another form field in the same Word document like this:

Sub Copyfield()
Dim Temp As String
Temp = ActiveDocument.FormFields("Field1").Result
ActiveDocument.FormFields("Field2").Result = Temp
End Sub

My problem is that my "Field1" is a piece of text of more than 255 characters which seems to be a problem with "Result". I know there is a very similar topic here: Passing MS-Access string >255 characters to MS-Word field but I still don't have the 50 reputation to comment on that thread.

Could anyone please help me understand how to implement the changes in my code?

Upvotes: 0

Views: 2447

Answers (2)

Josh045
Josh045

Reputation: 25

Ok, after 3 days at the border of madness, finally thanks to the help of @Cindy Meister (and some serious personal digging), I made it work. Maybe it's not a big deal for you geniouses out there but believe me for me it was like seeing everything in Matrix code (from the movie guys, the movie).

I want to post it and share because I tried to find it in every corner of our Internet and part of the extraterrestrial one and I couldn't. So hopefully it will be useful for another programming illiterate / dumb person (as myself).

Here is the code:

Sub CopyField()

  Dim ffld As Word.FormField
  Dim doc As Word.Document
  Dim rng As String

  Dim s1 As String, s2 As String

  Set doc = ActiveDocument

  'Get the long text
  rng = ActiveDocument.FormFields("Field1").Result

  'Split off a bit to go into FormField.Result
  s1 = Left(rng, 4) 'Keeps the first 4 characters of the rng string starting from left to right this can be adapted
  'The rest of the long text, to be assigned to Selection.Text
  s2 = Mid(rng, 5) 'Starting from the 5th character from the left keeps the rest of the string

  Set ffld = doc.FormFields("Field2")
  ffld.Result = s1
  ffld.Select
  Selection.MoveRight wdCharacter, 1

  ActiveDocument.Unprotect 'Unprotects the document!
  Selection.Text = s2
  ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True 'Protects the document again!

  ActiveDocument.Bookmarks("Field1").Select ' "Sends cursor" back to Field1

End Sub

Big part of the code is originally by @Cindy Meister... I just adapted it to my situation where I had 2 form fields instead of paragraphs. I also had to add some lines to unprotect the document at a certain point in order to make it work (ask the Pros for the reason) and a final instruction to come back to "Field1" (which is some pages up) after the process. Finally just a note for my dumb fellows: I added the macro "on exit" on the "Field1" properties to automatize the process.

Huge thanks Cindy again and I hope you help in my dark programming moments again ! (please do)

:)

Upvotes: 1

Cindy Meister
Cindy Meister

Reputation: 25663

Well, here's one possibility. Since I don't have your environment it was easier for me to test text in the document rather than another form field with so much content. You'll need to adjust the code accordingly.

The key is to get the Selection "inside" the form field so that it doesn't hit the "protection barrier". Just using FormField.Select puts the focus at the beginning of the field, which VBA is seeing as "protected". Moving one character to the right corrects that and long text can then be assigned to the Selection. But the field needs to have content.

So what my code is doing is "slicing off" the first word of the text to go into the form field. That's short enough to assign to the Result property and lets the Selection move to its right. Then the rest - the long text - can be assigned to the Selection.

You'll probably want to assign the entire FormField.Result to a string variable, then manipulate that string.

Sub WriteLongTextToFormField()
  Dim ffld As word.FormField
  Dim doc As word.Document
  Dim rng As word.Range
  Dim s1 As String, s2 As String

  Set doc = ActiveDocument
  'Get the long text
  Set rng = doc.Range(doc.Paragraphs(1).Range.Start, doc.Paragraphs(6).Range.End)
  'Split off a bit to go into FormField.Result
  s1 = rng.Words(1)
  rng.MoveStart wdWord, 1
  'The rest of the long text, to be assigned to Selection.Text
  s2 = rng.Text

  Set ffld = doc.FormFields("Text1")
  ffld.result = s1
  ffld.Select
  Selection.MoveRight wdCharacter, 1
  Selection.Text = s2
End Sub

Upvotes: 1

Related Questions