- - PR -
Excelシートを2度コピーでエラー
1
投稿者 | 投稿内容 |
---|---|
|
投稿日時: 2007-02-20 18:31
お世話になります。
題の通りですが、VB.NetでExcel上のシートコピーを2度行うと、エラーとなってしまいます。 1度のコピーは正常に処理できますし、セルへの記入や罫線等の処理も正常に行う事が出来ます。 シートコピーの2度目で必ずエラーとなるので、メモリの解放が出来ていないのかと調べましたが、 結論が出ずに行き詰ってしまいました。 どなたか、お力添えをよろしくお願いします。 -------------------------------------------------------- 【環境】 OS : Windows2000Server SP4 Soft : Excel2000 SP3 Visual Studio .Net 2003 .Net FrameWork 1.1 【ソースの流れ】 @Excelを開く(シートが4つあるExcel 今回は仮に[a][b][c][d]とします。) A[b][c]シートを消す(今回使用したいのは[a][d]の2シートのみ) B残った[a][d]シートを複製 ※最終的に[a (2)][d (2)][a][d]というシート状況を実現したいです。 【ソース】 Private oExcel As Excel.Application Private oBooks As Excel.WorkBooks Private oBook As Excel.WorkBook Private oSheets As Excel.Sheets Private oSheet As Excel._Worksheet 'コピー用 Private xlSheets_Base As Excel.Sheets Private xlSheets_From As Object Private xlSheets_FromS As Excel.Worksheet Private xlSheets_To As Object Private xlSheets_ToS As Excel.Worksheet Private xlNewSheet As Object '■Excel接続部(関数) oExcel = nothing oBooks = nothing oBook = nothing oSheets = nothing oSheet = nothing Try oExcel = New Excel.Application oBooks = oExcel.Workbooks oBook = oBooks.Open([Excelファイルのパス]) oSheets = oBook.Worksheets oSheet = oSheets([a]) Catch 'エラー処理 End Try '■シート削除(関数) Try oSheets([b]).Delete oSheets([c]).Delete Catch 'エラー処理 End Try '■シートコピー(関数で2度呼び出し) xlSheets_Base = Nothing xlSheets_From = Nothing xlSheets_FromS = Nothing xlSheets_To = Nothing xlSheets_ToS = Nothing xlNewSheet = Nothing Try xlSheets_Base = oBook.Worksheets xlSheets_From = DirectCast(xlSheets_Base.Item(※intFromSheetNumber),ExcelWorksheet) xlSheets_FromS = DirectCast(xlSheets_From,Excel.Worksheet) xlSheets_To = DirectCast(xlSheets_Base.Item(※intToSheetNumber),ExcelWorksheet) xlSheets_ToS = DirectCast(xlSheets_To,Excel.Worksheet) xlNewSheet = xlSheets_From.Copy(after:=xlSheets_To) Catch 'エラー処理 Finally System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets_ToS) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets_To) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets_FromS) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets_From) System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets_Base) End Try ...(以下略) ※intFromSheetNumber => コピー元のシート番号 1度目は1、2度目は3が入ります。 ※intToSheetNumber => コピー先のシート番号 1度目は1、2度目は2が入ります。 |
|
投稿日時: 2007-02-27 10:17
こんにちわ。
エラーがでているとなっていますが、どのようなエラーがでていますか? |
1