Imports System
Imports System.Diagnostics
Private Sub printdocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles printdocument1.EndPrint
Do While True
'kill all resident excels in memory with extreme prejudice
If CloseAllResidentExcels() = True Then
Exit Do
End If
Loop
'reinit your vars here
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
wbkToShow = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
wshToShow = DirectCast(wbkToShow.Worksheets("Sheet1"), Excel.Worksheet)
End Sub
Private Function CloseAllResidentExcels() As Boolean
CloseAllResidentExcels = False
Try
'loop this process until there are no valid id's to return
Dim Prc As Process
Dim localByName As Process() = Process.GetProcessesByName("EXCEL")
Prc = Process.GetProcessById(localByName(0).Id)
Prc.Kill()
Catch ex As Exception
CloseAllResidentExcels = True
End Try
End Function
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)