
Need help doing a Subtotal through Excel automation in vb.net
Hi, all.
I'm hoping someone out there has done this and can tell me what I'm
doing wrong. I'm trying to add subtotals to an Excel sheet through
automation in vb.net. I can get it to work perfectly in VB 6, but
when I try a similar call in .Net, I get the error:
"An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
microsoft.visualbasic.dll
Additional information: Subtotal method of Range class failed"
I'm developing on a Windows 2000 machine and am using Excel 9.0.
The code I'm using looks like this:
-------
range = wks.Range("A1", GetRangeLetter(lColumnCount) & lRowCount + 1)
range.Value2 = asRowString
range2 = wks.Range("A1", GetRangeLetter(lColumnCount) & 1)
range2.BorderAround(, Excel.XlBorderWeight.xlThin,
Excel.XlColorIndex.xlColorIndexAutomatic)
If g_iGroupBy <> -1 Then
range.Subtotal(g_iGroupBy, Excel.XlConsolidationFunction.xlSum,
g_aiSubTotal, True, False,
Excel.XlSummaryRow.xlSummaryBelow)
End If
-------
In last line, g_iGroupBy is a valid integer and g_aiSubTotal is a
valid array of integers. The range2 stuff is there so that Excel
knows what the column headers are. If you remove that, you get the
standard Excel message telling you that it doesn't know what the
column headers are and to hit OK to use the top row. If you hit OK,
you then get the same error message I posted above.
I've also tried this as a Selection instead of a Range and got the
same result.
Any ideas?
Thanks!
-Manny Wolfe
Four Rivers Software Systems, Inc.