简体   繁体   中英

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

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 one of the fields.

.FormFields("txtReasonforReward").Result = Me![Reason for Reward]

is giving me some issues as I am hitting the character limit.

I've seen several examples on how to circumvent this, but I'm not sure how they work within my base code. I'm feeling a little inadequate in my understanding of VBA at the moment so any clear idiot-proof advice would be appreciated.

Please could someone give me a heads up on how to proceed.

Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set doc = objWord.Documents.Open(***path to file***, , True)
With doc
.FormFields("txtFirstName").Result = Me![First Name]
.FormFields("txtLastName").Result = Me![Last Name]
.FormFields("txtReasonforReward").Result = Me![Reason for Reward]
.FormFields("txtCompanyValue").Result = Me![Company Value]
.FormFields("txtRequestingManager").Result = Me![Requesting Manager]
.FormFields("txtLocation").Result = Me![Location]
.FormFields("txtJobTitle").Result = Me![Job Title]
.FormFields("txtReqMgrJobTitle").Result = Me![Requesting Manager Job Title]
.FormFields("txtMonetaryValue").Result = Me![MoneyCalculated]
.FormFields("txtDesc").Result = Me![Description]
.FormFields("txtPayroll").Result = Me![Payroll Number]
.FormFields("txtGrade").Result = Me![Grade]
.FormFields("txtLocation2").Result = Me![Location]
.FormFields("txtRequestingMgr").Result = Me![Requesting Manager]
.FormFields("txtLevelofAction").Result = Me![ValueofPayment]
.FormFields("txtGemNom").Result = Me![GemNomination]
.FormFields("txtHRID").Result = Me![ID]
.FormFields("txtPeriod").Result = Me![Period]

.Visible = True
.Activate

End With

objWord.View.ReadingLayout = True

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM