VBA CreateObject

前端 未结 2 844
闹比i
闹比i 2021-02-06 12:02

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

相关标签:
2条回答
  • 2021-02-06 12:46
    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.

    0 讨论(0)
  • 2021-02-06 12:47

    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.

    0 讨论(0)
提交回复
热议问题