VBA CreateObject [英] VBA CreateObject

查看:353
本文介绍了VBA CreateObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在1月10日以后收到这个代码行,我收到了一封电子邮件,我发现我不得不学习类模块,所以我已经做了,现在返回问一个新的基础。代码行是(严重警告:您必须转到VBE中的工具 - >引用,并使用Early Binding激活 Microsoft WinHTTP服务5.1版):

  Dim WinHttpReq As Object 

设置WinHttpReq = CreateObject(WinHttp.WinHttpRequest.5.1)

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



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

  Dim ThatIKnow as Workbook 

然后我们实例化它这样

  Set ThatIKnow = Workbooks.Add 

为什么我们需要CreateObject?



帮助继续说 ... ActiveX对象



如果我在帮助上点击ActiveX,它会指出说:
通过自动化界面暴露给其他应用程序或编程工具的对象



这一行绝对不会告诉我。我所知道的关于ActiveX的都是这个





...我认为是ActiveX控件...(我必须承认,虽然ActiveX对我来说总是一个有雾的术语)



现在在CreateObject(WinHttp.WinHttpRequest.5.1)内部,我已经扫过了Web,我找不到WinHttp对象的一些体面的MS帮助, 。无论如何,对象浏览器都有它作为库,但是F1帮助按钮什么都不显示。所以对象浏览器说它是一个库,我发现它在Web中被称为WinHttp参考,也作为一个WinHttp对象。所有这些是什么?



为了上帝的爱,为什么它被称为 5.1 ?我没有发现任何一个WinHttpRequest.5.1术语



我不是要求咀嚼食物,但是任何努力破解ainigma真的会使整个情况更加紧张。请帮助我破解这行代码的任何一点都将是帮助的色调



感谢观看我的问题

解决方案

我不会覆盖早期绑定和延迟绑定之间的区别。您可以在此知识库文章中阅读有关它们的信息



但是,这样做会回答您在问题中所遇到的所有小问题,例如


  1. 什么是CreateObject?

  2. 什么是ActiveX控件?

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

  4. 为了上帝的爱,为什么它被称为5.1?






什么是CreateObject?






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






什么是ActiveX控件?






ActiveX控件是一个组件程序对象,可以被许多应用程序重新使用。基于组件对象模型(COM)创建ActiveX控件的主要技术。一般来说,ActiveX控件可以替换早期的OCX(对象链接和嵌入自定义控件)。



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



这些ActiveX控件以所谓的容器(例如使用组件对象模型程序接口的MS Excel)运行。其实这实际上帮助我们大家。想象一下,每次都从头开始编写这些控件的代码!






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






请参考您所引用的内容来解释。

  Dim ThatIKnow as Workbook 

Set ThatIKnow = Workbooks.Add

我们正在做的是 Dim ThatIKnow as Workbook 告诉运行时环境,我们将创建一个类型为工作簿,并在我们的代码中将其称为ThatIKnow。然而,这实际上并没有创建对象,也不分配任何内存。仅当使用新建关键字或任何其他方式(例如 Createobject )创建对象时才会分配内存,并设置为这个变量 ThatIKnow



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






为了上帝的爱,为什么叫5.1?






这是因为上帝的爱,我们从与其他哺乳动物分离的灵长类动物进化而来。所以请考虑我们这些哺乳动物的版本X:D



是的,Pankaj Jaju是正确的,当他提到它是版本号。现在版本号是什么,为什么这很重要?



版本控制又称源代码控制aka简单的修改控制只不过是对文档,应用程序的收集,信息等。任何新的更改通常用数字或字母代码或其混合来标识。



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



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






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


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.1 with Early Binding):

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

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?

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

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

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

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

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?

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

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

thanks for watching my question

解决方案

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"?


What is CreateObject?


As I mentioned in the comment above CreateObject is 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.


What is an ActiveX Control?


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).

An ActiveX control can be created in any programming language that recognizes Microsoft's Component Object Model for example Visual Basic and 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!


Creates and returning a reference of an ActiveX object - Meaning


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 Workbook is 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 New keyword or any other way such as Createobject and assiged to this variable ThatIKnow

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


And for the love of God why it is called "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

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

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.

这篇关于VBA CreateObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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