如何绕过从Access到Word传递的FormFields VBA的字符限制 [英] How to get around the character limit on FormFields VBA passing from Access to Word

查看:144
本文介绍了如何绕过从Access到Word传递的FormFields VBA的字符限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Access表单中字段的内容传递给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.

我已经看到了一些有关如何避免这种情况的示例,但是我不确定它们如何在我的基本代码中工作.目前,我对VBA的理解有些不足,因此请您提出任何清晰的防白痴建议.

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有许多可能的对象可用作数据目标",其中表单字段是其中之一.书签和内容控件是附加的(但不是唯一的)可能性.

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.

在这种情况下,由于表单字段也是书签,因此我建议将数据写入Bookmark-无需更改目标文档.这将避免255个字符的限制,这是由于表单字段而不是Word引起的.

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

这篇关于如何绕过从Access到Word传递的FormFields VBA的字符限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