开发者

Conversion from type 'String' to type 'String' is not valid

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开发者_JAVA百科". Microsoft.VisualBasic.Information.TypeName(hurr.getString) says the type is "String". Any ideas how I should go about getting this working?

Thanks!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