VBA CreateObject

ExoticBirdsMerchant picture ExoticBirdsMerchant · Feb 6, 2014 · Viewed 20.2k times · Source

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

enter image description here

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

Answer

Siddharth Rout picture Siddharth Rout · Feb 6, 2014

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.