|
分類:[VB.NET/VB2005 以降]
お世話になります。
VB2005でEXCEL2000にデータを出力しています。
winXPSP2で実行するとプロセスが開放されません。しかし、Windws2003サーバーで実行するとプロセスが開放されます。
色々なサイトで皆さんが飽き飽きしたネタだと思いますが、何がおかしいかご教授下さい。
Dim xlApplication As Excel.Application = Nothing
' COM オブジェクトの解放を保証するために Try 〜 Finally を使用する
Try
xlApplication = New Excel.Application()
' 警告メッセージなどを表示しないようにする
xlApplication.DisplayAlerts = False
Dim xlBooks As Excel.Workbooks = xlApplication.Workbooks
Try
' 既存の Excel ブックを開く
Dim xlBook As Excel.Workbook = xlBooks.Open(wsPath)
Try
Dim xlSheets As Excel.Sheets = xlBook.Worksheets
Try
Dim xlSheet As Excel.Worksheet = DirectCast(xlSheets(1), Excel.Worksheet)
Try
Dim xlCells As Excel.Range = xlSheet.Cells
Try
'シートの再計算を中止(処理高速化のため)
xlSheet.EnableCalculation = False
Dim xlRange As Excel.Range = DirectCast(xlCells(3, 2), Excel.Range)
Dim xlBorders As Excel.Borders
Try
xlRange.Value = deCargoDate.Text
Finally
If Not xlRange Is Nothing Then
Dim iRemainCount As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange)
If iRemainCount <> 0 Then
MessageBox.Show("参照カウントの残り(xlRange) == " & iRemainCount.ToString())
End If
xlRange = Nothing
End If
End Try
'編集
For mrow As Integer = 0 To dt.Rows.Count - 1
For mcol As Integer = 0 To dt.Columns.Count - 2
xlRange = DirectCast(xlCells(mrow + 6, mcol + 2), Excel.Range)
xlBorders = DirectCast(xlRange.Borders, Excel.Borders)
Try
xlBorders.LineStyle = Excel.XlLineStyle.xlContinuous
xlRange.Value = dt.Rows(mrow).Item(mcol)
Finally
If Not xlBorders Is Nothing Then
Dim iRemainCount As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBorders)
If iRemainCount <> 0 Then
MessageBox.Show("参照カウントの残り(xlBorders) == " & iRemainCount.ToString())
End If
xlBorders = Nothing
End If
If Not xlRange Is Nothing Then
Dim iRemainCount As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange)
If iRemainCount < 0 Then
MessageBox.Show("参照カウントの残り(xlRange) == " & iRemainCount.ToString())
End If
xlRange = Nothing
End If
End Try
Next
Next
''シートの再計算を再開
xlSheet.EnableCalculation = True
xlBook.SaveAs(wsNewPath)
Finally
If Not xlCells Is Nothing Then
Dim iRemainCount As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCells)
MessageBox.Show("参照カウントの残り(xlCells) == " & iRemainCount.ToString())
xlCells = Nothing
End If
End Try
Finally
If Not xlSheet Is Nothing Then
Dim iRemainCount As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
MessageBox.Show("参照カウントの残り(xlSheet) == " & iRemainCount.ToString())
xlSheet = Nothing
End If
End Try
Finally
If Not xlSheets Is Nothing Then
Dim iRemainCount As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets)
MessageBox.Show("参照カウントの残り(xlSheets) == " & iRemainCount.ToString())
xlSheets = Nothing
End If
End Try
Finally
If Not xlBook Is Nothing Then
Try
xlBook.Close()
Finally
Dim iRemainCount As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
MessageBox.Show("参照カウントの残り(xlBook) == " & iRemainCount.ToString())
xlBook = Nothing
End Try
End If
End Try
Finally
If Not xlBooks Is Nothing Then
Dim iRemainCount As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks)
MessageBox.Show("参照カウントの残り(xlBooks) == " & iRemainCount.ToString())
xlBooks = Nothing
End If
End Try
Finally
If Not xlApplication Is Nothing Then
Try
xlApplication.Quit()
Finally
Dim iRemainCount As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication)
MessageBox.Show("参照カウントの残り(xlApplication) == " & iRemainCount.ToString())
xlApplication = Nothing
End Try
End If
End Try
参照カウントの残りはすべてゼロです。
以上、宜しくお願い致します。
|