从“String”类型转换输入“字符串”无效
我正在将用 VBA 编写的旧 Excel 插件移植到 VB .NET。 Excel 插件与许多外部 com 对象进行交互。代码看起来像这样:
Dim hurr as Object
Dim durr as String
hurr = CreateObject("COM Object")
durr = hurr.getString
我想做的是从 COM 对象读取字符串并将其放入 durr 中以供稍后在我的程序中使用。
第二行导致上面发布的异常。如果我尝试使用 CStr/CType 进行转换,我会得到相同的异常。 Visual Studio 监视窗口将 hurr.getString 的类型报告为“System.__ComObject”,而 VBA 监视窗口将类型报告为“Variant/Object/String”。 Microsoft.VisualBasic.Information.TypeName(hurr.getString) 表示类型为“String”。我有什么想法应该如何让它发挥作用吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很荒谬,但我想为了完整性我应该在这里发布答案。解决方案是在 hurr.getString 的末尾添加一对括号,
这样工作代码如下所示:
上面的代码给了我转换异常,并且无论出于何种原因它需要括号在这里工作。我只是想补充一点,使用后期绑定 com 对象是很糟糕的,应该不惜一切代价避免。
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:
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.