【问题标题】:Word Fill VBA in MS Access for various fields in tableMS Access中的Word Fill VBA用于表中的各个字段
【发布时间】:2020-04-25 07:45:10
【问题描述】:

对于我们数据库的事件管理方面,我正在尝试在 149 调查报告中生成表格中字段的数据,该报告是由州 (see link here) 提供的 Word 文档模板。

我制作了一个只读版本的文档,以通过强制用户另存为来保持其完整性,并使用带有书签的文本表单字段加载它以供参考(例如:txtcaseintroduction)。

我修改了在互联网上找到的用于处理表单字段的代码,并将其分配给我的一个表单上的一个按钮以帮助生成报告(出于安全原因,修改了 Open 引用):

Private Sub cmdPrint_Click()

'Export 149 Report.

Dim appWord As Word.Application

Dim doc As Word.Document

'Avoid error 429, when Word isn't open.

On Error Resume Next

Err.Clear

'Set appWord object variable to running instance of Word.

Set appWord = GetObject(, "Word.Application")

If Err.Number <> 0 Then

'If Word isn't open, create a new instance of Word.

Set appWord = New Word.Application

End If

Set doc = appWord.Documents.Add("Y:\ABC\2018\Case Files\2018 - Incident Forms\OPWDD 149 - Access Database Reference.docx", , True)

With doc
    .FormFields("txtNIMRS").Result = Me.NIMRSID
    .FormFields("txtInternalID").Result = Me.InternalIncidentID
    .FormFields("txtIncidentDate").Result = Me.[IncidentOccurrenceDate]
    .FormFields("txtDiscoverydate").Result = Me.[IncidentReportDate]
    .FormFields("txtCaseIntroduction").Result = Me.CaseIntroduction
    .FormFields("txtIncidentLocation").Result = Me.Location
    .FormFields("txtBackground").Result = Me.BackgroundInfo
    .FormFields("txtProtections").Result = Me.ImmedProtec
    .FormFields("txtQuestion").Result = Me.InvestQuestion
    .FormFields("txtTestName").Result = Me.[TestimonialEvidence]
    .FormFields("txtDocumentaryE").Result = Me.[DocumentaryEvidence]
    .FormFields("txtDemonstrativeE").Result = Me.[DemonstrativeEvidence]
    .FormFields("txtPhysicalE").Result = Me.[PhysicalEvidence]
    .FormFields("txtWSName").Result = Me.[WrittenStatements]
    .FormFields("txtSummary").Result = Me.SummaryEvidence
    .FormFields("txtConclusions").Result = Me.Text409
    .FormFields("txtRecommendations").Result = Me.Text411
    .FormFields("txtInvestigator").Result = Me.Investigator_s__Assigned
    .FormFields("txtdatereport").Result = Me.Investigative_Report_Completion_Date
.Visible = True

.Activate

End With

Set doc = Nothing

Set appWord = Nothing

Exit Sub

errHandler:

MsgBox Err.Number & ": " & Err.Description

End Sub

以下字段有效:

 .FormFields("txtNIMRS").Result = Me.NIMRSID
        .FormFields("txtInternalID").Result = Me.InternalIncidentID
        .FormFields("txtIncidentDate").Result = Me.[IncidentOccurrenceDate]
        .FormFields("txtDiscoverydate").Result = Me.[IncidentReportDate]
.FormFields("txtIncidentLocation").Result = Me.Location
        .FormFields("txtBackground").Result = Me.BackgroundInfo
        .FormFields("txtProtections").Result = Me.ImmedProtec
        .FormFields("txtQuestion").Result = Me.InvestQuestion
 .FormFields("txtConclusions").Result = Me.Text409
        .FormFields("txtRecommendations").Result = Me.Text411
.FormFields("txtdatereport").Result = Me.Investigative_Report_Completion_Date

其余字段(case introductioninvestigator 和附件字段)没有。所有这些字段都存在于同一张表中。还注意到案例介绍曾经有效,但由于我试图找出更多表单字段以应用于文档和参考,因此停止了工作。目标是让调查人员基本上在数据库中完成所有工作,然后将其导出为所需格式以提交给州。

我的问题:我需要对上述代码执行什么操作才能使非工作字段在填充 Word 文档时发挥作用?

在 cmets 中回答问题

  • 没有发生错误;当我按下按钮时,文本框根本没有填充。

  • 表单字段不需要出现在结果文档中。它们只是数据的“目标”。

【问题讨论】:

  • 首先,该文档应该被用作模板,而不是被设置为“只读”的文档。使用appWord.Documents.Add("PathToOriginalFile") - 这会创建一个原始文档的副本作为新文档,因此它不可能被更改。
  • 正确 - 您评论末尾的问题。您没有看到任何错误的原因是On Error Resume Next 禁用了错误通知。在GetObject后面加上On Error GoTo 0,然后再测试。
  • 所以我将文档保存为 .doctm 文件以允许模板启用宏并更改 VBA 中的文件源以反映更改,但 Access 仍然无法找到它。我把它放回 .docx 只读版本,它能够找到该文件。不确定那里发生了什么,它不会找到新文件。我还对您在上一条关于错误的评论中建议的更改进行了更改;在没有抑制错误代码的情况下,VBA 返回 CaseIntroduction 字符串“太长”。我应该缩短表单字段文本框的书签名称吗?
  • 可以用作书签名称的字符数是有限制的,尽管我预计 Word 应用程序不会接受它,如果这是问题所在。更有可能是代码试图写入表单字段的数据量。如果你通过缩短来测试它会起作用吗?
  • 检查我的编辑 - 这样做是为了让未来可能遇到相同问题的访问者更容易理解/有用 - 以确保我没有犯意外错误。另外,如果您有时间,您可以编辑使用Documents.Add 的代码版本吗?这样一来,可能复制此代码的其他人就可以采用“更好”的方法来强制用户使用文档的副本。 (请不要合并我的答案 - Stack Overflow 不是那样工作的。)完成后,cmets 可以进一步精简,提高贡献的有用性:-)

标签: vba ms-access ms-word form-fields


【解决方案1】:

由于不需要在结果文档中保留表单域,最简单的方法是将数据简单地插入到FormField.Range,这将替换(删除)表单域。如果一致性很重要(最终结果在用户看来如何),则可以以这种方式编写整个代码,但从 编程 的角度来看则不必如此。

注意:如果激活了表单保护,则需要将其关闭才能使用此方法

If doc.ProtectionType <> -1 Then doc.Unprotect  '-1 = wdNoProtection

超过 255 个字符的字符串的示例代码行

.FormFields("txtCaseIntroduction").Range = Me.CaseIntroduction

【讨论】:

  • 这就是解决方案!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
相关资源
最近更新 更多