execute javascript on a webpage via VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LWKnox
    New Member
    • Aug 2013
    • 5

    execute javascript on a webpage via VBA

    RE: execute javascript on a webpage via VBA

    I have a procedure that opens a webpage. What I want to do is click a button on that page, but it isn't really a button. There is a Javascript function that clicking the "button" runs to open a different version of the webpage.
    Last edited by zmbd; Aug 19 '13, 06:10 PM. Reason: [z{copied question from title into the post}]
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    This is in the VBA forum. I think you actually want to post that elsewhere. Either in the. .Net forum or javascript forum.

    That being said, here's documentation on opening a URL in javascript.
    W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


    Jim

    Comment

    • LWKnox
      New Member
      • Aug 2013
      • 5

      #3
      Code:
      Private Sub Command292_Click()
      Dim StrWebpage As String
      StrWebpage = "http://exclusions.oig.hhs.gov/Default.aspx"
           
          Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
          Dim HTMLDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
          Dim htmlInput As MSHTML.HTMLInputElement
          Dim htmlColl As MSHTML.IHTMLElementCollection
           'Dim HTMLImage As MSHTML.IHTMLInputImage
           
           
          Set objIE = New SHDocVw.InternetExplorer
           
             With objIE
               .navigate StrWebpage ' Main page
               .Visible = 1
               Do While .Busy: DoEvents:   Loop
      
       Do While .ReadyState <> 4: DoEvents: Loop
       
       Set HTMLDoc = .Document
       Set htmlColl = HTMLDoc.getElementsByTagName("Image")
       Set objIE = New SHDocVw.InternetExplorer
        Call objIE.Document.Frames(1).Document.parentWindow.execScript("Javascript:_doPostBack('ctl00$cpExclusions$lbSearchMP','')")
        end with
      end sub
      Here is part of the source code:
      Code:
      For Multiple Individuals" style="border-width:0px;" />
                  <a id="ctl00_cpExclusions_lbSearchMP" href="javascript:__doPostBack('ctl00$cpExclusions$lbSearchMP','')">Search For Multiple Individuals</a> &nbsp;&nbsp; |

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        jimatqsi:
        I moved the question from the title to the body of the text. This helps to clarify that the topic concerns VBA.


        LWKnox:
        We will need to see the code that you are using to call the webpage so that one of the experts here can determine how best to get the "handle" to the open webpage.

        Comment

        • jimatqsi
          Moderator Top Contributor
          • Oct 2006
          • 1293

          #5
          So you don't actually turn control over the browser in this case? I didn't know you could do that. This could very useful. Thanks, zmbd.

          jim

          Comment

          • LWKnox
            New Member
            • Aug 2013
            • 5

            #6
            The VBA code here is just a small part of a larger, more ambitious effort. When I can get the page with fields for multiple persons open, I will make it enter into those spaces names I have on the Access form and click the search button. This is just a first step.

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              LWKnox:
              Line 24 of your code appears to be malformed; however, honestly, I've never attempted this so I could be very mistaken.
              Double check that the reference to the frame is correct. If the control is in a different frame or if there are no frame then you call will more than likely fail. I don't know if it will error.
              execScript("jav ascript:_doPost Back('ctl00$cpE xclusions$lbSea rchMP','')")
              is the part that I think may be your issue.
              You need to call the function within the frame so that it looks like:
              execScript("Pre ssThisHereButto n()", "JavaScript ")

              However, please take that with a "grain or two of salt."

              Comment

              • LWKnox
                New Member
                • Aug 2013
                • 5

                #8
                Should ("PressThisHere Button()") be 'ctl00$cpExclus ions$lbSearchMP '?

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  LWKnox
                  That you will have to try yourself. I have no knowledge of your page design and I'm afraid I just don't have the knowledge to dig thru the java to find what you should place there and what little help from the VBA side that I can offer is based upon the help files for IE-Automation.

                  You may be able to use a simple "object.cli ck" if you know the element's name.

                  We'll just have to wait a bit a see if someone with more IE-Automation has something to offer.

                  Comment

                  • zmbd
                    Recognized Expert Moderator Expert
                    • Mar 2012
                    • 5501

                    #10
                    jimatqsi
                    So you don't actually turn control over the browser in this case? I didn't know you could do that. This could very useful. Thanks, zmbd.

                    jim
                    Yes, that is what I'm saying.
                    I ran across the following article awhile back:
                    Working with Internet Explorer Using VBA

                    using that information, some trial and error with the element id to find the text box, I came up with this just as a proof of concept; however, I couldn't figure out how to get the "enter" or "search" button to trigger - I didn't put a lot of time into it either:

                    {Note: 2013-08-21-1334CST: The following code was edited to add error trapping and modified to actually run the search.}
                    Code:
                    Sub IE_Run_Google()
                        '
                        'based on: http://vba-corner.livejournal.com/4623.html.
                        '
                        On Error GoTo errorjumppoint
                        Dim z_ie As InternetExplorer
                        '
                        Set z_ie = New InternetExplorer
                        '
                        z_ie.Navigate "www.google.com"
                         'Loop until it has loaded.
                        Do Until z_ie.ReadyState = READYSTATE_COMPLETE
                        Loop
                        '
                        z_ie.Visible = True
                        z_ie.Document.getElementById("q").Value = "VBA Example"
                        z_ie.Document.getElementById("q").Click
                        z_ie.Document.getElementById("btnK").Click
                        '
                    errorjumpreturn:
                        If Not z_ie Is Nothing Then Set z_ie = Nothing
                        '
                    Exit Sub
                    errorjumppoint:
                        MsgBox "Somethin went wrong:" & Err.Number & vbCrLf & Err.Description
                        Resume errorjumpreturn
                    End Sub
                    Maybe the journal entry is still available for view!
                    Appears blocked by my Co-ISP; however, they block about 65% of the web!
                    Last edited by zmbd; Aug 20 '13, 06:34 PM. Reason: [z{Updated the code with error trapping and finally the click event I needed to run the search}]

                    Comment

                    • LWKnox
                      New Member
                      • Aug 2013
                      • 5

                      #11
                      I've already tried several permutations of the code above without success. Apparently the "button" is not a button at all but an image that when clicked runs a Javascript sub that opens the next webpage. That is precisely what I want to do is run the js and open the next page.

                      Comment

                      • zmbd
                        Recognized Expert Moderator Expert
                        • Mar 2012
                        • 5501

                        #12
                        then you need to find the parent of the image and try a click event on that.

                        Comment

                        • zmbd
                          Recognized Expert Moderator Expert
                          • Mar 2012
                          • 5501

                          #13
                          Just to update,
                          As I noted in the last post to find the parent image, this what I did for my code in post#10 which I've now updated to reflect this, and I've added a bit of error traping etc...
                          This is what you'll have to try to do... either find the function call or find the element in the document that does the call.
                          It can be done.

                          Comment

                          Working...