table 中各个字段的 MS Access 中的 Word Fill VBA

Word Fill VBA in MS Access for various fields in table

对于我们数据库的事件管理方面,我试图从我的 table(s) 中的字段中生成 149 调查报告,该报告是国家提供的 Word 文档模板 (see link here).

我制作了文档的只读版本以通过强制用户另存为来保持其完整性,并加载带有书签的文本表单字段以供参考(示例: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:\ABC18\Case Files18 - 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 和附件字段)没有。所有这些字段都存在于同一个 table 上。还需要注意的是,案例介绍曾经有效,但由于我试图找出更多的表单字段以应用于文档和参考而停止了工作。目标是让调查人员基本上在数据库中完成所有工作,然后将其导出为所需格式以提交给州政府。

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

回复评论中的问题

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

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

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

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

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