[RESOLVED]string too long (formfields)
I'm writing a VBA macro to deal with formfields in Word Documents.
I need to extract the text from them, translate that and replace the text in the document.
The first part is fine, I can extract everything and place it in a a tag-delimited file and then translate it.
The next part is giving me a problem, though. When I try and copy the translated text back into the original document containing formfields, one of two things happens:
1) - the grey formfield property is removed
2) - I get a 'string too long' error message
Let me explain the code a little.
When I start to run the macro, I have two files; one original word document containing formfields and one other word document containing translated versions of the strings in these formfields.
VB Code:
Sub ReplaceFormFields()
Dim FormDoc As String
Dim FormPath As String
Dim MainDoc As String
Dim fField As FormField
Dim i As Integer
'the name of the main document
MainDoc = ActiveDocument.Name
'the name of the document with the translations
FormDoc = "forms_" & Left$(ActiveDocument.Name, Len(ActiveDocument.Name) - 3) & "doc"
FormPath = ActiveDocument.Path & "\" & FormDoc
'Check that the doc with translations exists
If FExists(FormPath) = False Then
MsgBox ("There is no forms file associated with this document")
Exit Sub
End If
'open the doc with translations
Documents.Open FileName:=FormPath, ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
Windows(MainDoc).Activate
i = 0
'copy each string in turn from the translated doc to the formfields in the original document
For Each fField In ActiveDocument.FormFields
i = i + 1
If fField.Type = wdFieldFormDropDown Then
'GetDropDownFormValues i, fField, MainDoc, FormDoc
'copy method one
'Else: fField.Range.Text = GetFormValue(i, fField, MainDoc, FormDoc, fField.Range.Font)
' copy method two
Else: fField.Result = GetFormValue(i, fField, MainDoc, FormDoc)
End If
Next fField
End Sub
The above contains two different calls to GetFormValue, the old one with a Font parameter and the new one without.
VB Code:
Private Function GetFormValue(ByRef index As Integer, ByRef field As FormField, _
main As String, fdoc As String) As String
Dim OpenTag As String
Dim CloseTag As String
Dim SearchText As String
Windows(fdoc).Activate
'describe the opening and closing tags and define the text to search for
OpenTag = "<Field: " & index & ">"
CloseTag = "</Field: " & index & ">"
SearchText = "\<Field: " & index & "\>*\</Field: " & index & "\>"
'find the string corresponding to the field to be filled in
Selection.Find.ClearFormatting
With Selection.Find
.Text = SearchText
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
'Shrink selection to exclude tags
Selection.Start = Selection.Start + Len(OpenTag)
Selection.End = Selection.End - Len(CloseTag)
'When setting text directly, original font formatting is lost
'Selection.Range.Font.Name = fFont.Name
'Selection.Range.Font.Size = fFont.Size
GetFormValue = Selection.Text
End Function
problem 1) arises if I assign a value directly to fField.Range.Text. The formfield is overwritten by plain text.
problem 2) arises if I assign the translated string to the result property of the formfield. It will accept strings up to a certain length (255 or so characters I think) and in that case the formfield itself is preserved, but if the string in question is longer than that the macro crashes.
If this makes any sense to anyone, can you help me figure out where the string too long error is coming from and how I might avoid it?
Re: string too long (formfields)
OK, I found the workaround with a bit of digging on google. Answer here.