VBA.Strings 模块中的函数在 VBA 内部处理它们的方式上是“特殊的”。对于它们中的大多数,类型库中实际上有 2 个版本 - 一个返回 String(以 $ 结尾)的版本,以及一个返回 Variant 的版本。这些在内部被声明为一对函数 - 例如,Right(来自 vbe7.dll 类型库):
[entry(618), helpcontext(0x000f6ea5)]
BSTR _stdcall _B_str_Right(
[in] BSTR String,
[in] long Length);
[entry(619), helpcontext(0x000f656e)]
VARIANT _stdcall _B_var_Right(
[in] VARIANT* String,
[in] long Length);
编译器显然在内部将$ 视为“类型提示”,因为(使用上面的示例)实际上没有函数@ 987654328@ 在 TypeLib 中定义。事实上,也没有声明为VBA.Strings.Right 的函数。它们存在于名为_HiddenInterface 的特殊受限界面中:
[
odl,
uuid(1E196B20-1F3C-1069-996B-00DD010EF676)
]
interface _HiddenInterface {
...
[restricted, helpcontext(0x000f6d7c)]
void _stdcall Right();
...
};
请注意,Right$ 不会出现在 _HiddenInterface 中,也不会出现任何其他字符串返回函数。 VBA 编译器使用“函数类型提示”将函数调用转发到_B_str_Right 或_B_var_Right。
现在您可能想知道这与您的问题有什么关系。答案是Replace 实际上没有 有两种不同的内部表示。它总是返回一个字符串,在_HiddenInterface 上不存在,直接存在于VBA.Strings 模块中:
[entry(712), helpstring("Find and replace a substring within a string"), helpcontext(0x000f6522)]
BSTR _stdcall Replace(
[in] BSTR Expression,
[in] BSTR Find,
[in] BSTR Replace,
[in, optional, defaultvalue(1)] long Start,
[in, optional, defaultvalue(-1)] long Count,
[in, optional, defaultvalue(0),
custom(270D72B0-FFB8-11CF-A4BD-00A0C90F26EE, 1)
] VbCompareMethod Compare);
基本上,根本没有Replace$ 函数。 VBA 将$ 视为返回值 的类型提示(无论如何它始终是String)。就 ODBC 和 OLE 驱动程序而言,我想(TBH 我真的没有仔细研究过)它们仅限于由 TypeLib 公开的名称并且不解释由 VBA 运行时转发给不同的函数。如果您在 vbe7.dll 上进行 IDispatch 查找,它们根本就不存在。