掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
複数Excelファイルオープンにおける、オブジェクト解放について (ID:146855)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
VB2008 初心者のはるです。初投稿させて頂きます。 環境は以下の通りです。 Excel:Excel2007 VB.net:VB2008express SP1 OS:WinXPsp3 以下はExcelのオープンから解放までのコーディングを抜粋しております。 Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click Dim exlExcelApp As New Excel.Application Dim exlBooks As Excel.Workbooks = exlExcelApp.Workbooks Dim exlExcelBook As Excel.Workbook Dim exlExcelSheet As Excel.Worksheet For Each intFileList In Me.lstTargetFile.SelectedIndices 'lstTargetFileリストボックスで選択されているExcelファイル分処理 exlExcelBook = exlExcelApp.Workbooks.Open(strDirPath & "\" & Me.lstTargetFile.Items(intFileList)) Dim exlSheets As Excel.Sheets = exlExcelBook.Worksheets exlExcelApp.Visible = True exlExcelApp.DisplayAlerts = False For i = 1 To exlExcelBook.Sheets.Count exlExcelSheet = exlExcelBook.Sheets(i) Dim exlCells As Excel.Range = exlExcelSheet.Cells For iRows = intEmployeeDataFromRows To intEmployeeDataToRows '全シートのexlCellsに対する参照処理 Next MRComObject(exlCells) exlCells = Nothing MRComObject(exlExcelSheet) exlExcelSheet = Nothing Next If Not exlExcelBook Is Nothing Then Try exlExcelBook.Close(False) Finally MRComObject(exlExcelBook) End Try End If exlExcelBook = Nothing MRComObject(exlSheets) exlSheets = Nothing MRComObject(exlBooks) exlBooks = Nothing exlExcelApp.Quit() intFileCnt = intFileCnt + 1 Next 'Excelの結果に応じて、画面のListViewの編集処理 If Not exlExcelApp Is Nothing Then Try exlExcelApp.Quit() Finally MRComObject(exlExcelApp) End Try End If exlExcelApp = Nothing End Sub Public Shared Sub MRComObject(Of T As Class)(ByRef objCom As T, Optional ByVal force As Boolean = False) If objCom Is Nothing Then Return End If Try If System.Runtime.InteropServices.Marshal.IsComObject(objCom) Then If force Then System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objCom) Else Dim count As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(objCom) End If End If Finally objCom = Nothing End Try End Sub [質問] 上記の処理で数回処理した場合は閉じるボタン等でExcel.EXEがプロセスから消えてくれます。 しかし、10回起動を越したぐらいからかプロセスにExcel.EXEが5個以上残ってしまった時、 終了してもプロセスから消えてくれません。 たぶん、複数のCOMがオープンの度にExcel.EXEを起動してて、残骸がどんどん増えてるようですが・・。 いろいろ試してみたのですが、どこかおかしいのかわからない状態に陥ってしまったので、 ご教授をお願いしたく投稿させて頂きました。 宜しくお願い致します。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.