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

Switch to next webpage clicking on anchor link with vba

Code:
Option Explicit
Sub SaveHTml()
Dim str1, str2, str3, str4, str5, str6, str7, URL As String
Dim ie, frm As Object
Dim i As Long
Dim filename As String
Dim FF As Integer
'Dim elems As IHTMLInputTextElement
Dim wb As WebBrowser
Dim objElement As Object
Dim objCollection As Object
Dim Button As Object

'On Error Resume Next
Application.ScreenUpdating = False
str1 = Sheets("PR_data_with_search").Range("F10").Value
str2 = Sheets("PR_data_with_search").Range("I10").Value

URL = "https://webtac.industrysoftware.automation.com/webpr/webpr.php?objtype=frames" 'for TEST
filename = "C:\Users\" & Environ("UserName") & "\Desktop\Test.htm"

Set ie = CreateObject("Internetexplorer.Application")
ie.Visible = True
ie.Navigate URL

Do Until ie.ReadyState = 4
DoEvents
Loop

Set ie = Nothing
' load next web page
Dim i1 As Integer
Dim txt, p As String, link As String
txt = "Advanced Search"
Do Until link = txt
    i1 = i1 + 1
    p = ie.Document.Links(i1).tostring
    link = Right(p, 6)
Loop
ie.Document.Links(i).Click

ie.Document.getelementsbyname("openedFrom_dateText")(0).Value = str1
ie.Document.getelementsbyname("openedTo_dateText")(0).Value = str2

'Call ie.Document.getElementsByTagName("ok").Item(1).Click
Set Button = ie.Document.getElementById("ok")
Button.Click

Do
Loop While ie.Busy

CreateObject("Scripting.FileSystemObject").CreateTextFile filename

Do Until ie.ReadyState = 4
DoEvents
Loop

FF = FreeFile
Open filename For Output As #FF

With ie.Document.body
    Print #FF, .outerHTML & .innerHTML
End With

Close #FF

ie.Quit
Set ie = Nothing
Application.ScreenUpdating = True
End Sub

I can load web page with URL - https://webtac.industrysoftware.automation.com/webpr/webpr.php?objtype=frames
but unable to navigate to next page by clicking on hyperlink having text "Advanced Search".
here is the html code for link:
Code:
<li><a href="javascript:parent.gotoSearch('advanced')">Advanced Search</a></li>
Also I want to hit OK button on the same page.
Code:
<input type=button name="ok" id="ok" value=" OK " onClick="onSubmitFunction()">
 
Last edited:
So the button is sending request via script. Instead of manipulating IE. Just trace what requests are sent to Web Service through GET/POST using Developer Tool in your browser.

You can then find what URL is used to send request with what parameter. Construct the string and send request via XML.

Do a search on the forum and you'll find plenty of examples.

Also basic tutorial for web scraping in the link.
http://analystcave.com/web-scraping-tutorial/
 
Code:
Sub Advanced_Serach()
    Dim ie As InternetExplorer
    Dim doc As HTMLDocument
    Dim str1, str2, str3, str4, str5, str6, str7, str, URL As String
    Dim form As Variant, button As Variant

    Set ie = New InternetExplorer
    ie.Visible = True
    ie.Navigate "https://webtac.industrysoftware.automation.com/webpr/webpr.php?objtype=frames&"
    'read values from excel sheet
    str1 = Sheets("PR_data_with_search").Range("F10").Value
    str2 = Sheets("PR_data_with_search").Range("I10").Value
    'Wait for page to load
     Do While ie.Busy Or Not ie.ReadyState = READYSTATE_COMPLETE
        DoEvents
     Loop

     Set doc = ie.Document
    'Do whatever manipulation of the HTML document you need
    ie.Navigate "javascript:parent.gotoSearch('advanced')"

    'add read values in web page
    ie.Document.getelementsbyname("openedFrom_dateText")(0).Value = str1
    ie.Document.getelementsbyname("openedTo_dateText")(0).Value = str2
    ''error is here, the page does not switch to next page when closed, there is     automation error, The object invoked has disconnected from its clients
    While ie.ReadyState <> 4
    DoEvents
    Wend
    'Do whatever manipulation of the HTML document you need
    'Set doc = ie.Document
    ie.Document.getelementsbyname("Ok")(1).Value = str
        'Reassign document
    'Set Button = ie.Document.getElementById("ok")
    'Button.Click

    Do
    Loop While ie.Busy

    End Sub
With above code, I can open web page, click anchor link. But as soon as anchor tag link(here javascript will run), there is runtime error 91 "object variable or With block variable not set" is displayed though page is loaded successfully. Next I want to add read values from sheet to displayed web page and hit OK button. Any help will be appreciated.
 
Last edited:
Back
Top