• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Unable to run my script successfully using late-binding

shahin

Active Member
How to create my code using late-binding method so that I do not need to add any reference to the library? I tried with the below one but it doesn't seem to work. I know it has been discussed several times in several forums but it's hard to find them when in need. Any help on this will be highly appreciated.

This is what I tried:
Code:
Sub Late_Binding()

    Dim html As Object, post As Object

    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = CreateObject("htmlfile")
        Set html = .document
     
        Set post = html.querySelector("h3 a[href*=help]")
        MsgBox post.innerText
    End With
 
End Sub

I know what I did above is a clear mess. Please don't laugh at my ignorance.
 
Last edited:
As much as I see the late binding is not an issue here and you've done it correctly.

Issue seems to be following:

If I add watch to object "CreateObject("InternetExplorer.Application")" and test then it seems that code just doesn't get past this loop:
Code:
Do Until .readyState = READYSTATE_COMPLETE: Loop

as .readyState value remains just at READYSTATE_UNINITIALIZED.
 
Thanks shrivallabha, for pointing me into the right direction. I've fixed it already. Now it is doing fine:
Code:
Sub Late_Binding()

    Dim html As Object, post As Object

    With CreateObject("InternetExplorer.Application")
        .Visible = False
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        Do Until .readyState = 4: DoEvents: Loop
        Set html = CreateObject("htmlfile")
        Set html = .document
   
        Set post = html.querySelector("h3 a[href*=help]")
        MsgBox post.innerText
    End With

End Sub

However when I do the same outside with block, I don't find any way to quit the browser.

Code:
Sub Late_Binding()

    Dim html As Object, post As Object

    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        Do Until .readyState = 4: DoEvents: Loop
        Set html = CreateObject("htmlfile")
        Set html = .document
    End With
  
    Set post = html.querySelector("h3 a[href*=help]")
    MsgBox post.innerText

End Sub

Btw, one last thing: is it necessary to use like this "Set html =" twice, I meant, ain't there any better way?
 
Last edited:
Btw, one last thing: is it necessary to use like this "Set html =" twice, I meant, ain't there any better way?
I will try to answer the VBA query leaving the other question for Web scraping Gurus to answer.

It is not necessary to use the set sentence twice and it will still work for this code i.e.
Code:
  'Set html = CreateObject("htmlfile") Commented out!
        Set html = .document
but I have read somewhere (probably Chihiro or Marc L post) that if you are late binding then there are chances of getting some unwanted surprises due to possible binding to incorrect/unintended object type so I'd leave the CreateObject statement as it is so that variable binds to correct object type at runtime.
 
Hi ,

You say :
However when I do the same outside with block, I don't find any way to quit the browser.
This has nothing to do with the code being inside the With block or outside it.

In the first procedure , you have the following line of code :
Code:
.Visible = False
whereas in the second procedure , you have :
Code:
.Visible = True
Thus , in the second case , you can see the window which opens Internet Explorer for yourself.

However , if you run the first procedure , and then use the Task Manager , you will find that the Internet Explorer process is still running , even after the procedure has completed execution.

Secondly , using late binding does not necessarily mean that you have to use a With clause. You can use :
Code:
Sub Late_Binding()
    Dim html As Object, post As Object
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        Do Until .readyState = 4: DoEvents: Loop
        Set html = CreateObject("htmlfile")
        Set html = .document
 
        Set post = html.querySelector("h3 a[href*=help]")
        MsgBox post.innerText
    End With
 
    IE.Quit
End Sub
and it will still be late binding.

Narayan
 
Thanks Narayan, for your elaborative answer. I'll keep all the suggestions in my mind. Basically, I could not ask in the right way what I wanted to. Few days back, while roving around different threads I could come across a piece of code written by Marc L. However, I was very fond of that style and I wanted to apply the way I saw. The below script is more or less similar to that in which ".quit" has been defined within "with" block.

