EXCELのプロセスが残る問題とその解決方法

このQ&Aのポイント
  • EXCELのプロセスが残る問題について質問があります。VB2008でEXCELを操作する際、特定の記述を行うとプロセスが残ってしまいます。問題を解決するためには、特定の記述を削除することが必要です。
  • 具体的な問題の内容を調査し、いらない部分を削除した結果、解決に至った経緯を説明しています。プロセスが残らずに消えるためには、特定の操作が必要です。
  • 質問者は単純なブックの中のシートのコピーを行っており、プロセスが残る問題に悩んでいます。解決方法として、特定の記述を削除することを提案しています。
回答を見る
  • ベストアンサー

EXCELのプロセスに関して

教えて下さい。 VB2008でEXCELを操作しようと考えていますが、以下のような記述をした場合、 EXCELのプロセスが残ってしまいます。 いろいろ調査し、いらない部分を削除した結果、以下のところまで絞込みを行い、 ****のところを削除した場合、問題なくプロセスは終了した為、****印の中の記述が 原因というところまで絞れました。 単純にブックの中のシートをコピーで追加しているだけですが、何がどのようにすれば プロセスは残らずに消えてくれるでしょうか。。。 よろしくお願いします。 《記述内容》 '既存のEXCELファイルを開く Dim xlFilePath As String = "C:\test.xls" '起動時の処理 Dim xlApp As New Excel.Application Dim xlBooks As Excel.Workbooks = xlApp.Workbooks Dim xlBook As Excel.Workbook = xlBooks.Open(xlFilePath) xlApp.Visible = True ' 確認のためExcelのウィンドウを表示する Dim xlSheets As Excel.Sheets = xlBook.Worksheets Dim xlSheet As Excel.Worksheet = CType(xlSheets.Item(1), Excel.Worksheet) ************************* xlSheet = xlSheets.Item(1) 'シートの選択 xlSheet.Copy(After:=xlBook.Worksheets(1)) 'シートのコピー xlSheet = xlSheets.Item(2) '再度シートを選択 xlSheet.Name = "zz" 'シートに名前を付ける ************************* xlBooks.Close() xlApp.DisplayAlerts = False xlApp.Quit() ' COM オブジェクトの解放 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet) ' Excel のプロセス終了 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

>xlSheet.Copy(After:=xlBook.Worksheets(1)) 'シートのコピー を Dim xlSheet2 As Excel.Worksheet = xlBook.Worksheets(1) xlSheet.Copy(After:=xlSheet2) 'シートのコピー のようにしてみてはどうでしょうか。 また、 >System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) の後ろに GC.Collect() を入れるといいかもしれません。

yurix_1
質問者

お礼

返答が遅くなりまして申し訳ありません。 ご指摘の通り記述したところ、問題なくプロセスが終了しました。 ありがとうございました。

その他の回答 (1)

回答No.2

COM オブジェクトの解放で 上位オブジェクトから解放してますが、それは大丈夫なのでしょうか? 上位オブジェクトにつながっている下位オブジェクトが宙ぶらりんになったりしないでしょうか?

yurix_1
質問者

お礼

教えて頂き、ありがとうございました。 とても参考になりました。

