【问题标题】:How to get around the character limit on FormFields VBA passing from Access to Word如何绕过从 Access 传递到 Word 的 FormFields VBA 的字符限制
【发布时间】:2019-02-13 23:36:31
【问题描述】:

我正在尝试将 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

【问题讨论】:

  • txtReasonforReward 的数据类型是什么?如果是Short text,则将数据类型更改为Long text,并在其下将TextFormat更改为RichText
  • 嗨,Tarun,您是指在 Access 中吗?这会在不更改我的代码的情况下消除字符数问题吗?
  • 你能在Form中看到Reason for Reward的全文吗?
  • Short Text 最多可包含 255 个字符。您可以更改它,它不会影响您的代码。
  • Joe,使用表单域是绝对必要的吗?您是否希望用户随后在受保护的文档中编辑此信息?如果不是,请使用Bookmark 作为数据目标。表单字段名称也是一个书签,因此只需取消对文档的保护并写入书签...

标签: vba ms-access ms-word


【解决方案1】:

Word 有许多可以用作“数据目标”的可能对象,表单域就是其中之一。书签和内容控件是额外的(但不是唯一的)可能性。

在这种情况下,我建议将数据写入Bookmark,因为表单字段也是书签 - 不需要更改目标文档。这将避免 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

【讨论】:

  • 您好辛迪,感谢您的帮助。您能否建议如何在我现有的代码块中实现这一点?
  • @Joe 我在移动设备上,当我意识到我需要检查我的逻辑时......我在测试后编辑了代码。 cmets 应该解释它是如何工作的。将声明与其他声明放在一起,然后在写入表单字段后将其余部分放入。 (请记住删除当前导致错误的那个,它正在替换。)
  • 认为我在某个地方出错了...我已将前两行放在我的 With doc 部分上方,然后在我的 End with 之后的其余部分没有成功
  • End With 之前 - 在我写入表单字段后显示的位置;我从您的代码中复制/粘贴了最后两行以显示位置。如果它仍然“不工作”,您需要提供更多关于它如何它不工作的信息。
  • 道歉...没有注意到它们是最后两行...只是为了确认,您的前两行不属于 With?我在外面和他们一起工作......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-03
相关资源
最近更新 更多