用字符串变量替换字符串-错误91 [英] Replace string with string variable - Error 91

查看:97
本文介绍了用字符串变量替换字符串-错误91的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

Background

最近我回答了一个问题,该问题涉及查看文件的属性.最终,我提出的代码运行良好,但是有一点让我感到困惑.

Recently I answered a question which involved looking at a file's properties. Eventually the code I put up worked fine, but there is one thing about it that got me puzzled.

问题

Problem

在两行中,我想用一个变量替换一个字符串(在我看来是这样),更具体地说,请尝试以下操作:

There are two specific lines where I wanted to replace a (to me what looks like) a string, with a variable, more specifically, try the following:

Sub TestForSO()

Dim oDir As Object: Set oDir = CreateObject("Shell.Application").Namespace("C:\Users\...\")
Debug.Print oDir.GetDetailsOf(oDir.Items, 1)

End Sub

将路径名替换为包含excel文件的目录,它应该可以正确返回属性值.

Replace the pathname to a directory which includes an excel file, and it should return the property value just fine.

现在,当我尝试用变量替换完整路径时,以下内容会在debug.print行上引发运行时错误91:对象变量或未设置块变量":

Now when I try to replace the full path with a variable the following throws an "Runtime Error 91: Object variable or with block variable not set" on the debug.print line:

Sub TestForSO()

Dim MainPath As String: MainPath = "C:\Users\...\"
Dim oDir As Object: Set oDir = CreateObject("Shell.Application").Namespace(MainPath)
Debug.Print oDir.GetDetailsOf(oDir.Items, 1)

End Sub

解决方案

Solution

以下内容确实有效,这对我来说有点奇怪:

A bit peculiar to me that the following did work:

Sub TestForSO()

Dim MainPath As String: MainPath = "C:\Users\...\"
Dim oDir As Object: Set oDir = CreateObject("Shell.Application").Namespace(CStr(MainPath))
Debug.Print oDir.GetDetailsOf(oDir.Items, 1)

End Sub

我不理解其中的区别,因为下面的代码将通过手表"给出相同的结果:

I do not understand the difference per se as the code below will give the same result through "Watches":

Sub test()

Dim check1 As String, check2 As String

check1 = "Hello"
check2 = CStr("Hello")

End Sub

问题

Question

有人知道为什么字符串变量本身不够用并会引发错误吗?为何看起来好像是相同的数据类型,为什么要添加Cstr()来使代码正常工作?

Does somebody understand why the string variable on itself was not enough and would throw an error? Why would adding Cstr() make the code work when seemingly it's the same data type?

推荐答案

根据有关

According to documentation about Namespace, it needs a parameter that must be a Variant or can be a string that specifies the path of the folder.

这说明了为什么这两种方法都可以正常工作:

That explains why these 2 methods work with no problems:

Set oDir = CreateObject("Shell.Application").Namespace("C:\Users\...\ 'string path

或定义一个Variant变量:

Or defining a Variant variable:

Dim MainPath As Variant: MainPath = "C:\Users\...\"
Dim oDir As Object: Set oDir = CreateObject("Shell.Application").Namespace(CStr(MainPath))

但是将MainPath定义为字符串会导致错误运行时错误91:对象变量或未设置块变量

But defining MainPath as string causes error Runtime Error 91: Object variable or with block variable not set

OP找到了解决方案.如果MainPath声明为字符串,并与

OP found a solution. If MainPath declared as string, and combined with Cstr, the code works.

这只是一种理论,但是一些非官方的消息(与VBA没有直接关系)提到Cstr将值转换为带有子类型的变体.

It's just a theory, but some unnoficial sources (not directly related to VBA) mention that Cstr converts the value to a variant with a subtype.

http://www.csidata.com/custserv/onlinehelp/vbsdocs /vbs89.htm https://docs.oracle.com/cd/E57185_01/HFMAD/ch10s06s04s03. html

实际上,

Actually, the official documentation it's kind of confusing, because at first lines it says:

每个函数都将表达式强制转换为特定的数据类型.

然后说

函数名称确定返回类型

但是,如果我们仔细阅读,还会发现一些重要信息,例如:

But if we read carefully, there is also some important information like this:

"... 通常,您可以使用数据类型来记录代码 转换功能以显示某些操作的结果 应该表示为特定的数据类型,而不是 默认数据类型 ..."

"...In general, you can document your code using the data-type conversion functions to show that the result of some operation should be expressed as a particular data type rather than the default data type..."

还有:

"... 此技术与所有其他转换相同 内部类型为其等效的变体子类型 ..."

"...This technique is consistent with the conversion of all other intrinsic types to their equivalent Variant subtypes..."

因此,在过去的24小时内进行了一些研究和思考之后,并多次阅读了我之前发表的文章,我敢说所有转换函数都返回带有子类型的Variant.在这种情况下,CStr确实会返回一个变体,该变体被强制表示为字符串,它是字符串的子类型,但是数据是变体.

So after doing some research and thinking about it in the last 24 hours, and reading a lot of times the previous paragraphs I've posted, I would dare to say that all conversion functions returns a Variant with a subtype. In this case, CStr does return a Variant that is being coerced to be expressed as string being string the subtype, but the data is Variant.

这将解释为什么执行Cstr(MainPath)使代码有效.

That would explain why doing Cstr(MainPath) makes the code works.

这篇关于用字符串变量替换字符串-错误91的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