VBA 创建对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21609975/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:47:43  来源:igfitidea点击:

VBA CreateObject

excelvbaexcel-vbacreateobject

提问by ExoticBirdsMerchant

I am stranded on this code line since 10th of January where i got it in an email and i found out i had to learn class modules so i did and returned to ask on a new basis now. The code line is (Critical Warning: you have to go to Tools--> References in VBE and activate the Microsoft WinHTTP Services, version 5.1with Early Binding):

自 1 月 10 日以来,我一直被困在此代码行上,我在电子邮件中收到了它,我发现我必须学习课程模块,所以我学习了,现在又回来询问新的基础。代码行是(严重警告:您必须在 VBE 中转到“工具”-->“参考”并激活Microsoft WinHTTP 服务,具有早期绑定的5.1 版):

Dim WinHttpReq As Object

Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

For CreateObject I go to MS Help and says: Creates and returns a reference of an ActiveX object

对于 CreateObject,我转到 MS 帮助并说:创建并返回 ActiveX 对象的引用

Now for all i know, when we create a reference it is for an object only and done like this (please correct me if i am wrong):

现在就我所知,当我们创建一个引用时,它只针对一个对象,并且是这样完成的(如果我错了,请纠正我):

Dim ThatIKnow as Workbook

then we instantiate it like this

然后我们像这样实例化它

Set ThatIKnow = Workbooks.Add

Why we need CreateObject?

为什么我们需要 CreateObject?

Help continues by saying "...of an ActiveX object"

帮助继续说“ ...的 ActiveX 对象

And if I go to click ActiveX on the help it points out the glossary that says: An object that is exposed to other applications or programming tools through Automation interfaces

如果我点击帮助上的 ActiveX,它会指出术语表,上面写着: 通过自动化接口暴露给其他应用程序或编程工具的对象

And this line absolutely tells me nothing. All I knew about ActiveX is this

这条线绝对告诉我什么。我对 ActiveX 的了解仅此而已

enter image description here

在此处输入图片说明

...which i think are the ActiveX controls... (I must admit though ActiveX was always a foggy term for me)

...我认为是 ActiveX 控件...(我必须承认 ActiveX 对我来说总是一个模糊的术语)

Now inside the CreateObject("WinHttp.WinHttpRequest.5.1") i have scoured the Web and i cannot find some decent MS help for the WinHttp object and what it does. Anyway the Object Browser has it as library but the F1 help button shows up nothing. So the Object Browser says it's a Library, i have found it in the Web called as WinHttp Reference and also as a WinHttp Object. What is it from all these?

现在在 CreateObject( "WinHttp.WinHttpRequest.5.1") 中,我已经搜索了 Web,但我找不到 WinHttp 对象及其功能的一些体面的 MS 帮助。无论如何,对象浏览器将其作为库,但 F1 帮助按钮什么也没显示。所以对象浏览器说它是一个库,我在 Web 上发现它被称为 WinHttp 引用,也被称为 WinHttp 对象。所有这些是什么?

And for the love of God why it is called "5.1"? i didn't found anywhere a WinHttpRequest.5.1 term

而对于上帝的爱,为什么它被称为“ 5.1”?我没有在任何地方找到 WinHttpRequest.5.1 术语

i am not asking for chewed up food but any effort to crack the ainigma really tightens the whole situation more. Please any pinch that could help me crack this line of code will be tones of help

我不是要求咀嚼食物,但任何破解 ainigma 的努力确实会使整个情况更加紧张。请任何可以帮助我破解这行代码的捏将是帮助的语气

thanks for watching my question

谢谢你看我的问题

回答by Siddharth Rout

I will not cover the difference between Early Binding and Late Binding. You can read about them in this KB Article

我不会介绍早期绑定和后期绑定之间的区别。您可以在这篇知识库文章中了解它们

What I will do however is answer all your little questions that you have in your question such as

但是,我要做的是回答您在问题中提出的所有小问题,例如

  1. What is CreateObject?
  2. What is an ActiveX Control?
  3. Creates and returning a reference of an ActiveX object - Meaning
  4. And for the love of God why it is called "5.1"?
  1. 什么是创建对象?
  2. 什么是 ActiveX 控件?
  3. 创建并返回 ActiveX 对象的引用 - 含义
  4. 而对于上帝的爱,为什么它被称为“5.1”?


What is CreateObject?

什么是创建对象?



As I mentioned in the comment above CreateObjectis a function which is used in Visual Basic (vb6 and vb.net), Visual Basic for Applications (VBA) and VBScript to dynamically create an instance of an ActiveX control or COM object.

