jdmurphy42
jdmurphy42

Reputation: 33

Open IE and Interact with Web Form using Excel VBA

I'm trying to create an excel spreadsheet with a landing page where I input a specific week number, period number and/or year.
I would then click a button to open an instance of Internet Explorer, navigate to the web form, input the data from the cells in the spreadsheet and then load up the reports selected.

Code for the web form

<br>





<body>

</body>
</html>


<head>
<!--include file="../../Connections/SQL.asp" -->





<title>Performance Reporting</title>

<link href="../../CSS/KPI_Style.css" rel="stylesheet" type="text/css">


<link href="../../CSS/KPI_Style.css" rel="stylesheet" type="text/css">
<link href="../CSS/KPI_Style.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style45 {color: #990000}
-->
</style>

<script type="text/javascript" language="javascript">
    <!--
     
    function makeDisable(){
    var y=document.getElementById("store")
    var x=document.getElementById("CHNSplits")
    if (y.value=='CP11')
    {    
    x.disabled=true
    }
}
function makeEnable(){
    var x=document.getElementById("CHNSplits")
    x.disabled=false
}

    -->
    </script>

</head>



<body>



<div align="center">
  
<link href="../CSS/PL_Style.css" rel="stylesheet" type="text/css" />

<style type="text/css">
<!--
.style2 {font-size: 16px}
.style33 {color: #ffffff}

-->
</style>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#001446" class="border9"><div align="left"><img src="80.gif" alt="DSGi"  /></div></td>
    <td valign="bottom" bgcolor="#001446" class="border16"><div align="right" class="xbig style1 style33"><span class="style2">Store Performance</span>&nbsp;</div></td>
  </tr>
</table>



<br>
<br>






<style type="text/css">
<!--
body,td,th {
    font-family: Arial, Helvetica, sans-serif;
}
body {
    background-image: url();
}
.style1 {
    color: #d33;
    font-size: 18px;
}
.style2 {color: #000000; font-size: 2px; }
.style10 {color: #d33}
.style15 {font-size: 12px}
-->
</style>
  <title></title>
  
</div>
<link href="../../../Testing/KPI/NewKPI/Digital_KPI/stylesheet.css" rel="stylesheet" type="text/css">
<div align="center"><span class="reportname"> Reporting Selection</span></div>
<form name="form1" id="form1" method="post" action="Get_Page_V4.asp">
<input name="store" type="hidden" value="2397" >
<input name="ViewType" type="hidden" value="store" >

  <table width="60%" border="1px" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
  
      <td colspan="2"><div align="center" class="formnamesmall">
          <table width="100%"  border="0" cellspacing="0" cellpadding="0">
            <tr height="4">
            <td></td>

          </tr>

          <tr height="5">
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
    
      
          <tr>
           <td colspan="6"><div align="center" class="formtextverylarge">Selected Location:</span> 1111</div></td>
            </tr>  
          <tr>
            <td><input name="RadioGroup1" type="radio" value="1" checked></td>
              <td class="formnamesmall" ><strong>Yesterday</td>
              <td class="formtext">&nbsp;</td>
              <td class="formtext">&nbsp;</td>            
              <td class="formtext">&nbsp;</td>
              <td class="formtext">&nbsp;</td>
          </tr>
          <tr height="15">
            <td height="10"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>              
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
          </tr>
          <tr class="formtext">
            <td><input type="radio" name="RadioGroup1" value="2"></td>
              <td class="formnamesmall" ></td>
              <td class="formtext">&nbsp;</td>
              <td class="formtext">&nbsp;</td>            
              <td class="formnamesmall"> </td>
              <td class="formtext">
<input name="datebox" type="hidden" id="datebox" value="07/10/2018" size="10" maxlength="10"></td>
          </tr>
          <tr height="15">
            <td height="10"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>              
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
          </tr>
          <tr>
            <td><input type="radio" name="RadioGroup1" value="3"></td>
              <td class="formnamesmall" ><strong>Week</td>
              <td class="formtext">&nbsp;</td>
              <td class="formtext">&nbsp;</td>            
              <td class="formnamesmall">Enter Week </td>
              <td class="formtext">
<input name="week" type="text" id="week" value="24" size="2" maxlength="2"></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
              <td class="formtext">&nbsp;</td>
              <td class="formtext">&nbsp;</td>            
              <td class="formtext">&nbsp;</td>
              <td class="formnamesmall">Enter Financial Year </td>
              <td class="formtext">
<input name="weekyear" type="text" id="weekyear" value="2019" size="4" maxlength="4"></td>
          </tr>
          <tr height="15">
            <td height="10"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>              
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
          </tr>
          <tr class="formtext">
            <td><label>
              <input type="radio" name="RadioGroup1" value="4">
            </label></td>
              <td class="formnamesmall" ><strong>Period</td>
              <td class="formtext">&nbsp;</td>
              <td class="formtext">&nbsp;</td>            
              <td class="formnamesmall">Enter Period </td>
              <td class="formtext">
<input name="period" type="text" id="period" value="6" size="2" maxlength="2"></td>
          </tr>
          <tr class="formtext">
            <td>&nbsp;</td>
              <td class="formtext">&nbsp;</td>
              <td class="formtext">&nbsp;</td>            
              <td class="formtext">&nbsp;</td>
              <td class="formnamesmall">Enter Financial Year </td>
              <td class="formtext">
<input name="periodyear" type="text" id="periodyear" value="2019" size="4" maxlength="4"></td>
          </tr>
         <tr height="15">
            <td height="10"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>              
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
          </tr>
         <tr height="15">
            <td height="10"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>              
              <td height="10" class="formtext"></td>
              <td height="10" class="formtext"></td>
          </tr>
          <tr class="formtext">
            <td><input type="radio" name="RadioGroup1" value="6"></td>
              <td class="formnamesmall" ><strong>Year </td>
              <td class="formtext">&nbsp;</td>
              <td class="formtext">&nbsp;</td>            
              <td class="formnamesmall">Enter Financial Year </td>
              <td class="formtext">
<input name="YearYear" type="text" id="YearYear" size="4" maxlength="4" value="2019" ></td>
          </tr>
            <tr><td height="10">&nbsp;</td>
            <td height="10">&nbsp;</td>
            <td height="10">&nbsp;</td>
            <td height="10">&nbsp;</td>
            <td height="10">&nbsp;</td>                                             
            <td height="10">&nbsp;</td>                                                         
            </tr>
            <!--
            <tr>
            <td colspan="4"><div align="center"><span class="formnamesmall style45">Select Report Type</span></div></td> 
            <td>&nbsp;</td>
            <td colspan="2" class="formname" ><strong></td>
            </tr>
            -->
            <td height="10">&nbsp;</td> 
            <td height="10">&nbsp;</td> 
            <td height="10">&nbsp;</td> 
            <td height="10">&nbsp;</td> 
            <td height="10">&nbsp;</td> 
            <td height="10">&nbsp;</td>                                                             
            <tr>
  <!--          
            <td><div align="center">
              <input name="RadioGroup2" type="radio" class="Style10" value="1" >
              </div></td> 
             
                        
            <td>
              <div align="right" class="formnamesmall style45">
                <div align="left">Charts </div>
              </div></td>
             -->
         
    
              <input name="RadioGroup2" type="hidden" class="Style10" value="6"  checked="Checked">
     
            <!--
            <td><div align="right" class="formnamesmall">
              <div align="left" class="style45">League Tables 
                </div>
            </div></td>
    -->         
            <td class="formnamesmall" colspan="4">&nbsp; </td>
            <td><input type="submit" name="Submit" value="Display Report"></td>
            </tr>
    
        </table>        

        <p> </p>

        </div></td>
    </tr>
  </table>
<input name="page_name" type="hidden" value="Region_Page1_" />
</form>
<br>
<br>
<style type="text/css">
<!--
.style1 {
    font-size: 10px;
    color: #FFFFFF;
}
-->
</style>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
  <td valign="bottom" bgcolor="#001446"><div align="right" class="xbig style1 style1">&nbsp;</div></td> 

  </tr>
</table>



</body>

VBA code

Sub Get_Data()
    Dim i As Long
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim HWNDSrc As Long
    Dim dates As Object
    
    Set IE = CreateObject("InternetExplorer.Application")
    
    With IE
        .Visible = True
        .navigate "http://INTRANETSITE/Summary_Select.asp"
    
        Do Until .readyState = 4 And Not .Busy
            DoEvents
        Loop
    
        .document.all.Item("radioGroup1")(3).Checked = True   'Select the 4th checkbox down on the list
        .document.forms(0).submit                             'Submit the form to generate the result
    End With
    
    Pause (2)
        
    With IE
        .navigate "http://INTRANETSITE/ReportDetail2.asp?"
        
        Do Until .readyState = 4 And Not .Busy
            DoEvents
        Loop
    End With
End Sub

The code above navigates to the web form and does select the 4th radio button which is for period reports, then submits the form and displays the first page.

How do I interact with the text boxes to also amend which week or period I want to select reports for?

Also, when it tries to navigate to ReportDetail2.asp it returns the following error and then crashes the VBA code.

Method 'Navigate' of object 'IWebBrowser2' failed

The long term goal of this is to navigate through 20+ pages of reports, pulling the data from each table into a tab on the spreadsheet, so if it can't navigate between the reports, even before we get to the code to pull the data, then that's a problem.

Upvotes: 2

Views: 1888

Answers (1)

QHarr
QHarr

Reputation: 84465

Try something like the following. I am showing the different options you might consider so remember to comment out those you don't want to use.

You need to add a reference to Microsoft Internet Controls via VBE > Tools > References or use late bound call of

Dim ie As New InternetExplorer : Set ie = CreateObject("InternetExplorer.Application")

VBA:

Option Explicit
Public Sub MakeSelections()
    Dim ie As New InternetExplorer
    Const URL As String = "yourURL"
    With ie
        .Visible = True
        .navigate URL

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .querySelector("[value='1']").Checked = True 'Yesterday. CSS attribute = value selector
            .querySelector("[value='3']").Checked = True 'Week
            .querySelector("[value='4']").Checked = True 'Period
            .querySelector("[value='6']").Checked = True 'Year
            .getElementById("week").Value = 6 'week value
            .getElementById("weekyear").Value = 2018 'financial year value
            .getElementById("period").Value = 6 'period value
            .getElementById("periodyear").Value = 2018 'period financial year value
            .getElementById("yearyear").Value = 2019 'financial year value
            .querySelector("[value='Display Report']").Click 'display report
        End With
        Stop '<== Delete me later
        '.Quit '<== Remember to quit application
    End With
End Sub

Upvotes: 1

Related Questions