Freigeben über


A Beta 2 VSTO Issue you may encounter in VB.NET: Me.Range("A1") throws a COMException

In Beta2 we shipped with an unforunate bug that will affect VB.NET users.

If you write code like this in a worksheet project item, it will fail with a COMException at runtime: 

MsgBox(Me.Range("A1").Value)

The problem is that when you wrote the code Range("A1") you are actually omitting a second optional parameter that the Range property takes. In VSTO Beta2, we are passing that second omitted optional parameter incorrectly to Excel. This issue potentially affects any the following parameterized properties on objects in the Microsoft.Office.Tools.Excel namespace that have optional parameters:

Worksheet.Range
Chart.HasAxis
ChartSheet.HasAxis
NamedRange.AddressLocal
NamedRange.Characters
XmlMappedRange.AddressLocal
XmlMappedRange.Characters
Workbook.Colors
Bookmark.XML
XmlNode.ValidationErrorText
NamedRange.Value
XmlMappedRange.Value

There are three workarounds. One--specify all the parameters:

MsgBox(Me.Range("A1", "A1" ).Value)

Workaround two is useful if you still don't want to specify an optional parameter--pass System.Type.Missing to the optional parameter:

MsgBox(Me.Range("A1", System.Type.Missing).Value)

Workaround three is to call the "InnerObject" property which effectively "unwraps" a VSTO object to give you the underlying PIA:

MsgBox(Me.InnerObject.Range("A1").Value)