How to get around the character limit on FormFields VBA passing from Access to Word

前端 未结 1 1250
礼貌的吻别
礼貌的吻别 2021-01-28 23:26

I\'m attempting to pass the content of fields in an Access form to a Word document, which, using the below code, does exactly what I need it to, except for one small issue with

相关标签:
1条回答
  • 2021-01-28 23:44

    Word has a number of possible objects that can be used as "data targets", of which form fields is one. Bookmarks and content controls are additional (but not the only) possibilities.

    In this case, I would suggest writing the data to a Bookmark, since a form field is also a bookmark - the target document wouldn't need to be changed. This will avoid the 255 character limit, which is due to the form field, not to Word.

    In order to write to a bookmark in a document protected as a form (which this appears to be) it is necessary to remove forms protection. This can be re-instated writing the data. It's probably a good idea to do so, as otherwise the form fields could reset, which would lose the data written to them. That, or apply the bookmark technique across the board rather than to only one data target.

    'Add objects to the declarations
    Dim rng As Word.Range
    Dim x As String
    
    'Do things...
    
    .FormFields("txtHRID").Result = Me![ID]
    .FormFields("txtPeriod").Result = Me![Period]
    
    'After writing to the form fields, add the long string of data
    If doc.ProtectionType <> wdNoProtection Then
       doc.Unprotect
    End If
    'Get the range of the form field, based on its name
    Set rng = doc.Bookmarks("txtReasonforReward").Range
    'Move the starting point back one character so that the form field can be deleted. 
    rng.MoveStart wdCharacter, -1
    'Be sure to save this character as the deletion will remove it
    x = rng.Characters.First
    rng.FormFields(1).Delete
    'Assign the value
    rng.Text = x & Me![Reason for Reward]
    'Re-instate document protection
    doc.Protect wdAllowOnlyFormFields, True
    
    0 讨论(0)
提交回复
热议问题