VBA CreateObject - vba

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
What is CreateObject?
What is an ActiveX Control?
Creates and returning a reference of an ActiveX object - Meaning
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.

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

Related

Can you override a Publicnoncreatable program to apply its TLB?

I have referenced the SketchUp.exe into a project or at times an extracted TLB from the SketchUp.exe and can see the object model but cannot create an instance of any object in it.
I found out it may be a non-shared object or set to PublicNoncreatable and thus unlike AutoCAD or Excel I cannot create object instances.
When working with Adobe Acrobat I found it had the same problem but had a non-viewable object name which allowed instancing its objects.
Maybe SketchUp also has a completely hidden object or method that allows this. In the mean time is there a way to apply and instance of the object by overriding its non-share status as shared?
SketchUp's COM API is no longer supported. You may want to take a look at the new C API.

Does the CreateObject method follow any specific syntax?

For instance, at first I thought that what went in the parentheses was the library and class separated by a period (Ex: "Scripting.Dictionary", "Scripting.FileSystemObject". However, when you use it for internet explorer it is "InternetExplorer.Application" instead of "SHDocVw.InternetExplorer". Is the syntax not standard and something you just need to look up every time? I will use early binding anyways but I'm just curious.
The string used as a parameter to CreateObject() is what's known as a programmatic identifier or "ProgId". COM classes identify themselves using a CLSID, or what is essentially a GUID. But rather than having to remember and type the CLSID for the FileSystemObject COM class whenever you need to instantiate the COM object (which is {0D43FE01-F093-11CF-8940-00A0C9054228}, by the way), COM allows for the specification of a ProgId string.
You can examine the contents of the HKEY_CLASSES_ROOT\CLSID registry key to see all COM classes available on your PC. Here's the CLSID for the FileSystemObject class, showing the ProgID:
Tools like OleView or NirSoft's ActiveXHelper make finding ProgIds much easier. Other languages require you to jump through hoops to create COM objects. But the VB family of languages is "COM aware" and provides the necessary utilities to easily create and work with COM components without having to worry about IIDs, CLSIDs, Type Libraries, and other "COM" mumbo jumbo. In VB, when you add a "Reference", you're referencing a Type Library. When you call CreateObject(<string>), you're telling Windows to look up the CLSID of the COM class with a ProgId of <string>. They're just simpler, cleaner ways to interface with the COM world.
To (try to) answer your question, a ProgId can really be anything the creator of the COM object chooses. It's typically "<COM Library>.<COM Class>" but that's not always the case. Often version numbers are included. "MSMXL2.DOMDocument.6.0", for example, includes a version number for both the library and the class. Other times, there's no library at all (CreateObject("htmlfile"), for example). So, unfortunately, there's no perfect standard here.
It depends on how you are accessing the library.
NOTE: This will work in Visual Basic or VBScript
fso = CreateObject("Scripting.FileSystemObject")
Scripting is the name of the type library, and FileSystemObject is the name of the object of which you want to create an instance.
If you have the library referenced, you don't need to go through the scripting library.
NOTE: This will only work in Visual Basic.
Dim fso As New FileSystemObject

Enable Dictionary Object in VBA

When I initiate a dictionary object in Excel VBA, I found out two methods:
Use CreateObject("Scripting.Dictionary") (no "Microsoft Scripting Runtime");
Turn on the reference "Microsoft Scripting Runtime" first, then use Dim dict As New Scripting.Dictionary
Both of them work on my machine. I am wondering is there any difference between these two methods?
The only difference I know is the first one uses Late bind and the second one uses Early bind. The difference between the two is explained here.
Early bind has advantages. One of which is you can use Intellisense to guide you on the available properties of the bound object that you can use in coding. Also, it is said that it is faster in terms of performance. You can also use built-in constant as is. No need to check it's equivalent value. This is discussed here.
Late bind has advantages as well specially if you bound objects that have different versions. This reduces the risk of runtime errors due to version incompatibility (also mentioned in the 1st link).
As for me, I always use Early bind during development so I can utilize Intellisense. If there is a need to convert to Late bind, I'll do it later after I or the customer have done robust testing.
Additional:
When you use Early bind Don't Use Auto-Instancing Object Variables as discussed by CPearson in his post here. Below is the excerpt from his blog.
For object type variables, it is possible to include the New keyword in the Dim statement. Doing so create what is called an auto-instancing variable. Again, while this may seem convenient, it should be avoided. Contrary to what some programmers may believe, the object isn't created when the variable declaration is processed. Instead, the object is created when it is first encountered in the code. This means that, first, you have limited control when an object is created. Second, it means that you cannot test whether an object is Nothing, a common test within code and a common testing and diagnostic technique.
So a better way to set your variable is:
Dim dict As Scripting.Dictionary
Set dict = New Scripting.Dictionary

reference uninstansiated objects in vb.net

Hi i'm having to learn VB.net for a new job having previously been a C# guy. I just come across an interesting feature of VB.net. I can reference objects on a second form that has not been instatiated!
So from Form1 i can get the text property of textbox1 on Form2 as follows
Dim txt As String = Form2.TextBox1.Text
Can anyone explain how this works? Are all forms instatiated at the start of the program and then their visibility is toggled throughout the program lifespan?
Forms in VB are a special case. The compiler generates a strongly-typed list of forms in the My.Forms object of the My namespace. Each form is exposed as a property My.Forms.TheNameOfTheForm. These properties always return valid instances – i.e. if a form hasn’t been instantiated before, it will when you first use the property.
So far, so good.
But Microsoft also made the brain-dead (!) decision of importing the properties from the My.Forms object by default, everywhere, and there’s nothing you can do. Superficially, this is for backwards compatibility reasons to VB6 but that’s nonsense since VB7 (.NET 1.0) didn’t have this feature, it only came later.
But just to clarify:
Are all forms instatiated at the start of the program …?
No, luckily not. They are instantiated the first time you access the property.

How do I know whether an application support OLE2 and which methods & attributes are exposed?

I want to call an ActiveX DLL or OLE2 object from ABAP.
I already know the syntax of how to instantiate the object & execute the methods:
data: my_object type ole2_object.
create object my_object <ole2object>.
call method of my_object <objectmethod>.
But given a particular application, how do I know if this is supported, what the values/names of ole2object and objectmethod is?
Transaction SOLE supplies a table of OLE Applications, among this is Excel.Application which I know can be instantiated as an OLE object, so it looks like you have to add the OLE2 app to this table first, but once again where can I read the data like CLSID & LibType from - is it published as part of the application?
Objects are coming from the table TOLE. Their methods and propeties are stored into table OLELOAD.
I found some time ago somes documents (this one and this one) that contained indications of how you can find the properties and methods.
Indication on the CLSID are succint, but it seems to be the value in the register with HKEY_CLASS_ROOt/CLSID that goes with the application indicated (ie VISIO.APPLICATION for exemple). If you search this application value with regedit, you'll find the corresponding CLSID.
Hope it's helping
Guillaume
In this case, you're simply using OLE2 to access a COM interface. If you're into accessing Office applications, you might want to take a look at KB222101. For other applications and libraries, you'll need the API documentation, some suitable examples (not necessarily in ABAP, VB will do just fine). a pointed stick to poke the developer with or all three of them...
Oh, and there's a tool called OLE/COM Object Explorer by MiTeC that can be downloaded for free from their website. Haven't used it myself, but it looks like it might be helpful.