2016-06-17 5 views
0

Excelのスプレッドシートの特定の範囲に送信しようとしているVB.netのデータテーブルがあります。以下のサブのVB.NETのデータテーブルをExcelに転送する方法

An exception of type 'System.Runtime.InteropServices.COMException' occurred in MeasurementFinder.dll but was not handled in user code

Additional information: Exception from HRESULT: 0x800A03EC

エラー警告:

Private Sub WriteDataTableToRng(targetWs As Excel.Worksheet, anchor As Excel.Range, tbl As System.Data.DataTable) 
    'This sub writes the given tbl to the targetWs as a range with its top left cell acting as anchor 
    Dim wRange As Excel.Range = anchor 'wRange = write range. This range represents the cell being written to over every iteration 

    For Each colm As DataColumn In tbl.Columns 'This loop writes the column names into the target ws 
     targetWs.Range(wRange).Value2 = colm.ColumnName '**THIS LINE IS CALLED OUT BY THE ERROR 
     wRange = wRange.Offset(0, 1) 
    Next colm 
    wRange = anchor.Offset(1, 0) 
    For Each row As DataRow In tbl.Rows 
     For Each col As DataColumn In tbl.Columns 
      targetWs.Range(wRange).Value2 = tbl.Rows.Item(tbl.Rows.IndexOf(row)).Item(tbl.Columns.IndexOf(col)) '**THIS LINE IS CALLED OUT BY THE SAME ERROR IF THE PREVIOUS LOOP IS COMMENTED OUT 
     Next col 
    Next row 
End Sub 

前のものである呼び出すサブ:しかし、プログラムを実行している時に、私はエラーを取得する

Private Sub ReportOnTube(TubeID As Integer) 
    'This sub creates an Excel workbook that acts as a report on a tube, given its ID 
    'The report has a worksheet for each measurement tied to the tube (From the Gauge DB) 

    'Verify the tube is in the DB 
    Dim TubeExists As Boolean 
    TubeExists = VerifyTube(TubeID) 
    If TubeExists Then 
     'Create a new excel workbook and name/time stamp it 
     Dim wb As Excel.Workbook = Me.Application.Workbooks.Add() 
     wb.SaveAs("C:\Gauge Reports\Tube " & TubeID & System.DateTime.Now.ToString(" HH_mm_ss dd-MM-yyyy")) 
     'Add a worksheet for each measurement tied to the tube 
     Dim ws As Excel.Worksheet 
     ws = wb.Worksheets.Add 
     Dim aRng As Range 
     aRng = ws.Range("B2") 
     TubesConn.Close() 
     TubesConn.Open() 
     Dim selectTbl As New SqlCommand("SELECT * FROM [Tubes]", TubesConn) 
     Dim rdr As SqlDataReader 
     Dim aTbl As New System.Data.DataTable 
     rdr = selectTbl.ExecuteReader() 
     aTbl.Load(rdr) 
     Call WriteDataTableToRng(ws, aRng, aTbl) 
     TubesConn.Close() 
    End If 
End Sub 

I以下の輸入品を使用しています:

Imports System.Data.Sql 
Imports System.Data.SqlClient 
Imports System.Data 
Imports System.IO 
Imports Microsoft.Office.Interop.Excel 

私がしようとしているのは、与えられたデータテーブルを反復して、テーブルの値をスプレッドシート内の "アンカー"範囲変数の左上隅の範囲に書き込むことです。私はVisual StuioのIntelliSenseからの警告はないので、どこから始めるべきかはわかりません。

ありがとうございます!

+0

問題のこの種を追跡するとき、デバッガが極めて有用です。問題のコードをステップ実行すると、どのようなことがわかりますか? –

答えて

1

targetWs.Range(wRange).Value2 = colm.ColumnNameは冗長/間違っています。 wRangeに格納されているExcelオブジェクトは、すでに含まれているワークシートのプロパティです。

つまり、wRange.Parent.Nameが印刷されている場合は、その範囲内のワークシートが表示されます。2つの異なるワークシートの範囲を指定することはできません。試したが、誰が、とにかく、あなたはおそらくそれを行うことができないだろう決して... </streamOfConciousness>

代わりに、ちょうど使用:

wrange.value = colm.columnName 
関連する問題