Download web page information via vba

  • Hello all,


    Im stuck on using Internet explorer objects and how to approach the following issue.


    I wish to see all the new planning applications that have been received for in past week here http://planning.bournemouth.go…Register/planappsrch.aspx, then extract out the information that comes out (if you click on the link it gives further information which I would like to capture such as name address etc). I had hoped that I could use the data from web function in excel, but that doesn't seem to give all the details (it cuts off at first ten).


    Does anyone have any starters for ten, or an approach I can take or read about?

  • Re: Download web page information via vba


    Try this code!


  • Re: Download web page information via vba


    Hello,


    Thanks for taking the time to respond, however I am not sure what this is actually doing? The output on sheet2 doesn't give me any details of the applications, nor the names of people which currently I would have to manually click into. I am hoping I can get steered into


    • Populating the first web "search" page by just putting in the relevant date search (I know how to work out previous week date, I just don't know how to send it to web page). So if I try and put dates of June into received, I get 81 results
    • The search results that come back I would like to get out the details (name, address, reason), which you can click on but not sure how to use vba to navigate through the different tabs on the webpage. So under the application column from the search results, I have to manually click then copy details from the main details and location tabs that come up, though id be interested to learn how all the tabs can be imported.


    VBA with web objects I have little experience with, so some guidance would be terrific. Essentially once I have the data into the spreadsheet I will be able to do the rest.


    Thanks in advance

  • Re: Download web page information via vba


    Hello,


    Is anybody able to direct me with how to approach this? Im assuming its possible but just need a push in the direction to take?

  • Re: Download web page information via vba


    very quick and dirty approach, will only get the first page results but should get you started:


  • Re: Download web page information via vba


    Thank you for helping. I am getting a run time "object required" error on this line, do I need to add in a reference?


    Code
    x = Replace(IE.Document.GetElementByID("MainContent_grdResults_ctl00__" & i).OuterHTML, vbLf, ":")


    Many many thanks for helping me! Also in the approach to this, is it then better to calculate how many pages or results it is, then individually program to open, copy and close each result until total number of results is reached? Given the way the web page is done it makes sense, but this is all new learning for me!

  • Re: Download web page information via vba


    Ok, I understand the first part you gave me (go me) and I now understand how to get the name of the fields.


    However the second part of code you gave me that errors I don't understand at all, and even a web search doesn't seem to help me so cant work out the problem! Sorry if I am being thick, but everything I have done in vba is self taught. Its eems you are using "regular expressions" but not sure how this works as I cant play with it?

  • Re: Download web page information via vba


    The regular expression in pattern matching - it's looking for everything between ">" and "<" (i.e. everything NOT inside a HTML tag) Definitely not the best way to do things but as I said - quick and dirty approach. You can see an example of what it's doing here


    Once it's grabbed the text from the RegEx it's just using replace functions to strip the ">" and "<" characters and any unwanted white space so that it can be chucked into a text file.


    You're not being thick - RegEx is very specific and not usually used to this degree in VBA


    The best thing I can advise here is early binding - set a reference to use Microsoft Internet Controls and then you can use the IntelliSense menus to have a play around with different methods to extract data from the HTML Document.

  • Re: Download web page information via vba


    Ok, thanks again.


    I have Microsoft Internet Controls already ticked, but still giving me the run time error 424, am I doing something wrong? Once I get it pasting into excel I can then bodge round it, and will post back. Im sure my code historically wasn't "pure" but it got the job done, so quick and dirty isn't a problem!

  • Re: Download web page information via vba


    hmmm.. not too sure what's causing that - have you tried going into IE->Tools->Internet Options->Security->Internet and uncheck "Enable Protected Mode"? (note you have to restart IE to take effect)


    I wouldn't advise leaving this unticked, but I know it can cause problems with VBA automation.

  • Re: Download web page information via vba


    Quote from S O;749971

    hmmm.. not too sure what's causing that - have you tried going into IE->Tools->Internet Options->Security->Internet and uncheck "Enable Protected Mode"? (note you have to restart IE to take effect)


    I wouldn't advise leaving this unticked, but I know it can cause problems with VBA automation.


    Tried your suggestion, but still erroring on that line. Have Microsoft Internet Controls, Microsoft HTML Object Library and Microsoft VBScript Regular Expression 5.5 all ticked. Should I try an alternative HTML way of copying the results in to excel?

  • Re: Download web page information via vba


    You've added the references, but have you changed the code so that it uses early binding?


    i.e.


    change

    Code
    Set IE = CreateObject("InternetExplorer.Application")


    to

    Code
    Dim IE As New InternetExplorer


    That way, when you type a "." after the "IE" you will see the IntelliSense menu with all the different methods/properties.


    Same goes for the other objects being used.

  • Re: Download web page information via vba


    Still no joy.


    What is odd is I retyped "ie.document" so that the menu would show, and it worked. Tried it again and same error, and retyping does nothing! Im well out of my depth now!


    Also, I wanted to import the number of results from the search but it doesn't like this code.


    Code
    Range("A1") = ie.Document.getElementById("contenttext").Value


    in this example, I was hoping to return "Your search returned the following results (a total of 31).Click on an application number to view its case file." I would then extract the number 31, but have tried by value, innerhtml, innertext and errors.

  • Re: Download web page information via vba


    Hello all,


    This is getting to be a little too much for me! I have spent a lot of time researching, but a lot of it is going above my head, so help is appreciated! I had this code cobbled, but it doesn't seem to give me the results I want



    Problems so far:-


    1. The code doesn't always bring back the "results" and I cant work out why as the webpage should be loaded
    2. IE.Quit doesn't close down the IE session.


    Now heres the interesting thing. If I set IE.visible to false, the code seems to always bring the information back. Does anyone know why this is? I thought putting a do while meant that ie was completely loaded, but seems to be a timing issue? I know I will want to use the getelementsbyID approach, but I am getting all the elements simply to try and find out what is and isn't being loaded (originally it couldn't find any). However, I might go the root of copying everything, then deleting out the results I want.


    The bigger picture is I want to click on all the results (so will have to look at how many results there are and find some code to load the next page), and for each result click on their further details and copy that information in.


    I really hope someone can help as I'm close to crying! I did look at getting html directly but didn't understand it at all.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!