Dim lastRow As Integer = sh.Cells(sh.Rows.Count, "B").End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).row とrange.ClearContents()をコメントにし、 range = Cells.Range("B5:I" & lastRow)をrange = Cells.Range("B5:I5")に変更するとプロセスが残らず終了できるので、lastRowかrange.ClearContents()のどちらかが解放漏れしていてプロセスが残ってしまっていると考えて居るのですが、解放の仕方が分からない状態です。 お力を貸してください。
Private Sub btn作成_Click(sender As Object, e As EventArgs) Handles btn作成.Click If txtPath.Text = "" Then Exit Sub End If
Dim str As String = "" Dim strCount = 0
Dim ex As New Microsoft.Office.Interop.Excel.Application Dim books As Microsoft.Office.Interop.Excel.Workbooks = ex.Workbooks Dim wb As Microsoft.Office.Interop.Excel.Workbook Dim sheets As Microsoft.Office.Interop.Excel.Sheets Dim sh As Microsoft.Office.Interop.Excel.Worksheet
Dim Cells As Microsoft.Office.Interop.Excel.Range Dim range As Microsoft.Office.Interop.Excel.Range
Try For rw As Integer = 0 To dgv.RowCount - 1 If strCount = 0 Then wb = books.Open(txtPath.Text) sheets = wb.Worksheets sh = sheets("発注一覧") Cells = sh.Cells strCount = strCount + 1
'ファイル内のセル初期化 Dim lastRow As Integer = sh.Cells(sh.Rows.Count, "B").End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).row If lastRow >= 5 Then range = Cells.Range("B5:I" & lastRow) range.ClearContents() End If
End If
strCount = strCount + 1 End If Next
wb.Save() wb.Close(False) ex.Quit()
Catch exc As Exception Finally If Cells IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(Cells) End If If range IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(range) End If If sheets IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets) End If If sh IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(sh) End If If books IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(books) End If If wb IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(wb) End If If ex IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(ex) End If
Cells = Nothing range = Nothing sheets = Nothing sh = Nothing books = Nothing wb = Nothing ex = Nothing
GC.Collect() GC.WaitForPendingFinalizers()
btn作成.Enabled = True
If strCount <> 0 Then MsgBox(“作成完了”) End If End Try