使用Excel作为数据源自动进行邮件合并,并将其合并到多个.doc文件中作为模板。
第一次通过效果很好!以下是代码应该如何工作的概述:
1)将数据从SQL Server拉取到Excel中,并以.xlsx格式保存在网络驱动器上。
2)将Excel表格作为数据源附加到.doc文件中,合并成功。
3) xlWorkbook.Close()、xlApp.Workbooks.Close()和xlApp.Quit()。然后,我调用我的垃圾回收例程来使用Marshal.ReleaseComObject释放COM对象,它似乎正确地关闭了Excel。
4)使用不同模板的同一个Excel源文件创建下一批信件。
在这一点上,似乎Excel文件在之前被用作数据源之后并没有从内存中释放。当我使用wdAffDoc.MailMerge.OpenDataSource时,Word会弹出一个窗口,询问我要使用哪个表,而表的列表是空白的。源数据电子表格没有列在弹出窗口的“电子表格”窗口中。上一次我遇到这个问题是因为我在另一台机器上打开了源文件,但由于锁的原因,它不能合并。当这段代码崩溃时,我在任务管理器中看到,在我的用户名下面列出了1到2个"EXCEL.EXE *32“条目。在终止剩余的EXCEL.EXE *32进程之前,代码不会运行。
寻找任何关于我应该在这里进行的方向的输入。我应该怀疑我的垃圾收集例程,还是你认为它是别的什么?
下面是我的垃圾收集:
Public Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
MsgBox(ex.Message)
Finally
GC.Collect()
End Try
End Sub下面是拉取源数据后的第一次传递(工作方式与预期一致):
frmPleaseWait.Label1.Text = "Now merging documents. Please wait."
frmPleaseWait.Refresh()
Dim wdApp As New Word.Application
Dim wdDoc As New Word.Document
'Select template based on Queue chosen
If Queue = "716" Then
wdDoc = wdApp.Documents.Open("X:\Admin\LEGAL\MERGE LETTERS\UPH Vfn 2 Def.doc")
End If
wdApp.Visible = False 'Set this to False before going live -- true for debugging
wdDoc.MailMerge.OpenDataSource(Name:=fileDest, SQLStatement:="SELECT * FROM [Sheet1$]") 'Add a WHERE clause for filtering for affidavits, etc.
'.Destination 0 = DOCUMENT, 1 = PRINTER
wdApp.ActiveDocument.MailMerge.Destination = 0 'send to new document
With wdApp.ActiveDocument.MailMerge.DataSource
.FirstRecord = 1 'wdDefaultFirstRecord
.LastRecord = -16 'wdDefaultLastRecord
End With
wdApp.ActiveDocument.MailMerge.Execute(Pause:=False)
wdDoc.Close(SaveChanges:=False) 'Close the original mail-merge template file
wdApp.ActiveDocument.SaveAs2(savePath & "\" & ProcessDate & " " & Queue & " Verifications.doc")
wdApp.Quit()
wdDoc = Nothing
wdApp = Nothing下面是第二个(冒犯的)步骤:
Dim wdAffApp As New Word.Application
Dim wdAffDoc As New Word.Document
If Queue = "716" Then
wdAffDoc = wdAffApp.Documents.Open("X:\Admin\LEGAL\MERGE LETTERS\Suit Affidavit 2 Def.doc")
End If
wdAffApp.Visible = False 'Set this to False before going live -- true for debugging
'****************THIS IS THE LINE THAT PRODUCES THE ERROR****************
wdAffDoc.MailMerge.OpenDataSource(Name:=fileDest, SQLStatement:="SELECT * FROM [Sheet1$] WHERE [Suit_Bal] >= 5000") 'Add a WHERE clause for filtering for affidavits, etc.
'************************************************************************
'.Destination 0 = DOCUMENT, 1 = PRINTER
wdAffApp.ActiveDocument.MailMerge.Destination = 0 'send to new document
With wdAffApp.ActiveDocument.MailMerge.DataSource
.FirstRecord = 1 'wdDefaultFirstRecord
.LastRecord = -16 'wdDefaultLastRecord
End With
wdAffApp.ActiveDocument.MailMerge.Execute(Pause:=False)
wdAffDoc.Close(SaveChanges:=False) 'Close the original mail-merge template file
wdAffApp.ActiveDocument.SaveAs2(savePath & "\" & ProcessDate & " " & Queue & " Affidavits.doc")
wdAffApp.Quit()
wdAffDoc = Nothing
wdAffApp = Nothing
'Signal the end
frmPleaseWait.Dispose()
MsgBox("Mail merge complete")发布于 2016-12-15 04:47:40
显然我的代码起作用了!
在进一步查看之后,我发现任务管理器中仍然运行着WINWORD.EXE *32。当我切换到该应用程序时,一个弹出窗口询问我是否要保存或删除恢复的文件...一定是我之前的一次运行中的一个错误卡在了内存中。一旦我告诉Word删除恢复的文件,代码就会按预期完成。呼!很高兴这个问题解决了!
https://stackoverflow.com/questions/41151428
复制相似问题