Late Binding vs Early Binding in VBA - (CreateObject() vs New)
Asked Answered
R

1

3

I'm trying to use VBA code to invoke a protected API which need authentication with OAuth2. Once I try to open a URL, I'm redirected to the ADFS page for authentication and than I come back.

Now for some applications using CreateObject("InternetExplorer.Application") and the .Navigate URL works fine, for other web app I need to use New InternetExplorerMedium in order to have the code working.

Can you tell me the differences between these objects and why some websites work with one and some work with the other?

Thank you

Refusal answered 29/5, 2018 at 11:51 Comment(0)
D
3

This way of referring to objects is called "Early" and "Late Binding". From MSDN:

The Visual Basic compiler performs a process called binding when an object is assigned to an object variable.

An object is early bound when it is assigned to a variable declared to be of a specific object type. Early bound objects allow the compiler to allocate memory and perform other optimizations before an application executes.

By contrast, an object is late bound when it is assigned to a variable declared to be of type Object. Objects of this type can hold references to any object, but lack many of the advantages of early-bound objects.

You should use early-bound objects whenever possible, because they allow the compiler to make important optimizations that yield more efficient applications. Early-bound objects are significantly faster than late-bound objects and make your code easier to read and maintain by stating exactly what kind of objects are being used.

TL DR:

The difference is that in early binding you get intellisense and compiling time bonus, but you should make sure that you have added the corresponding library.


  • Example usage of late binding:

Sub MyLateBinding()

    Dim objExcelApp     As Object
    Dim strName         As String

    'Definition of variables and assigning object:
    strName = "somename"
    Set objExcelApp = GetObject(, "Excel.Application")

    'A Is Nothing check:
    If objExcelApp Is Nothing Then Set objExcelApp = CreateObject("Excel.Application")

    'Doing something with the Excel Object
    objExcelApp.Caption = strName

    MsgBox strName

End Sub

  • Example usage of early binding:

First make sure that you add the MS Excel 16.0 Object Library from VBE>Extras>Libraries:

enter image description here


Sub MyEarlyBinding()

    Dim objExcelApp     As New Excel.Application
    Dim strName         As String

    'Definition of variables and assigning object:
    strName = "somename"

    'A IsNothing check:
    If objExcelApp Is Nothing Then Set objExcelApp = CreateObject("Excel.Application")

    'Doing something with the Excel Object
    objExcelApp.Caption = strName

    MsgBox strName

End Sub

Related articles:

Demello answered 29/5, 2018 at 12:4 Comment(10)
thanks for your quick reply. i checked and i have that library, now as some app works with early binding and other don't is a way to have bothe working for example managing errors like on error resume next or somethign like this? how can i fix this?Refusal
@Refusal - on the samples, it is explained for early and late binding of an Excel object. For this you need the corresponding Excel library. If you are trying to early-bind other objects, you have to add their libraries to the VBE.Demello
i have both library microsoft office 16.0 object library and microsoft internet control. do i need other libraries?Refusal
@Refusal - I do not know. It depends what objects are trying to refer to. You need a library for every type of object. Thus, if you are writing Dim objXML As MSXML2.DOMDocument then you need MSXML2.DOMDocument library.Demello
CreateObject("InternetExplorer.Application") vs New InternetExplorerMedium is not only late vs early binding, but above all it's 2 different objects, the latter uses medium security settings. Defaults sometimes don't allow macro to work properly.Greenlet
i got your point. what it is not clear to me is why some web app works with CreateObject and some work with New Object.Refusal
I have never encountered similar issue, have you tested CreateObject("InternetExplorerMedium.Application") vs New InternetExplorerMedium and CreateObject("InternetExplorer.Application") vs New InternetExplorer on the same website?Greenlet
@RyszardJędraszyk i tried but same issue. is there any way to manage the error and when it happens create the object with the other approach?Refusal
@Refusal - I guess here is the time to kindly ask to provide some minimal reproducible example in your question.Demello
i got my example working after unchecked the protected mode in internet zone within IE. this fixed all my issues. thank all of you for your help and effort. zanza67Refusal

© 2022 - 2024 — McMap. All rights reserved.