LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default System.Runtime.InteropServices.COMException (0x80020005) in excel 2k7, but worked well in 2k3


'-----------------------------------------------------------------------
option strict off
imports System
imports System.Reflection ' For Missing.Value and BindingFlags
imports System.Runtime.InteropServices ' For COMException
Imports Microsoft.Office.Interop.Excel

class AutoExcel
public shared sub Main()

Console.WriteLine ("Creating new Excel Application")
Dim app As Application
app = New Application()
if app is nothing then
Console.WriteLine("ERROR: EXCEL couldn't be started!")
environment.exitcode = 0
exit sub
end if

Console.WriteLine ("Making application visible")
app.Visible = true

Console.WriteLine ("Getting the workbooks collection")
Dim workbooks As Workbooks
workbooks = app.Workbooks

Console.WriteLine ("Adding a new workbook")
Dim workbook As _Workbook
workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet)

Console.WriteLine ("Getting the worksheets collection")
Dim sheets As Sheets
sheets = workbook.Worksheets

Dim worksheet As _Worksheet
worksheet = sheets.Item(1)
if worksheet is nothing then
Console.WriteLine ("ERROR: worksheet == null")
end if

Console.WriteLine ("Setting the value for cell")
dim range as Object 'Range
range = worksheet.Range("A10", Missing.Value)
if range is nothing then
Console.WriteLine ("ERROR: range == null")
end if
range.Value2 = 5

' This paragraph sends single dimension array to Excel
Dim range2 as Object
range2 = worksheet.Range("A1", "E1")
Dim array2(5) as Integer
Dim i As Integer
For i = 0 To 4
array2(i) = i+1
Next i
range2.Value2 = array2

' This paragraph sends two dimension array to Excel
Dim range3 as Object
range3 = worksheet.Range("A2", "E3")
Dim array3(2, 5) as Integer
Dim j As Integer
For i = 0 To 1
For j = 0 To 4
array3(i, j) = i*10+j
Next j
Next i
range3.Value2 = array3

' This paragraph reads two dimension array from Excel
Dim range4 as Object
range4 = worksheet.Range("A2", "E3")
Dim array4(,) as Object
array4 = range4.Value2

For i = array4.GetLowerBound(0) To array4.GetUpperBound(0)
For j = array4.GetLowerBound(1) To array4.GetUpperBound(1)
if CDbl(array4(i, j)) < array3(i-1, j-1) then
environment.exitcode = 0
Console.WriteLine ("ERROR: Comparison FAILED!")
exit sub
end if
Next j
Next i

' This paragraph sends two dimension array to Excel
Dim range5 as Object
range5 = worksheet.Range("A5", "J6")
Dim array5(2, 10) as Double
For j = 0 To array5.GetUpperBound(1)
Dim arg as Double
arg = Math.PI/array5.GetLength(1) * j
array5(0, j) = Math.Sin(arg)
array5(1, j) = Math.Cos(arg)
Next j
range5.Value2 = array5

' The following code draws the chart

range5.Select

Dim chartobjects As Object
chartobjects = worksheet.ChartObjects(Missing.Value)

Dim chartobject As Object
chartobject = chartobjects.Add(10, 100, 450, 250)

Dim chart As Object
chart = chartobject.Chart

chart.ChartWizard(range5, XlChartType.xl3DColumn,
Missing.Value, XlRowCol.xlRows, 0, 0, True, "Sample Chart", _
"Sample Category Type", "Sample Value Type")

Console.WriteLine("Press ENTER to finish the sample:")
Console.ReadLine()

Try
' If user interacted with Excel it will not close when the
app object is destroyed, so we close it explicitely
workbook.Saved = True
app.UserControl = False
app.Quit()
Catch Outer As COMException
Console.WriteLine("User closed Excel manually, so we don't
have to do that")
End Try

Environment.ExitCode = 100
Console.WriteLine("Sample successfully finished!")
End Sub
end class


Copy it and run it. The line "chart.ChartWizard...." worked well in
xpsp2, office2003, but threw an exception under office2007.

Unhandled Exception: System.Runtime.InteropServices.COMException
(0x80020005): T
ype mismatch. (Exception from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))
at
Microsoft.VisualBasic.CompilerServices.LateBinding .InternalLateCall(Object
o, Type objType, String name, Object[] args, String[] paramnames,
Boolean[] Cop
yBack, Boolean IgnoreReturn)
at
Microsoft.VisualBasic.CompilerServices.NewLateBind ing.LateCall(Object
Inst
ance, Type Type, String MemberName, Object[] Arguments, String[]
ArgumentNames,
Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
at AutoExcel.AutoExcel.Main()

It's wierd, and why I got this low-level exception?
Thx advance.

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I open an Excel file on XP system, saved on a Vista system JLS7 Excel Discussion (Misc queries) 3 December 2nd 08 04:21 AM
Sheet.columns(aa:zz) ,COMException (0x80020005) VSTO Formatting is chan Excel Discussion (Misc queries) 0 October 2nd 08 10:05 PM
COMException With Microsoft.Office.Interop.Excel frankvfox Excel Programming 4 September 27th 06 04:36 PM
Excel SaveCopyAs COMException 0x800A03EC Jim McLeod Excel Programming 0 May 25th 04 12:26 AM
Calling from C#.Net App Office._CommandBarButton.Execute() method in Excel 2003 throws a COMException Jacek Excel Programming 1 December 21st 03 03:48 PM


All times are GMT +1. The time now is 11:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"