【发布时间】:2012-08-08 16:13:28
【问题描述】:
我在 MS-Access 中有一个 VBA 模块,它应该将数据从数据库加载到 MS-Word 文档的表单域中。我认为它工作正常,但它似乎不一致。有时它有效,有时则无效。我不知道是什么让它无法工作。当我单步调试调试器时,它不会抛出任何错误,但有时它不会打开 MS-Word。
以下是相关代码:
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.Open("\\srifs01\hresourc\EHS Department\EHS Database\IpadUpload\Lab Inspection Deficiency Resolution Report.docx", , True)
'Sometimes word doesn't open and I think the issue is around here.
With doc
.FormFields("frmID").Result = Me!id
.FormFields("frmSupervisor").Result = Me!LabPOC
.FormFields("frmInspector").Result = Me!InspectorName
.FormFields("frmBuilding").Result = Me!BuildingName
.FormFields("frmRoom").Result = Me!Rooms
.FormFields("frmComments").Result = Me!Comments
.Visible = True
.Activate
.SaveAs "'" & Me!id & "'"
.Close
End With
Set doc = Nothing
Set appWord = Nothing
感谢任何帮助。提前致谢。
【问题讨论】:
-
您收到的错误消息的确切措辞是什么?
-
没有错误信息。有时它只是行不通。它通过但什么也不做。我认为是因为它没有打开 MS-Word。
-
您似乎没有关闭 Word。 (appWord.Quit) 您是否在任务管理器中显示副本?
-
我在
With doc .Close中结束词 -
您正在关闭文档,您不一定要关闭 Word。