从“String”类型转换输入“字符串”无效

发布于 2024-08-15 19:05:57 字数 513 浏览 7 评论 0原文

我正在将用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-08-22 19:05:57

这很荒谬,但我想为了完整性我应该在这里发布答案。解决方案是在 hurr.getString 的末尾添加一对括号,

这样工作代码如下所示:

Dim hurr as Object
Dim durr as String

hurr = CreateObject("COM Object")
durr = 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文