我有一个访问数据库,该数据库将标签打印为Word文档。使用我的访问数据库中的信息填充单词文档,然后将其关闭。这可以在我的个人笔记本电脑上使用,并且每次都可以打印。当我将其转移到我的工作笔记本电脑时,它第一次工作,然后由于文档保持打开状态而失败。该文档未显示在任务管理器的流程中,我的笔记本电脑正在使用Office 365,而我的工作笔记本电脑在Office 2016中是版本问题吗?下面的代码。如果完全错误,请提出修复建议
Dim appWord As Word.Application
Dim doc As Word.Document
Dim thepath As String
thepath = CurrentProject.Path
'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(thepath & "\label.docx", , False)
'ActiveDocument.Tables(1).Cell(1, 1).va Me.PartNumber
'
'ActiveDocument.FormFields(fldPartNumber). = Me!PartNumber
If Selection.FormFields.Count >= 1 Then
MsgBox Selection.FormFields(1).Name
End If
ActiveDocument.FormFields("Text1").Result = Me.PartNumber
ActiveDocument.FormFields("Text2").Result = Me.SerialNumber
'MsgBox (ActiveDocument.FormFields("Text1").Result)
ActiveDocument.FormFields("Text10").Result = Me.BatchNumber
ActiveDocument.FormFields("Text7").Result = Me.Qty
ActiveDocument.FormFields("Text6").Result = Me.Lifex
ActiveDocument.FormFields("Text3").Result = Me.Station
ActiveDocument.FormFields("Text4").Result = Me.Store
ActiveDocument.FormFields("Text5").Result = Me.Bin
ActiveDocument.FormFields("Text11").Result = Me.Description
'.FormFields("fldCountry").Result = Me.Country
' FormFields("fldPhone").Result = Me.Phone
'.FormFields("fldFax").Result = Me.Fax
activedocuments.FormFields.Visible = True
'ActiveDocument.FormFields.Activate
appWord.DisplayAlerts = False
doc.PrintOut Background = True
appWord.DisplayAlerts = True
'CreateObject("Shell.Application").Namespace(0).ParseName("C:\Boeing Ireland Serviceable Label editable form.docx").InvokeVerb ("Print")
Set doc = Nothing
doc.Close
appWord.Quit (True)
Set appWord = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description
'
End Sub
我认为问题在于您在代码末尾处的工作顺序。您应该先关闭/退出对象,然后再将其设置为空。另外,我建议您有一个退出部分来清理对象,而不管是否存在错误。类似于:
Sub sFoo
On Error GoTo E_Handle
' Word automation code here
sExit:
On Error Resume Next
doc.Close
Set doc=Nothing
appWord.Quit (True)
Set appWord=Nothing
Exit Sub
E_Handle:
MsgBox Err.Description
Resume sExit
End Sub
问候,
[好,答案是Microsoft Office自动化中的一个已知问题,由于未能引用对象,该文档被打开了。 Microsoft问题189618是我用来解决此问题的参考。{原因由于有一行代码调用了Word对象,方法或属性,而没有使用Word对象变量对它进行限定,因此Visual Basic已建立了对Word的引用。在结束程序之前,Visual Basic不会释放此引用。当代码多次运行时,该错误的引用会干扰自动化代码。解决方法修改代码,以使对Word对象,方法或属性的每次调用都使用适当的对象变量进行限定。}
我最初的错误捕获是在接下来的错误恢复上使用,这可以绕过实际的运行时错误426。这是由于代码行将文档引用设置为
`enter code here`Set doc = Documents.Open(thepath & "\label.docx",,False,,,,True)'
因为它不引用appword,所以它使实例保持打开状态。因此,我第二次打开文档。解决方法很简单。Set doc= appword.Documents.Open(thepath & "\label".docx",,False,,,,True)
该代码现在可以正常工作,下面是该脚本的经过完全更正和清理的版本,其中包括提供的Applecores建议。
Private Sub Command67_Click()
On Error GoTo E_Handle
Dim appwd As Word.Application
Dim doc As Word.Document
Dim thepath As String
thepath = CurrentProject.Path
Set appwd = CreateObject("Word.Application")
appwd.Visible = True
Set doc = appwd.Documents.Open(thepath & "\label.docx", , False, , , , True)
doc.FormFields("Text1").Result = Me.PartNumber
doc.FormFields("Text2").Result = Me.SerialNumber & nullstring
doc.FormFields("Text10").Result = Me.BatchNumber & nullstring
doc.FormFields("Text7").Result = Me.Qty
doc.FormFields("Text6").Result = Me.Lifex
doc.FormFields("Text3").Result = Me.Station
doc.FormFields("Text4").Result = Me.Store
doc.FormFields("Text5").Result = Me.Bin & nullstring
doc.FormFields("Text11").Result = Me.Description
appwd.DisplayAlerts = False
doc.PrintOut Background = True
appwd.DisplayAlerts = True
doc.Close SaveChanges:=wdDoNotSaveChanges
Set doc = Nothing
appwd.Quit
Set appwd = Nothing
Exit Sub
sExit:
On Error Resume Next
doc.Close
Set doc = Nothing
appwd.Quit
Set appwd = Nothing
Exit Sub
E_Handle:
MsgBox Err.Description
Resume sExit
End Sub