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

克里斯·F

对于数据库的事件管理方面,我试图让表中字段的数据在149调查报告(该州提供的Word文档模板)中生成(请参见此处的链接)。

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

我修改了我在Internet上找到的用于处理表单字段的代码,并将其分配给我的一个表单上的按钮以帮助生成报告(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文档时起作用?

回应评论中的问题

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

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

辛迪大师

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

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

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用VBA在Access中创建Word文档中的表

来自分类Dev

从 MS-Access 的 VBA 查询中取消选择多个字段

来自分类Dev

DLookup:MS Access VBA 中的查询与表?

来自分类Dev

Word VBA中的嵌套IF字段

来自分类Dev

如何使用MS Access中触发的VBA更改MS Word文档中形状的颜色?

来自分类Dev

如何使用VBA for MS Access在预先存在的表中创建字段?

来自分类Dev

Word中的VBA阵列

来自分类Dev

通过VBA访问MS Word中的嵌套表

来自分类Dev

如何从Access VBA中的表中删除字段

来自分类Dev

比较VBA Access中的表

来自分类Dev

通过检查一张表中的各个字段来推导值

来自分类Dev

Access 2016 VBA 按钮在表中创建新字段

来自分类Dev

Access VBA中的IF .... then语句

来自分类Dev

Access VBA 中的图表

来自分类Dev

Word VBA中的条件机制

来自分类Dev

Word 2010 VBA中的InputBox

来自分类Dev

Word中的条件替换VBA

来自分类Dev

Word对象中的Word VBA范围

来自分类Dev

MS Access中VBA代码的继承

来自分类Dev

VBA MS Access在文本框中更改值的范围为2个字符

来自分类Dev

MATLAB中的fill函数

来自分类Dev

从Access VBA中的表查询日期

来自分类Dev

MongoDB集合中各个字段的不同值

来自分类Dev

使用Knockout.js更新ejGrid中的各个字段

来自分类Dev

使用Knockout.js更新ejGrid中的各个字段

来自分类Dev

MS Access 2013:通过VBA使用MS Word的语法检查

来自分类Dev

Access VBA中的UNION查询

来自分类Dev

在Access中运行VBA代码

来自分类Dev

Access VBA中的SQL查询