投稿者 neptune  (社会人) 投稿日時 2009/1/19 02:06:22
こんにちは

http://homepage1.nifty.com/MADIA/vb/vb_bbs2/200412/200412_04120059.html
を参考に自分の保存用サンプルを書いてみました。
詳しくは魔界の仮面弁士 さんのコメントを読んで下さい。
要はCellsプロパティの使い方みたいです。

私もまだよく判りませんが、取り合えずプロセスは残りません。
COMを使う場合特にOption Strict Onは要りますね。


Option Strict On
'無関係部分省略 
    Private Sub ExcelTest()
        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
        Dim xlRange As Excel.Range
        Dim xlCells As Excel.Range
        Const csSheetName As String = "Sheet1"
        Const csPath As String = "E:\Data\Office\Excel\vb2008Test.xls"

        'ファイルを削除しておく 
        Try
            System.IO.File.Delete(csPath)
        Catch ex As Exception
            '何もしない 
        End Try

        xlApp = New Excel.Application
        xlBooks = xlApp.Workbooks
        xlBook = xlBooks.Add()
        My.Application.DoEvents()
        xlSheets = xlBook.Worksheets
        xlSheet = DirectCast(xlSheets(csSheetName), Excel.Worksheet)
        xlCells = DirectCast(xlSheet.Cells, Excel.Range)

        'データ入力 
        For i As Integer = 1 To 5
            xlRange = DirectCast(xlCells(i, 1), Excel.Range)
            xlRange.Value = i * 100
            ReleaseComObject(xlRange)
        Next

        xlApp.Visible = True
        xlBook.SaveAs(csPath)
        xlBook.Close(False)             'xlBook を閉じる 

        '開放処理 
        ReleaseComObject(xlCells)        'xlCells の解放 
        xlCells = Nothing
        ReleaseComObject(xlSheet)       'xlSheet の解放 
        xlSheet = Nothing
        ReleaseComObject(xlSheets)      'xlSheets の解放 
        xlSheets = Nothing
        ReleaseComObject(xlBook)        'xlBook の解放 
        xlBook = Nothing
        ReleaseComObject(xlBooks)       'xlBooks の解放 
        xlBooks = Nothing

        xlApp.Quit()                    'Excel終了  
        ReleaseComObject(xlApp)         'xlApp を解放 
        xlApp = Nothing
    End Sub


#これでまともに動けば大丈夫ですよね?
おかしい所があれば教えてくださいね。