我正在尝试将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有许多可用作“数据目标”的对象,其中表单字段为1。书签和内容控件是附加(但不是唯一)的可能性。
在这种情况下,我建议将数据写入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