Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何绕过FormFields VBA从Access传递到Word的字符限制_Vba_Ms Access_Ms Word - Fatal编程技术网

如何绕过FormFields VBA从Access传递到Word的字符限制

如何绕过FormFields VBA从Access传递到Word的字符限制,vba,ms-access,ms-word,Vba,Ms Access,Ms Word,我正试图将Access表单中字段的内容传递到Word文档,该文档使用下面的代码,除了其中一个字段的一个小问题外,完全满足我的需要 .FormFields("txtReasonforReward").Result = Me![Reason for Reward] 给了我一些问题,因为我达到了角色的极限 我已经看到了几个关于如何避免这种情况的例子,但我不确定它们在我的基本代码中是如何工作的。我觉得目前对VBA的理解有点不够,所以任何明确的防白痴建议都将不胜感激 有人能告诉我怎么做吗 Dim obj

我正试图将Access表单中字段的内容传递到Word文档,该文档使用下面的代码,除了其中一个字段的一个小问题外,完全满足我的需要

.FormFields("txtReasonforReward").Result = Me![Reason for Reward]
给了我一些问题,因为我达到了角色的极限

我已经看到了几个关于如何避免这种情况的例子,但我不确定它们在我的基本代码中是如何工作的。我觉得目前对VBA的理解有点不够,所以任何明确的防白痴建议都将不胜感激

有人能告诉我怎么做吗

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

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

若要在受表单保护的文档(看起来是这样)中写入书签,必须删除表单保护。这可以通过写入数据来恢复。这样做可能是个好主意,否则表单字段可能会重置,这将丢失写入它们的数据。或者全面应用书签技术,而不是只应用于一个数据目标

'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
“将对象添加到声明中
作为单词范围的Dim rng
将x作为字符串
“做事。。。
.FormFields(“txtHRID”).Result=Me![ID]
.FormFields(“txtPeriod”).Result=Me![期间]
'写入表单字段后,添加长字符串数据
如果doc.ProtectionType wdNoProtection,则
解除保护
如果结束
'根据表单字段的名称获取其范围
设置rng=doc.Bookmarks(“txtReasonforReward”).范围
'将起始点向后移动一个字符,以便可以删除表单字段。
rng.MoveStart wdCharacter,-1
'请确保保存此字符,因为删除操作将删除它
x=rng.Characters.First
rng.FormFields(1).删除
'指定值
rng.Text=x&Me![奖励理由]
'重新恢复文档保护
文档保护wdAllowOnlyFormFields,正确

奖励的数据类型是什么?如果是
短文本
,则将数据类型更改为
长文本
,并将其下的TextFormat更改为
RichText
。您好,Tarun,您是指在Access内吗?这是否会在不更改我的代码的情况下消除字符计数问题?您是否能够在
奖励原因
的表单中看到全文?
短文本
最多限制为255个字符。您可以更改它,它不会影响您的代码。Joe,使用表单字段是绝对必要的吗?您是否希望用户随后在受保护的文档中编辑此信息?如果不是,请使用
书签
作为数据目标。表单字段名也是一个书签,所以需要做的就是取消文档保护并写入书签…嗨,辛迪,谢谢你的帮助。你能告诉我如何在我现有的代码块中实现这个吗?@Joe我在一个移动设备上,当我意识到我需要检查我的逻辑时。。。我在测试后编辑了代码。评论应该解释它是如何工作的。将声明与其他声明放在一起,然后在写入表单字段后将其余声明放入。(记住删除当前导致错误的一个,这是正在替换的。)认为我在某个地方出错了。。。我已将前两行放在我的With doc部分上方,然后在我的End With to NOT SUCCESS之后的剩余行放在
End With
-在写入表单字段后显示的位置;为了显示位置,我复制/粘贴了代码的最后两行。如果它仍然“不起作用”,你需要提供更多关于它如何不起作用的信息。道歉。。。没有注意到他们是最后两行。。。只是确认一下,您的前两行不在With范围内?我在外面和他们一起工作。。。