Announcement

Collapse
No announcement yet.

VBA Code To Activate/Press Button On Web Page

Collapse
This topic is closed.
X
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBA Code To Activate/Press Button On Web Page



    press a button on web page using VBA

  • #2
    Re: VBA Code To Activate/Press Button On Web Page

    Comment


    • #3
      Re: VBA Code To Activate/Press Button On Web Page

      Hi Mctabish, this really really depends on how the webpage was programmed, but in a recent task that I did, I had to enter a username and password and click a submit button. This is the code that did this part. The code uses the IE Object Model and you will need to become familiar with it - this will give you an idea on how to take on the task.

      Code:
      Option Explicit
      
      Public Sub Press_Button()
      
      
      'make sure you add references to Microsoft Internet Controls (shdocvw.dll) and
      'Microsoft HTML object Library.
      'Code will NOT run otherwise.
      
      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
      
      Set objIE = New SHDocVw.InternetExplorer
       
      With objIE
          .Navigate "http://your.website.com" ' Main page
          .Visible = 1
          Do While .READYSTATE <> 4: DoEvents: Loop
          Application.Wait (Now + TimeValue("0:00:02"))
            
          'set user name and password
          Set htmlDoc = .document
          Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
          Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
          For Each htmlInput In htmlColl
              If htmlInput.Name = "username" Then
                  htmlInput.Value = "your_usernname"
              Else
                  If htmlInput.Name = "password" Then
                      htmlInput.Value = "your_password"
                  End If
              End If
          Next htmlInput
          
          'click login
          Set htmlDoc = .document
          Set htmlColl = htmlDoc.getElementsByTagName("input")
          Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
          For Each htmlInput In htmlColl
              If Trim(htmlInput.Type) = "submit" Then
                  htmlInput.Click
                  Exit For
              End If
          Next htmlInput
      End With
      
      End Sub
      This code probably wont work for you -for your website, so dont come back to me saying that you cant get it to work

      You need to amend for your own HTML and play around with it. You WILL need to read up on

      •Microsoft Internet Controls
      •Microsoft HTML Object Library
      •getElementsByTagName, getElementsByID, getElementsByName

      See this link here for more discussion and links on the IE Object model
      http://www.ozgrid.com/forum/showthread.php?t=78229

      Especially post number 12.

      Ger
      Check out our new reputation system. Click on the Like button under the post!
      _______________________________________________

      There are 10 types of people in the world. Those that understand Binary and those that dont.

      Why are Halloween and Christmas the same? Because Oct 31 = Dec 25...

      The BEST Lookup function of all time

      Dynamic Named Ranges are your bestest friend

      _______________________________________________

      Comment


      • #4
        Re: VBA Code To Activate/Press Button On Web Page

        I tried to send the code last eve, but for some reason, it would not (maybe to long, and I was putting it in the message, not attaching it.
        Here is the code I have tried:

        Code:
        Private Sub ApplyandExit_Click()
         With BTS.document.BTSFeatureBean.Apply.Click
         End With
         
        End Sub
        
        Private Sub CancelChanges_Click()
        Dim i_CurRow As Integer
        
        i_CurRow = ActiveCell.Row
        If Not BTS Is Nothing Then
        whatpage (BTS)
        End If
        If Not BTS.Visible = True Then
           BTS.Visible = True
        End If
          With BTS.document.BTSFeatureBean.Apply.submit
        End With
        End Sub

        Here is a snippet of the HTML that shows the 2 buttons I want to click
        Code:
                <td width="84">
                        <div align="center">
                          <input type="button" value="Apply" class="button" onClick="return chkReqFields('2');">
                          <input type="hidden" name="action" value="CHANGE">
                          
                          
                        </div>
                      </td>
                      <td width="95">
                        <div align="center">
                          <input type="button" value="Close" class="button" onClick="javascript:window.close()">
                        </div>
                      </td>
        This page is being 'popped' from a previous page, BTSDetailBean.

        Thanks,
        Mc
        Attached Files

        Comment


        • #5
          Re: VBA Code To Activate/Press Button On Web Page

          Ok, some new input (Thanks Ger, you code lead me to this discovery!)
          Here is what happens...

          Code:
          Public Sub Press_Button()
               
               
               'make sure you add  references to Microsoft Internet Controls (shdocvw.dll) and
               'Microsoft HTML object Library.
               'Code will NOT run otherwise.
               
              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
               
              Set objIE = New SHDocVw.InternetExplorer
               
              With BTS
           '       .Navigate "http://your.website.com" ' Main page
                  .Visible = 1
          It switches BACK to the calling page.

          So, I guess my question is now...
          How can I control a page, that was not opened with excel, yet it is the active web page.
          I need to get focus here.

          Here is the button that I click to get to this page
          Code:
           <div align="left"> 
                                      <input  type="button" value="FEATURES" class="button" id="featid0" onClick="launchFeatures('0', 'FTFRCAAMPS0', 'S8495290037953744-1', '0', 'CTXG_MLHG', 'CHANGE', 'null');">
                                    </div>
          And here is the calling function
          Code:
          function launchFeatures(a, b, c, i, catg, act, racfpilot)
          {
             var m,u;
             var tn =  document.BTSDetailBean.subservtn[i].value;
             if ( tn == null || tn == "" ) 
             {
                alert ("Please enter subscriber telephone number")    
                return false;
              }
             
             u = "./feature.jsp?subidx=" + a + "&clli=" + b + "&subid=" + c + "&subtn=" + tn + "&catg=" + catg + "&action=" + act + "&racfpilotcode=" + racfpilot;
             MM_openBrWindow(u,'locked','scrollbars=1,resizable=1');     
             return true;
          }
          I hope this help to lead to a possible solution!

          Thanks
          Mc

          Comment


          • #6
            Re: VBA Code To Activate/Press Button On Web Page

            Hi
            I'm new to VBA and found this script to be extremely useful for a task I'm doing. Now that I can log into a web site from Excel, I have 2 questions.

            1. How can I use .Navigate to open additional pages in separate IE9 tabs?
            2. What do I need to add to this code so that I can save the opened web pages to my local disk?

            Any help will be greatly appreciated.

            Thanks in advance.

            Comment


            • #7


              Re: VBA Code To Activate/Press Button On Web Page

              Barriet,

              Welcome to Ozgrid.

              Please do not post your questions in threads started by others -- this is known as thread hijacking.
              *ALWAYS* start a new question for your thread and, if you find it helpful, provide a link to any thread you feel helps clarify your need.

              Thanks.
              AAE
              ----------------------------------------------------

              Forum Rules | Message to Cross Posters | How to use Tags

              Comment

              Working...
              X