关于 VB6/VBA 中 CreateObject() 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/345178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Question about CreateObject() in VB6 / VBA
提问by Shane Miskin
I can do this:
我可以做这个:
Dim fso As New FileSystemObject
or I can do this:
或者我可以这样做:
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
How do I know what string to use for CreateObject? For example, how would I know to use the "Scripting." part of "Scripting.FileSystemObject"? Where do you go to look that up?
我如何知道用于 CreateObject 的字符串?例如,我怎么知道使用“脚本”。“Scripting.FileSystemObject”的一部分?你去哪里查?
回答by huseyint
It is the ProgIDof the component which is registered in Windows registry under HKCR key:
它是在 Windows 注册表中 HKCR 项下注册的组件的ProgID:
HKEY_CLASSES_ROOT\Scripting.FileSystemObject
ProgID's are human readable identifiers for COM objects. They point to the actual CLSIDs, which in this case is:
ProgID 是 COM 对象的人类可读标识符。它们指向实际的 CLSID,在本例中为:
HKEY_CLASSES_ROOT\CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}
This is the place where you can find the actual COM .dll that includes the implementation of the component.
您可以在此处找到包含组件实现的实际 COM .dll。
In the first sample code you have provided you are doing an early-binding, and in the second one you are doing a late-binding.
在您提供的第一个示例代码中,您正在执行早期绑定,而在第二个示例代码中,您正在执行后期绑定。
回答by onedaywhen
Using the VB6 IDE, choose Project, References, then to pick the reference 'Microsoft Scripting Runtime'.
使用 VB6 IDE,选择项目、引用,然后选择引用“Microsoft Scripting Runtime”。
If you didn't know what the reference is called, you could use the References dialog's Browse button to pick the file /system 32/scrrun.dll.
如果您不知道引用的名称,您可以使用“引用”对话框的“浏览”按钮选择文件 /system 32/scrrun.dll。
With the reference chosen, close the References dialog then open the Object Browser (View menu). Change the dropdown to the most likely candidate, being 'Scripting'. This will reveal the library's classes, one of which is 'FileSystemObject'. Hence, you will have discovered the the string required for CreateObject is 'Scripting.FileSystemObject'.
选择参考后,关闭参考对话框,然后打开对象浏览器(查看菜单)。将下拉列表更改为最有可能的候选者,即“脚本”。这将显示库的类,其中之一是“FileSystemObject”。因此,您会发现 CreateObject 所需的字符串是“Scripting.FileSystemObject”。
If you didn't know the Reference name or the file name but you did know the class name then you could search the registry for "FileSystemObject" and it should soon be revealed that the fully-qualified name you require is 'Scripting.FileSystemObject'.
如果您不知道引用名称或文件名,但知道类名,那么您可以在注册表中搜索“FileSystemObject”,很快就会发现您需要的完全限定名称是“Scripting.FileSystemObject” .
回答by Ed.
I would start by searching for FileSystemObject in the MSDN library at http://msdn.microsoft.com/library
我将首先在http://msdn.microsoft.com/library的 MSDN 库中搜索 FileSystemObject
The site is chock full of documentation, including the details of how to call CreateObject.
该站点充满了文档,包括有关如何调用 CreateObject 的详细信息。