对于数据库的事件管理方面,我试图让表中字段的数据在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 introduction
,investigator
和附件字段)没有。所有这些字段都存在于同一表中。还应注意,案例介绍曾经有用,但是由于我试图找出更多适用于文档和参考的表单字段而停止工作。目的是让调查人员基本上在数据库中完成所有工作,然后将其导出为所需格式,以提交给州。
我的问题:我需要对以上代码做些什么才能使非工作字段在填充Word文档时起作用?
回应评论中的问题
没有发生错误;当我按下按钮时,文本框根本就不会填充。
表单字段不需要出现在结果文档中。它们只是数据的“目标”。
由于不需要在结果文档中保留表单字段,因此最简单的方法是将数据简单地插入FormField.Range
,这将替换(删除)表单字段。如果一致性很重要(最终结果对用户的外观),则可以用这种方式编写整个代码,但是从编程的角度来看,则不必如此。
注意:如果激活了表单保护,则需要将其关闭才能使此方法起作用
If doc.ProtectionType <> -1 Then doc.Unprotect '-1 = wdNoProtection
超过255个字符的字符串的示例代码行
.FormFields("txtCaseIntroduction").Range = Me.CaseIntroduction
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句