PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED]string too long (formfields)-VBForums
Results 1 to 2 of 2

Thread: [RESOLVED]string too long (formfields)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Resolved [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:
    1. Sub ReplaceFormFields()
    2.  
    3. Dim FormDoc As String
    4. Dim FormPath As String
    5. Dim MainDoc As String
    6. Dim fField As FormField
    7. Dim i As Integer
    8.  
    9. 'the name of the main document
    10. MainDoc = ActiveDocument.Name
    11. 'the name of the document with the translations
    12. FormDoc = "forms_" & Left$(ActiveDocument.Name, Len(ActiveDocument.Name) - 3) & "doc"
    13. FormPath = ActiveDocument.Path & "\" & FormDoc
    14.  
    15. 'Check that the doc with translations exists
    16. If FExists(FormPath) = False Then
    17.     MsgBox ("There is no forms file associated with this document")
    18.     Exit Sub
    19. End If
    20.  
    21. 'open the doc with translations
    22. Documents.Open FileName:=FormPath, ConfirmConversions:=False, _
    23.         ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
    24.         PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
    25.         WritePasswordTemplate:="", Format:=wdOpenFormatAuto
    26.        
    27. Windows(MainDoc).Activate
    28.  
    29. i = 0
    30. 'copy each string in turn from the translated doc to the formfields in the original document
    31. For Each fField In ActiveDocument.FormFields
    32.     i = i + 1
    33.     If fField.Type = wdFieldFormDropDown Then
    34.         'GetDropDownFormValues i, fField, MainDoc, FormDoc
    35.     'copy method one
    36.     'Else: fField.Range.Text = GetFormValue(i, fField, MainDoc, FormDoc, fField.Range.Font)
    37.     ' copy method two
    38.     Else: fField.Result = GetFormValue(i, fField, MainDoc, FormDoc)
    39.     End If
    40. Next fField
    41.  
    42. End Sub

    The above contains two different calls to GetFormValue, the old one with a Font parameter and the new one without.

    VB Code:
    1. Private Function GetFormValue(ByRef index As Integer, ByRef field As FormField, _
    2.                                main As String, fdoc As String) As String
    3.  
    4. Dim OpenTag As String
    5. Dim CloseTag As String
    6. Dim SearchText As String
    7. Windows(fdoc).Activate
    8.  
    9. 'describe the opening and closing tags and define the text to search for
    10. OpenTag = "<Field: " & index & ">"
    11. CloseTag = "</Field: " & index & ">"
    12. SearchText = "\<Field: " & index & "\>*\</Field: " & index & "\>"
    13.  
    14. 'find the string corresponding to the field to be filled in
    15. Selection.Find.ClearFormatting
    16. With Selection.Find
    17.     .Text = SearchText
    18.     .Forward = True
    19.     .Wrap = wdFindContinue
    20.     .Format = True
    21.     .MatchCase = False
    22.     .MatchWholeWord = False
    23.     .MatchAllWordForms = False
    24.     .MatchSoundsLike = False
    25.     .MatchWildcards = True
    26. End With
    27. Selection.Find.Execute
    28.  
    29. 'Shrink selection to exclude tags
    30. Selection.Start = Selection.Start + Len(OpenTag)
    31. Selection.End = Selection.End - Len(CloseTag)
    32.  
    33. 'When setting text directly, original font formatting is lost
    34. 'Selection.Range.Font.Name = fFont.Name
    35.  
    36. 'Selection.Range.Font.Size = fFont.Size
    37.  
    38. GetFormValue = Selection.Text
    39.  
    40. 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?
    Last edited by pickarooney; Jul 8th, 2005 at 04:40 AM. Reason: resolved

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: string too long (formfields)

    OK, I found the workaround with a bit of digging on google. Answer here.
    Last edited by pickarooney; Jul 8th, 2005 at 04:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width