Code:
Sub Late_Binding()

    Dim html As Object, post As Object
  
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        While .readyState < 4: DoEvents: Wend
        Set html = CreateObject("htmlfile")
        Set html = .document
        Set post = html.querySelector("h3 a[href*=help]")
        MsgBox post.innerText
        .Quit  ''''Here it is
    End With
  
End Sub

And what I tried to achieve is do the same outside "with block", I meant make the browser quit. As Narayan suggested that "with" block doesn't have to be there always to create a "late binding", I completely agree to that. My point here is I didn't wish to use any variable like "IE" or something and then "set" it to "InternetExplorer.Application". This is it.
 
Do you mean something like below o_O:
Code:
Sub Late_Binding()

    Dim html As Object, post As Object
  
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        Do Until .readyState = 4: DoEvents: Loop
        Set html = CreateObject("htmlfile")
        Set html = .document
        Set post = html.querySelector("h3 a[href*=help]")
        MsgBox post.innerText
        End With
        .Quit  'Now written outside with loop 
End Sub
 
Thanks shrivallabha, for leading me to the reality. It seems I was expecting something which is not possible. I thought in the first place that "for loop" within "with block" will not bring expected results but I was wrong about that as well.

Code:
Sub Late_Binding()

    Dim html As Object, post As Object
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        While .readyState < 4: DoEvents: Wend
        Set html = CreateObject("htmlfile")
        Set html = .document
       
        For Each post In html.getElementsByClassName("PreviewTooltip")
            r = r + 1: Cells(r, 1) = post.innerText
        Next post
        .Quit
    End With
   
End Sub
 
One last thing: Is there any way to satiate the below requirement (the line containing comment)? The rest I can manage.
Code:
Sub Late_Binding()

    Dim post As Object
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        While .readyState < 4: DoEvents: Wend
        With CreateObject("htmlfile")
     
         = .document  ''Is there any way to make this line valid (that's all i expect)
     
        For Each post In .getElementsByClassName("PreviewTooltip")
            r = r + 1: Cells(r, 1) = post.innerText
        Next post
        .Quit
    End With
 
End Sub
 
Hi ,

No.

All assignment statements expect a left hand side , which can be a variable or an object , and a right hand side , which can be several , such as another variable or object , expression , or keywords / constants such as Nothing , vbnullstring ,...

However , I fail to understand the necessity for such coding.

Coding is meant to do a job , accurately and efficiently and elegantly.

None of the above three requirements requires cryptic code or even worse unintelligible code ; there are enough references on the Internet , and even books , which stress the need to code without comments , which means the code should be understandable on its own.

I would not take code snippets you see on forums as examples of great or even good code.

Go through professional books written by the masters and structure your code on the basis of what you find in them.

Narayan
 
Hi ,

Why not this ?
Code:
Sub Late_Binding()
    Dim post As Object
   
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        While .readyState < 4: DoEvents: Wend
       
        For Each post In .document.getElementsByClassName("PreviewTooltip")
            r = r + 1: Cells(r, 1) = post.innerText
        Next post
        .Quit
    End With
End Sub
Narayan
 
I've read none of them that is why I ask too many questions to learn through practice. Reading is definitely the option second to none. I just started writing codes watching some videos and coming across some giants in chandoo. For that reason I didn't find it necessary to start by reading books. For the clarity: my intention above was to get rid of all the variable so that I do not need to declare them as "Nothing" in the end. Thanks for everything Narayan.
 
You took too long Narayan!!:( I was expecting just this for the last few days. It solves this thread. Thankssssssssss a lot.
 
I've read none of them that is why I ask too many questions to learn through practice. Reading is definitely the option second to none. I just started writing codes watching some videos and coming across some giants in chandoo. For that reason I didn't find it necessary to start by reading books. For the clarity: my intention above was to get rid of all the variable so that I do not need to declare them as "Nothing" in the end. Thanks for everything Narayan.
Dimensioning variables and then releasing them from memory is nothing evil and in fact a necessity from good coding practice point of view. Read about variable scoping and their life spans. There are different styles of coding (with minimum variables), it is just a matter of your own inclination.

When I started coding I counted number of code rows. The more the merrier...

Then I saw codes which were small (sometimes one line) and did the same as long codes. Knowing various syntax became a fixation...

Then I started seeing codes which were short but took long to execute and some which appeared longer but took shorter time...

Then I saw codes which were reusable and module like. They'd ship easily from one application to another. More you had these then repeating certain actions became breeze...

On this forum if you search for SirJB7's code you will find several variables and constants and then on vbaexpress SNB's codes then you will find several codes in above manner i.e. without variable.

So from a solution standpoint, as Narayan has pointed out focus on the end result but you lose nothing from trying out different things / approaches!
 
Hi ,

There is a difference between an approach or an algorithm , and style of coding.

Style is something that one adopts once and for all.

Style is something that should adhere to what are known as best practices.

As I have mentioned , forums and snippets of code are never the indicators of best practices in coding. Read books and what the real masters have to say.

The ultimate test of good coding is peer review , and books which show how to build applications will offer good examples of professional coding.

As far as websites go , I would suggest Chip Pearson's site as offering good coding examples , but that is just my preference.

See what this says , and see if it has anything that you find useful :

http://www.spreadsheet1.com/vba-development-best-practices.html

Narayan
 
Back
Top