VB.NET邮件合并后,Excel保留在内存中

时间:2016-12-20 14:48:03

标签: excel vb.net excel-vba mailmerge vba

尝试使用VB.NET和Excel作为数据源并将Word文档作为主文档进行邮件合并。

无论我尝试过什么,EXCEL.EXE * 32都会在任务管理器中继续运行。我需要它退出,所以我可以重复使用相同的Excel文件作为数据源,以便在同一程序中进行后续合并。

以下是代码的调用方式:

        SourceFile = CreateSourceFile(Queue, F.SelectedPath)

这导致我的函数CreateSourceFile:

Public Function CreateSourceFile(ByVal Queue As String, ByVal PathToSave As String)
    'Creates suit data source file and returns the full path to the file location
    Dim fileDest As String
    ProcessDate = Date.Now().ToString("MM.dd.yy")
    fileDest = PathToSave & "\" & ProcessDate & " " & Queue & " Suit Data.xlsx"

    Dim oXL As New Excel.Application
    Dim oBooks As Excel.Workbooks = oXL.Workbooks
    Dim oMyBook As Excel.Workbook = oXL.Workbooks.Add
    Dim oMySheet As Excel.Worksheet

    frmPleaseWait.Label1.Text = "Exporting suit data to Excel. Please wait."
    ShowFormCentered(frmLegal, frmPleaseWait)

    oMySheet = oXL.ActiveSheet
    oMyBook = oXL.ActiveWorkbook

    Try
        'Begin exporting to Excel
        Using CONN As New SqlConnection(ConnStr)
            Dim SuitData As New DataTable
            Dim dt As DataTable = SuitData
            Dim dc As System.Data.DataColumn

            Dim colIndex As Integer = 0
            Dim rowIndex As Integer = 0
            Dim dr As System.Data.DataRow
            Dim Qry As String

            Qry = "EXEC sp_Custom_IA_POWERHOUSE_PullLegalDocData " & Queue

            Dim cmd As New SqlCommand(Qry, CONN)
            Dim adp As New SqlDataAdapter(cmd)
            adp.Fill(SuitData)

            'Set headers from datatable
            For Each dc In dt.Columns
                colIndex += 1
                oMySheet.Cells(1, colIndex) = dc.ColumnName
            Next

            'Get data rows (dr) from datatable
            For Each dr In dt.Rows
                rowIndex += 1
                colIndex = 0
                For Each dc In dt.Columns
                    colIndex += 1
                    oMySheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
                Next
            Next
        End Using

        'Autofit worksheet cells
        oMySheet.Columns.AutoFit()

        'Append extension to filename if necessary
        If Microsoft.VisualBasic.Right(fileDest, 5) <> ".xlsx" Then
            fileDest = fileDest & ".xlsx"
        End If

        'Remove previously existing file if re-running same file on same day
        If System.IO.File.Exists(fileDest) Then
            System.IO.File.Delete(fileDest)
        End If

        oMyBook.SaveAs(fileDest)

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        'Release and close objects
        oMyBook.Close()
        oXL.Workbooks.Close()
        oXL.Quit()

        releaseObject(oMySheet)
        releaseObject(oMyBook)
        releaseObject(oXL)

    End Try

    Return fileDest

End Function

...然后进入我的垃圾收集程序:

Public Sub releaseObject(ByVal obj As Object)
    Try
        Dim CheckIt As Integer
        CheckIt = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        While CheckIt > 0
            CheckIt = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        End While
        obj = Nothing
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Try
End Sub

我错过了什么?有任何想法如何让EXCEL.EXE * 32内存不足?我不想像以前建议的那样杀死所有Excel进程。回答者不仅表明这不是一个好的解决方案,而且人们整天都在我们公司使用Excel,如果我们杀死所有Excel进程,它可能会干扰最终用户正在做的其他工作。

我愿意接受建议并提前致谢!

编辑:我已经找到并审核了Excel application not quitting after calling quit,但最后的建议是使用GC.Collect()。将它添加到我的垃圾收集例程后,我仍然有同样的问题。请不要将此问题作为上述链接的副本关闭。谢谢。

1 个答案:

答案 0 :(得分:0)

Sorceri在上面的评论中找到了解决方案:oBooks已声明并设置但从未发布。

谢谢Sorceri !!