VB2013でexcel操作するプログラムを作成
実行するとタスクマネージャーにexeclが残ってしまいます。
ネット上に書いてあった解放は試したのですが残ってしまいました。
どこが間違っているか、わかる方がいれば
教えてください。
VB.net
1 Dim fname As String = "" 2 Dim fname2 As String = "" 3 4 For i As Integer = 17 To mCsvList1.GetUpperBound(0) 5 Dim xlApp0 As New Excel.Application 6 Dim xlBooks0 As Excel.Workbooks = xlApp0.Workbooks 7 Dim xlBook0 As Excel.Workbook = xlBooks0.Open(moto_xls) 8 Dim xlSheet0 As Excel.Worksheet = xlApp0.Worksheets("従来") 9 Dim xlSheet2 As Excel.Worksheet = xlApp0.Worksheets("小型") 10 11 xlApp0.Visible = True 12 xlSheet0.Select() 13 xlSheet0.Range("A2").Select() 14 xlSheet0.Range("A2").Activate() 15 16 fname = mCsvList1(i).Substring(0, mCsvList1(i).Length - 4) 17 Dim csvApp1 As New Excel.Application 18 Dim csvBooks1 As Excel.Workbooks = csvApp1.Workbooks 19 Dim csvBook1 As Excel.Workbook = csvBooks1.Open(csvPath1 & "\" & mCsvList1(i)) 20 Dim csvSheet1 As Excel.Worksheet = csvApp1.Worksheets(fname) 21 22 fname2 = mCsvList2(i).Substring(0, mCsvList2(i).Length - 4) 23 Dim csvApp2 As New Excel.Application 24 Dim csvBooks2 As Excel.Workbooks = csvApp2.Workbooks 25 Dim csvBook2 As Excel.Workbook = csvBooks2.Open(csvPath2 & "\" & mCsvList2(i)) 26 Dim csvSheet2 As Excel.Worksheet = csvApp2.Worksheets(fname2) 27 28 xlApp0.Visible = True 29 csvApp1.Visible = True 30 csvApp2.Visible = True 31 32 csvSheet1.Select() 33 Dim xlRange As Excel.Range = csvSheet1.Cells.Range("A2:R9000") 34 xlRange.Copy() 35 Dim xlPasteRange As Excel.Range = xlSheet0.Range("A2") 36 xlSheet0.Paste(xlPasteRange) 37 'csvApp1.CutCopyMode = False 38 '* 件数 39 Dim cnt As Integer = xlSheet0.Range("X3").Value 40 xlRange = xlSheet0.Range("W2") 41 xlRange.Copy() 42 xlPasteRange = xlSheet0.Cells.Range("W3:W" & Format("#0", cnt)) 43 xlSheet0.Paste(xlPasteRange) 44 csvApp1.CutCopyMode = False 45 46 xlSheet2.Select() 47 xlSheet2.Range("A2").Select() 48 xlSheet2.Range("A2").Activate() 49 50 csvSheet2.Select() 51 Dim xlRange2 As Excel.Range = csvSheet2.Cells.Range("A2:R9000") 52 xlRange2.Copy() 53 Dim xlPasteRange2 As Excel.Range = xlSheet2.Range("A2") 54 xlSheet2.Paste(xlPasteRange2) 55 'csvApp2.CutCopyMode = False 56 '* 件数 57 cnt = xlSheet2.Range("X3").Value 58 xlRange = xlSheet2.Range("W2") 59 xlRange.Copy() 60 xlPasteRange = xlSheet2.Cells.Range("W3:W" & Format("#0", cnt)) 61 xlSheet2.Paste(xlPasteRange) 62 csvApp2.CutCopyMode = False 63 64 xlApp0.CutCopyMode = False 65 '* 保存処理 66 xlBook0.SaveAs(mxlsPath & "\" & fname & ".xlsx") 67 MRComObject(xlPasteRange) 68 MRComObject(xlRange) 69 MRComObject(xlPasteRange2) 70 MRComObject(xlRange2) 71 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlPasteRange) 72 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlRange) 73 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlPasteRange2) 74 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlRange2) 75 76 csvBook2.Close() 77 csvBooks2.Close() 78 MRComObject(csvSheet2) 79 MRComObject(csvBook2) 80 MRComObject(csvBooks2) 81 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(csvSheet2) 82 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(csvBook2) 83 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(csvBooks2) 84 csvApp2.Quit() 85 MRComObject(csvApp2) 86 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(csvApp2) 87 csvSheet2 = Nothing 88 csvBook2 = Nothing 89 csvBooks2 = Nothing 90 csvApp2 = Nothing 91 92 csvBook1.Close() 93 csvBooks1.Close() 94 MRComObject(csvSheet1) 95 MRComObject(csvBook1) 96 MRComObject(csvBooks1) 97 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(csvSheet1) 98 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(csvBook1) 99 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(csvBooks1) 100 csvApp1.Quit() 101 MRComObject(csvApp1) 102 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(csvApp1) 103 csvSheet1 = Nothing 104 csvBook1 = Nothing 105 csvBooks1 = Nothing 106 csvApp1 = Nothing 107 108 xlBook0.Close() 109 xlBooks0.Close() 110 MRComObject(xlSheet2) 111 MRComObject(xlSheet0) 112 MRComObject(xlBook0) 113 MRComObject(xlBooks0) 114 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlSheet2) 115 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlSheet0) 116 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlBook0) 117 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlBooks0) 118 xlApp0.Quit() 119 MRComObject(xlApp0) 120 'Call System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp0) 121 xlSheet2 = Nothing 122 xlSheet0 = Nothing 123 xlBook0 = Nothing 124 xlBooks0 = Nothing 125 xlApp0 = Nothing 126 127 Next 128 End Sub
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/05/12 00:18 編集