関連するQ&A

  • EXCELファイルからの値取得に関して

    環境:Visual Basic 2008 教えて下さい。 画面上から2つのEXCELファイルを指定し、1回の処理でその2つの EXCELからそれぞれ値を取得したいと考えています。 その為、以下のような記述をしましたが、2回目の値取得のところで "オブジェクト参照がオブジェクト インスタンスに設定されていません。" というエラーメッセージとなってしまいます。 1つのファイルから取得する事は経験ありましたが、同時に2回は初めてで ある為、記述方法が正しいのか、、、 どのように記述したら良いかを教えて下さい。 初歩的な質問で申し訳ありませんが、よろしくお願いします。 《記述内容》 'EXCELファイルを開く Dim xlFilePath As String = ofd1.FileName '起動時の処理 Dim xlApp As Object xlApp = CreateObject("Excel.Application") Dim xlBooks As Object = xlApp.Workbooks Dim xlBook As Object = xlBooks.Open(xlFilePath) '現シート数 Dim S_COUNT As Integer = xlBook.Worksheets.Count Dim xlSheets As Object = xlBook.Worksheets Dim xlSheet As Object = CType(xlSheets.Item(1), Object) '現シート数 S_COUNT = xlBook.Worksheets.Count Dim R_名称 As String R_名称 = "" R_名称 = xlSheet.Cells(1, 1).Value 'COM オブジェクトの解放 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks) 'Excel のプロセス終了 xlApp.DisplayAlerts = False xlApp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) GC.Collect() 'のEXCELファイルを開く Dim xlFilePath2 As String = ofd2.FileName '起動時の処理 Dim xlApp2 As Object xlApp2 = CreateObject("Excel.Application") Dim xlBooks2 As Object = xlApp2.Workbooks Dim xlBook2 As Object = xlBooks2.Open(xlFilePath2) '現シート数 Dim S_COUNT2 As Integer = xlBook2.Worksheets.Count Dim xlSheets2 As Object = xlBook2.Worksheets Dim xlSheet2 As Object = CType(xlSheets2.Item(1), Object) '現シート数 S_COUNT = xlBook2.Worksheets.Count Dim R_番号 As String R_番号 = "" R_番号 = xlSheet2.Cells(I_COUNT2, 1).Value 'COM オブジェクトの解放 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks) 'Excel のプロセス終了 xlApp.DisplayAlerts = False xlApp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) GC.Collect()

  • EXCELのプロセスに関して2

    教えて下さい。 先日もEXCELに関して投稿させて頂きましたが、やはり少しソースを変更すると、最終的に EXCELのプロセスが残ってしまいます。 前回は、記述の最後に、"GC.Collect"を追加するようにご指摘頂き、余計なメモリを解放する事で、 プロセスは残らないようになりましたが、今回は、以下の記述のように、発注先コードが違った場合、 シートを分けるという事をしたい為、以下のような記述をした際に、プロセスが残ってしまう現象が 発生しました。 切り分けを行った結果、やはり*****印の部分があるかないかで、プロセスが残る現象が発生しますが、 何かプロセスを残してしまうような記述をしているのでしょうか。。。 どなたか教えて頂ければ幸いです。 よろしくお願いします。 《記述内容》 '既存のEXCELファイルを開く Dim xlFilePath As String = "C:\order.xls" '起動時の処理 Dim xlApp As New Excel.Application Dim xlBooks As Excel.Workbooks = xlApp.Workbooks Dim xlBook As Excel.Workbook = xlBooks.Open(xlFilePath) '確認のためExcelのウィンドウを表示する xlApp.Visible = True Dim xlSheets As Excel.Sheets = xlBook.Worksheets Dim xlSheet As Excel.Worksheet = CType(xlSheets.Item(1), Excel.Worksheet) 'シートの選択 xlSheet = xlSheets.Item(1) 'シートのコピー xlSheet.Copy(After:=xlBook.Worksheets(1)) '再度シートを選択 xlSheet = xlSheets.Item(2) 'シートに名前を付ける xlSheet.Name = "zz" Dim x, y As Integer x = 0 y = 0 Dim mcount As Integer Dim fcount As Integer Dim D_発注先コード As String D_発注先コード = h_date(0).IN_発注先コード Dim strDat(14, 14) As Object For icount As Integer = 0 To count - 1 If D_発注先コード <> h_date(icount).IN_発注先コード Then 'データをEXCELファイルに書き出し Dim xlRange As Excel.Range 'データの入力セル範囲 xlRange = xlSheet.Range("A17:O31") 'セルへデータの入力 xlRange.Value = strDat System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange) D_発注先コード = h_date(icount).IN_発注先コード x = 0 *********************************************************************** xlSheet = xlSheets.Item(1) 'シートの選択 xlSheet.Copy(After:=xlBook.Worksheets(2)) 'シートのコピー xlSheet = xlSheets.Item(3) '再度シートを選択 xlSheet.Name = "YY" 'シートに名前を付ける *********************************************************************** strDat(x, 1) = h_date(icount).IN_指示納期 strDat(x, 2) = h_date(icount).IN_発注番号 fcount = 1 Else strDat(x, 1) = h_date(icount).IN_指示納期 strDat(x, 2) = h_date(icount).IN_発注番号 End If x = x + 1 Next Dim xlRange3 As Excel.Range 'データの入力セル範囲 xlRange3 = xlSheet.Range("A17:O31") 'セルへデータの入力 xlRange3.Value = strDat 'COM オブジェクトの解放 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange3) xlBooks.Close() xlApp.DisplayAlerts = False xlApp.Quit() 'COM オブジェクトの解放 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets) 'Excel のプロセス終了 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) GC.Collect()

  • VB.NETからEXCELの起動が、うまくいかない

    3回目の同じ質問になってしまうのですがよろしくお願いいたします。 下記のようなソースでEXCELファイルを作成し、その後ファイルを開くという処理をしています。 Dim xlApp As New Excel.Application Dim xlBooks As Excel.Workbooks = xlApp.Workbooks Dim xlBook As Excel.Workbook = xlBooks.Add Dim xlSheets As Excel.Sheets = xlBook.Worksheets Dim xlSheet As Excel.Worksheet = xlSheets.Item(1) Dim xlRange As Excel.Range Dim xlCells As Excel.Range = xlSheet.Cells xlRange = xlCells(1, 10) xlRange.Value = "--" xlRange = xlCells(1, 5) xlCells.EntireColumn.AutoFit() xlBook.SaveAs(sPath & "\" & sFileName, Excel.XlFileFormat.xlWorkbookNormal) System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlCells) System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlRange) System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlSheet) System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlSheets) xlCells = Nothing xlRange = Nothing xlSheet = Nothing xlSheets = Nothing xlBook.Close() System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlBook) System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlBooks) xlBook = Nothing xlBooks = Nothing xlApp.Quit() System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp) xlApp = Nothing GC.Collect() System.Diagnostics.Process.Start(sPath & "\" & sFileName) ある特定のPCでのみEXCELのメニューバーとステータスバーのみしか表示されない状態です。 起動部分(Process.Start)をコメントアウトして確認したところ GC.COLLECTでEXCELが終了することなく、アプリの終了時にEXCELが終了する 状態にあります。 これはオブジェクトの開放がうまくいっていないのでしょうか? よろしくお願いいたします。

  • VB2005でのEXCELマクロ操作

    お世話になっております。 VB2005から指定したEXCELファイルを開き、標準モジュールにあるマクロを起動したいと考えております。 調べたところ、Runメソッドを使用するようですが、マクロファイルを開くところで停止してしまいます。 以下にソースを書きましたので、アドバイス頂きたいと思います。 Dim n As Short Dim xlApp As Excel.Application = Nothing Dim xlBooks As Excel.Workbooks = Nothing Dim xlBook As Excel.Workbook = Nothing Dim xlSheets As Excel.Sheets = Nothing Dim xlSheet As Excel.Worksheet = Nothing Dim xlRange As Excel.Range = Nothing Dim Fname As String = "C:\Documents and Settings\hoge\Application Data\Microsoft\Excel\XLSTART\PERSONAL.XLS" '読み込むファイル Try xlApp = New Excel.Application() xlBooks = xlApp.Workbooks For n = 0 To fil.Length - 1 xlBook = xlBooks.Open(fil(n)) xlSheets = xlBook.Worksheets xlApp.Run("'" & Fname & "'!module2.DB処理", fil(n)) '引数をもつ処理の呼び出し方法 '1000ミリ秒(1秒)待機する System.Threading.Thread.Sleep(1000) ' Excel ブックを保存する xlApp.DisplayAlerts = False xlBook.Save() Next ' Microsoft Excel を終了する xlApp.Quit() Finally If Not xlSheet Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet) End If If Not xlSheets Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets) End If If Not xlBook Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook) End If If Not xlBooks Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks) End If If Not xlApp Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) End If End Try

  • VB2005でのEXCEL処理で警告が消えません

    いつもお世話になっております。 私はVB2005入門者です。 EXCELで処理を行っております。 処理事態は完了するのですが 下記のコードの後半部の警告がどうやっても潰せないので 修正方法のアドバイスをお願いします。 Dim xlApp As Excel.Application Dim xlBooks As Excel.Workbooks Dim xlBook As Excel.Workbook Dim xlSheets As Excel.Sheets Dim xlSheet As Excel.Worksheet ' 必要な変数は Try の外で宣言する Try ' 必要な変数は Try の中でインスタンス化する xlApp = New Excel.Application() xlBooks = xlApp.Workbooks xlBook = xlBooks.Open("C\:hoge0.xls") xlSheets = xlBook.Worksheets xlBook.Worksheets(1).Range("F4") = shhoge(0) xlBook.Worksheets(1).Range("I4") = shhoge(0) xlBook.Worksheets(1).Range("K4") = shhoge(5) xlBook.Worksheets(1).Range("M5") = shhoge(0) xlBook.Worksheets(1).Range("O4") = shhoge(1) xlBook.Worksheets(1).Range("R4") = shhoge(1) xlBook.Worksheets(1).Range("U5") = shhoge(1) xlBook.Worksheets(1).Range("T5") = shhoge(2) xlBook.Worksheets(1).Range("W5") = shhoge(3) xlBook.Worksheets(1).Range("X5") = shhoge(4) ' シートのカウントが 1 より大きければシートを削除する If xlSheets.Count > 1 Then ' Display アラートを消す xlApp.DisplayAlerts = False ' シートを削除する xlSheet = DirectCast(xlSheets(2), Excel.Worksheet) xlSheet.Delete() End If ' 1000 ミリ秒 (1秒) 待機する System.Threading.Thread.Sleep(1000) ' Excel ブックを保存する xlApp.DisplayAlerts = False xlBook.SaveAs("C:\hoge.xls") ' Microsoft Excel を終了する xlApp.Quit() Finally ' 参照したオブジェクトをすべて解放する If Not xlSheet Is Nothing Then   ←[警告]値が割り当てられる前に使用されています。 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet) End If If Not xlSheets Is Nothing Then   ←[警告]値が割り当てられる前に使用されています。 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets) End If If Not xlBook Is Nothing Then   ←[警告]値が割り当てられる前に使用されています。 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook) End If If Not xlBooks Is Nothing Then   ←[警告]値が割り当てられる前に使用されています。 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks) End If If Not xlApp Is Nothing Then   ←[警告]値が割り当てられる前に使用されています。 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) End If End Try Form2.Show() End Sub

  • VB2008からのEXCEL出力に関して

    VB2008にて開発を行っていますが、その中で、あるデータをEXCELに出力するような 処理を行いたいと考えています。 その際に、ホームページ等で検索を行いながら、以下のような記述をしましたが、やはり 中身を完全に理解していない為、あまり応用がききません。。。 以下の内容で、1シート目までの出力は正しく行われ、問題なくプロセスも残らずに終了 する事ができました。 ただし、指定の行(10行目)までデータが書き込まれたら、1シートを追加して 次のシートに記載するというようなループ処理を考えていますが、どのように記述したら 良いでしょうか。。。。 どこまでをループ対象にすれば良いかが。。。 教えて下さい。 よろしくお願いします。 《記述内容》 '既存のEXCELファイルを開く Dim xlFilePath As String = "C:\order.xls" '起動時の処理 Dim xlApp As New Excel.Application Dim xlBooks As Excel.Workbooks = xlApp.Workbooks Dim xlBook As Excel.Workbook = xlBooks.Open(xlFilePath) xlApp.Visible = True ' 確認のためExcelのウィンドウを表示する Dim xlSheets As Excel.Sheets = xlBook.Worksheets Dim xlSheet As Excel.Worksheet = CType(xlSheets.Item(1), Excel.Worksheet) Dim x, y As Integer x = 0 y = 0 'データをEXCELファイルに書き出し Dim xlRange As Excel.Range Dim strDat(14, 14) As Object xlRange = xlSheet.Range("A17:O31") 'データの入力セル範囲 Do While x < count strDat(x, 1) = h_date(x).IN_指示納期 strDat(x, 2) = h_date(x).IN_発注番号 strDat(x, 5) = h_date(x).IN_品目名称 strDat(x, 8) = h_date(x).IN_メーカー名 strDat(x, 9) = h_date(x).IN_数量 strDat(x, 10) = h_date(x).IN_単価 strDat(x, 11) = h_date(x).IN_金額 strDat(x, 12) = h_date(x).IN_備考 xlRange.Value = strDat 'セルへデータの入力 x = x + 1 Loop 'System.Runtime.InteropServices.Marshal.ReleaseComObject(strDat) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange) xlBooks.Close() xlApp.DisplayAlerts = False xlApp.Quit() ' COM オブジェクトの解放 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet) ' Excel のプロセス終了 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

  • EXCELプロセスの正常終了に関して

    VB2008で、EXCELにデータを出力するプログラムを作成しています。 その際に、どうしてもタスクマネージャーにプロセスが残ってしまい、なんとかプロセスが残らない ように、様々なホームページから消す方法等を検索し、実際に試してみましたが、未だ残ったままと なってしまいます。 あるホームページには、下記のように"Cell"を使用するとプロセスが残ってしまうと記載が あった為に、それを削除して実行してみましたが、やはり消えないままです。 以下のサンプルプログラムで、何をどうしたらプロセスが正常に消えてくれるでしょうか。。。 教えて頂きたいと思います。 なかなかこのEXCELのプロセスの扱いは理解しにくく、直書きとなって申し訳ありません。 よろしくお願いします。 《サンプルプログラム》 '既存のEXCELファイルを開く Dim excelName As String = "C:\order.xls" '起動時の処理 Dim xlApp As New Excel.Application Dim xlBooks As Excel.Workbook xlApp = New Excel.Application() xlApp.Visible = True ' 確認のためExcelのウィンドウを表示する ' Excelファイルをオープンする xlBooks = DirectCast((xlApp.Workbooks.Open( _ excelName, _ Type.Missing, _ Type.Missing, _ Type.Missing, _ Type.Missing, _ Type.Missing, _ Type.Missing, _ Type.Missing, _ Type.Missing, _ Type.Missing, _ Type.Missing, _ Type.Missing, _ Type.Missing)), _ Excel.Workbook) Dim xlSheets As Excel.Sheets = xlBooks.Worksheets Dim xlSheet As Excel.Worksheet = xlSheets.Item(1) Dim x, y As Integer x = 0 y = 17 'データをEXCELファイルに書き出し Do While x < 1 xlSheet.Cells(10, 1).Value = "TEST" 'こちらの書き方も出来る x = x + 1 y = y + 1 Loop xlBooks.Close(Type.Missing, Type.Missing, Type.Missing) xlApp.Quit() ' COM オブジェクトの解放 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks) ' Excel のプロセス終了 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

  • エクセルの開放

    VB2008でエクセルを操作しているのですが、エクセルのプロセスが残ってしまってどうやって解放すればいいのかわかりません。 サンプルプログラム-------------- Dim xlApp As New Excel.Application Dim xlBooks As Excel.Workbooks = xlApp.Workbooks Dim xlBook As Excel.Workbook = xlBooks.Add Dim xlSheets As Excel.Sheets = xlBook.Worksheets Dim xlSheet As Excel.Worksheet = xlSheets.Item(1) Dim xlobj As Object '開放用 xlobj = xlSheet.Range("A1:C3") xlobj.Value = "TEST" MRComObject(xlobj) MRComObject(xlSheet) MRComObject(xlSheets) xlBook.Close(False) MRComObject(xlBook) MRComObject(xlBooks) xlApp.Quit() MRComObject(xlApp) MRComObjectでCOM オブジェクトへの参照を解放しています。 このプログラムでは特に問題ないのですが、 xlobj = xlApp.Worksheets("Sheet2") xlobj2 = xlobj.Range("A1:C2") xlobj2.Value = "TEST" のようにワークシートを指定すると解放できません。 xlSheet = xlBook.Worksheets("Sheet2") としてもプロセスが残ります。 またVB6.0では可能だった xlApp.Worksheets("Sheet2").Select() のようにワークシートを切り替えるときもVB2008ではプロセスが残ってしまいます。 これはどのようにしたら解決するのでしょうか?

  • VB2008でのExcelオブジェクトの宣言と解放

    下記のようなコードを書きました。 Public Sub XXXXXXXXXXX Dim xlApp As New Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As New Excel.Worksheet try xlApp = CreateObject("Excel.Application") xlApp.Workbooks.Open(Filename:=excelTempPath, UpdateLinks:=0) xlBook = DirectCast((xlApp.Workbooks.Open(excelTempPath)), Excel.Workbook) シートのコピー、削除など操作 xlBook.Close(SaveChanges:=True, Filename:=strExcelPath) xlApp.Quit() Finally MRComObject(xlSheet) MRComObject(xlBook) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) xlApp = Nothing End Try End Sub Public 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 MRComObject(xlBook)でコンパイルの警告が出力されるのですが 回避する方法はないのでしょうか。

  • シートのコピーでプロセスが残ってしまう

    シートのコピーでプロセスが残ってしまう vb2008よりエクセルを起動し、シートを同一のブック内でコピーしようと思うのですが、 下記コードだとシートのコピーはできるのですがリソースが解放できず、プロセスが残ってしまいます。 wsh.Copy(wshh) の部分を wsh.Copy()にして違うブックにコピーすると問題無い。 Excel2003以降の複数のバージョンに対応させるため、CreateObjectを使用しています。 この場合、wsh.copy の部分はどのように記述すれば良いのでしょうか? わかりやすくするため例外等のコードは省いています。 Dim app As Object Dim wbs As Object Dim wb As Object Dim wshs As Object Dim wsh As Object Dim wshh As Object app = CreateObject("Excel.Application") wbs = app.Workbooks wb = wbs.Open("c:\hoge.xls") wshs = wb.Worksheets wsh = wshs.item(1) wshh = wshs.item(1) wsh.Copy(wshh) wb.Close() wbs.close() app.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(wshh) System.Runtime.InteropServices.Marshal.ReleaseComObject(wsh) System.Runtime.InteropServices.Marshal.ReleaseComObject(wshs) System.Runtime.InteropServices.Marshal.ReleaseComObject(wb) System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs) System.Runtime.InteropServices.Marshal.ReleaseComObject(app) wshh = Nothing wsh = Nothing wshs = Nothing wb = Nothing wbs = Nothing app = Nothing

専門家に質問してみよう