views:

202

answers:

1

Hi,

I'm in the process of porting an old excel addin that was writen in VBA to VB .NET. The Excel addin interacts with a number of external com objects. The code sorta looks like this:

Dim hurr as Object
Dim durr as String

hurr = CreateObject("COM Object")
durr = hurr.getString

What I'm trying to do is read the string from the COM object and get it in durr for use later in my program.

That second line results in the exception posted above. If I try casting with CStr/CType I get the same exception. The visual studio watch window reports the type of hurr.getString as a "System.__ComObject" whereas the VBA watch window reports the type as "Variant/Object/String". Microsoft.VisualBasic.Information.TypeName(hurr.getString) says the type is "String". Any ideas how I should go about getting this working?

Thanks!

+1  A: 

This is ridiculous but I figured I would post the answer here for completeness. The solution was to add a pair of brackets to the end of hurr.getString

so the working code looks like this:

Dim hurr as Object
Dim durr as String

hurr = CreateObject("COM Object")
durr = hurr.getString()

The above code gave me the casting exception and for whatever reason it needs brackets to work here. I'm just going to add that working with late binding com objects is horrible and should be avoided at all costs.

FreaknBigPanda