正如我在上面的评论中提到的,这CreateObject是一个在 Visual Basic(vb6 和 vb.net)、Visual Basic for Applications (VBA) 和 VBScript 中使用的函数,用于动态创建 ActiveX 控件或 COM 对象的实例。



What is an ActiveX Control?

什么是 ActiveX 控件?



An ActiveX control is a component program object which can be re-used by numerous application programs. The main technology for creating ActiveX controls based on Component Object Model (COM). In general, ActiveX controls replace the earlier OCX (Object Linking and Embedding custom controls).

ActiveX 控件是一个组件程序对象,可以被众多应用程序重新使用。基于Component Object Model (COM). 通常,ActiveX 控件取代了早期的 OCX(对象链接和嵌入自定义控件)。

An ActiveX control can be created in any programming language that recognizes Microsoft's Component Object Model for example Visual Basic and C++

可以使用任何识别 Microsoft 组件对象模型的编程语言创建 ActiveX 控件,例如 Visual Basic 和 C++

These ActiveX control runs in what is known as a container, for example MS Excel, which uses the Component Object Model program interfaces. In fact this actually helps us all. Imagine writing the code for these controls from scratch every time!

这些 ActiveX 控件在所谓的容器中运行,例如 MS Excel,它使用组件对象模型程序接口。事实上,这实际上对我们所有人都有帮助。想象一下每次都从头开始为这些控件编写代码!



Creates and returning a reference of an ActiveX object - Meaning

创建并返回 ActiveX 对象的引用 - 含义



Let me explain it in reference to what you quoted.

让我参考你引用的内容来解释一下。

Dim ThatIKnow as Workbook

Set ThatIKnow = Workbooks.Add

What we are doing by Dim ThatIKnow as Workbookis telling the runtime environment that we will create an object of type "Workbook" and reference it as "ThatIKnow" in our code. However this actually doesnt create the object neither does it allocate any memory. The memory is allocated only when the object is created using the Newkeyword or any other way such as Createobjectand assiged to this variable ThatIKnow

我们所做的Dim ThatIKnow as Workbook是告诉运行时环境我们将创建一个类型为“Workbook”的对象,并在我们的代码中将其引用为“ThatIKnow”。然而,这实际上并没有创建对象,也没有分配任何内存。只有在使用New关键字或任何其他方式(例如Createobject和 分配给此变量)创建对象时才分配内存ThatIKnow

So when we say Set ThatIKnow = Workbooks.Addor Set oXLApp = CreateObject("Excel.Application"), we are actually creating the object in memory.

所以当我们说Set ThatIKnow = Workbooks.Addor 时Set oXLApp = CreateObject("Excel.Application"),我们实际上是在内存中创建对象。



And for the love of God why it is called "5.1"?

而对于上帝的爱,为什么它被称为“5.1”?



It is because of "God's Love" we evolved from primates which diverged from other mammals. So consider us a Version X of those mammals :D

正是因为“上帝的爱”,我们从灵长类动物进化而来,而灵长类动物与其他哺乳动物不同。所以把我们当作那些哺乳动物的 X 版:D

Yes, Pankaj Jaju is right when he mentioned that it is the version number. Now what is version number and why is it important?

是的,Pankaj Jaju 说的是版本号是对的。现在什么是版本号,为什么它很重要?

Version control a.k.a source control a.k.a Revision control in simple terms is nothing but management of changes to documents, applications, collection of information etc. Any new change is usually identified by a number or letter code or a mix of it.

版本控制又名源代码控制又名修订控制,简单来说就是管理对文档、应用程序、信息收集等的更改。任何新的更改通常由数字或字母代码或其组合标识。

This is helpful as it let's us know the current version of that document or application.

这很有帮助,因为它让我们知道该文档或应用程序的当前版本。

For further reading on version control see THIS LINK

有关版本控制的进一步阅读,请参阅此链接



Hope I have covered all your questions? If not, then feel free to ask.

希望我已经涵盖了您的所有问题?如果没有,请随时询问。

回答by nsg

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

is almost same as

几乎一样

Dim WinHttpReq As WinHttpRequest
Set WinHttpReq = New WinHttpRequest

The difference is that with first approach you do not have to include the library in the "References" list, but as a price to pay you will not have intllisense hints in the IDE because your reference is generic Object.

不同之处在于,使用第一种方法时,您不必将库包含在“引用”列表中,但作为要付出的代价,您将不会在 IDE 中获得智能提示,因为您的引用是通用对象。

Second form is preferable. It allows VB to check object types for compatibility as you assing them or pass them as parameters. It also give you hints which methods and properties the object has as you type its name.

第二种形式更可取。它允许 VB 在您分配对象类型或将它们作为参数传递时检查对象类型的兼容性。它还会在您键入对象名称时提示您该对象具有哪些方法和属性。