【问题标题】:Programmatically Remove Mailmerge Data Source using VB.NET使用 VB.NET 以编程方式删除 Mailmerge 数据源
【发布时间】:2016-12-14 20:16:50
【问题描述】:

致力于使用 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")

【问题讨论】:

    标签: excel vb.net ms-word mailmerge


    【解决方案1】:

    显然我的代码有效!

    进一步查看后,我发现任务管理器中仍然运行着 WINWORD.EXE *32。当我切换到该应用程序时,一个弹出窗口询问我是否要保存或删除恢复的文件......一定是我之前的一次运行中出现的错误,卡在了内存中。一旦我告诉 Word 删除恢复的文件,代码就会按预期完成。哇!很高兴这个问题解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多