Search code examples
javascripthtmlexcelvbainternet-explorer-11

Submitting form on child window


I have a vba script that automatically navigates its way through ups.com. It does just fine until it comes to a child window with a different url. In that child window is a simple form, but when the script submits the form it just refreshes the screen or an error saying the site is unavailable appears. The url to the child window is

https://wwwapps.ups.com/webClaims/address

and the relevant HTML on that window is

<script language="javascript" type="text/JavaScript">


 function updateReturn()
 {
 	"use strict" ;
 	opener.document.reportform.raddress.value = "true";
    opener.document.reportform.submit();
    top.close();
    }

 function updateDeliver()
 {
 	"use strict" ;
    opener.document.reportform.daddress.value = "true";
    opener.document.reportform.submit();
    top.close();
 }

 function updateContact()
 {
 	"use strict" ;
    opener.document.reportform.caddress.value = "true";
    opener.document.reportform.submit();
    top.close();
 }

 function updateOther()
 {
 	"use strict" ;
    opener.document.reportform.oaddress.value = "true";
    opener.document.reportform.submit();
    top.close();
 }

 function submitRefresh()
 {
 	"use strict" ;
    this.document.address.refresh.value = "true";
    document.address.submit();
 }

</script>


<div id="main">




<form name="address" action="/webClaims/address" method="post">
<input type="hidden" name="loc" value="en&#95;US">

<input type="hidden" name="addrtype" value="f">

<input type="hidden" name="refresh" value="">


<table border="0" cellpadding="0" cellspacing="0" width="566">
<tr>
	<td valign="top" width="422">

	
	<table border="0" cellpadding="0" cellspacing="0" width="100%">
	<tr>
		<td class="pi-mA1-t" valign="top">

		
		<table border="0" cellpadding="0" cellspacing="0" width="422">
		<tr>
			<td class="pi-m-txt-title">
        
          Edit Shipped From Address
        
        
        
        
        

      </td>
		</tr>
		</table>
		

		</td>
	</tr>
	<tr>
		<td class="pi-mA1-c-t-spc"><img alt="" border="0" height="1" src="/img/1.gif" width="1"></td>
	</tr>
	<tr>
		<td class="pi-mA1-c" valign="top">

  		
  		<table border="0" cellpadding="0" cellspacing="0" width="100%">
  		<tr>
  			<td>
          <div class="pi-m-pad">
            
            Please edit the address below. Required fields are shown in <span class="pi-h4">bold</span>.<br>
            <br>



            
              <span class="pi-h4">Company Name:</span>
            
            
            <br>
            
              <input class="modTxtMedium" name="companyName" maxlength="50" size="19" type="text" value="">
            
            
            <br><br>
    

            
              <span class="pi-h4">Name:</span>
            
            
            <br>
            
              <input class="modTxtMedium" name="name" maxlength="50" size="19" type="text" value="">
            
            
            <br><br />

    
              <span class="pi-h4">Country:</span>
    
    
            <br>
            <select name="country" class="modSelMedium" onChange="javascript:submitRefresh()" >
              
                <option SELECTED value="US">United States</option>
              
                <option  value="CA">Canada</option>
              

            </select>
            <br><br>

    

            
              <span class="pi-h4">Address Line 1:</span>
            
            
            <br>
            
              <input class="modTxtMedium" name="address1" maxlength="50" size="19" type="text" value="">
            
            
            <br><br>

            Address Line 2:
            <br>
                <input class="modTxtMedium" name="address2" maxlength="50" size="19" type="text" value="">
                <br>
                <span class="reqTxtInst">Apartment, suite, unit, building, floor, etc. </span>
            <br><br>

            Address Line 3:
            <br>
                <input class="modTxtMedium" name="address3" maxlength="50" size="19" type="text" value="">
            <br>
            <span class="reqTxtInst">Department, c/o, etc. </span>
            <br><br>

            
              <span class="pi-h4">City:</span>
            
            
            <br>
            
            
              <input class="modTxtMedium" name="city" maxlength="50" size="19" type="text" value="WALNUT">
            
            <br><br>

            
              
                  <span class="pi-h4">State:</span>
              
              
            

            
            <br>
            <select name="state" class="modSelMedium" size="1">
              
                <option  value="AL">Alabama</option>
              
                <option  value="AK">Alaska</option>
              
                <option  value="AZ">Arizona</option>
              
                <option  value="AR">Arkansas</option>
              
                <option SELECTED value="CA">California</option>
              
                <option  value="CO">Colorado</option>
              
                <option  value="CT">Connecticut</option>
              
                <option  value="DC">District of Columbia</option>
              
                <option  value="DE">Delaware</option>
              
                <option  value="FL">Florida</option>
              
                <option  value="GA">Georgia</option>
              
                <option  value="HI">Hawaii</option>
              
                <option  value="ID">Idaho</option>
              
                <option  value="IL">Illinois</option>
              
                <option  value="IN">Indiana</option>
              
                <option  value="IA">Iowa</option>
              
                <option  value="KS">Kansas</option>
              
                <option  value="KY">Kentucky</option>
              
                <option  value="LA">Louisiana</option>
              
                <option  value="ME">Maine</option>
              
                <option  value="MD">Maryland</option>
              
                <option  value="MA">Massachusetts</option>
              
                <option  value="MI">Michigan</option>
              
                <option  value="MN">Minnesota</option>
              
                <option  value="MS">Mississippi</option>
              
                <option  value="MO">Missouri</option>
              
                <option  value="MT">Montana</option>
              
                <option  value="NE">Nebraska</option>
              
                <option  value="NV">Nevada</option>
              
                <option  value="NH">New Hampshire</option>
              
                <option  value="NJ">New Jersey</option>
              
                <option  value="NM">New Mexico</option>
              
                <option  value="NY">New York</option>
              
                <option  value="NC">North Carolina</option>
              
                <option  value="ND">North Dakota</option>
              
                <option  value="OH">Ohio</option>
              
                <option  value="OK">Oklahoma</option>
              
                <option  value="OR">Oregon</option>
              
                <option  value="PA">Pennsylvania</option>
              
                <option  value="RI">Rhode Island</option>
              
                <option  value="SC">South Carolina</option>
              
                <option  value="SD">South Dakota</option>
              
                <option  value="TN">Tennessee</option>
              
                <option  value="TX">Texas</option>
              
                <option  value="UT">Utah</option>
              
                <option  value="VT">Vermont</option>
              
                <option  value="VA">Virginia</option>
              
                <option  value="WA">Washington</option>
              
                <option  value="WV">West Virginia</option>
              
                <option  value="WI">Wisconsin</option>
              
                <option  value="WY">Wyoming</option>
              
            </select>
            <br><br>

            
              <span class="pi-h4">Postal Code:</span>
            
            
            <br>
            
              <input class="modTxtMedium" name="postal" maxlength="9" size="19" type="text" value="">
            
            
            <br><br>

     

  			  </div>
  			</td>
  		</tr>
  		</table>
  		




      <table border="0" cellpadding="0" cellspacing="0">
  		<tr>
  			<td>
          <div class="pi-m-pad">
            
              <span class="pi-h4">Telephone:</span>
            
            
            <br>
            
              <span class="pi-h4"><input class="modTxtMedium" name="telephone" maxlength="15" size="19" type="text" value="">
            
            

            <br>
          </div>
        </td>
  			<td>
          
            Ext:
          
          
          <br>
          
            <input class="modTxtSmall" name="extension" maxlength="5" size="4" type="text" value="">
          
          
     			<br>
  			</td>
  		</tr>
  		</table>
  	</td>
	</tr>
	<tr>
		<td class="pi-mA1-c-b-spc"><img alt="" border="0" height="1" src="/img/1.gif" width="1"></td>
	</tr>
	<tr>
		<td class="pi-mA1-line"><img alt="" border="0" height="1" src="/img/1.gif" width="1"></td>
	</tr>
	<tr>
		<td class="pi-mA1-b" valign="top">

		
		<table border="0" cellpadding="0" cellspacing="0" width="422">
		<tr>
			<td class="pi-m-button" nowrap><input type="image" alt="Update" name="update_address" src="/img/en/btn_update.gif" WIDTH="51" HEIGHT="18"></td>
		</tr>
		</table>
		

		</td>
	</tr>
	</table>
	

	</td>
	<td width="10"><img alt="" border="0" height="1" src="/img/1.gif" width="10"></td>
	<td valign="top" width="134"><img alt="" border="0" height="1" src="/img/1.gif" width="134"></td>
</tr>
</table>


</form>

To fill out the form I simply do this and it works okay

IE.Navigate "https://wwwapps.ups.com/webClaims/address?addrtype=f&loc=en_US"
'IE.Document.Focus
Sleep (5000)
 IE.Document.getElementsByName("companyName")(0).Value = "COMPANY"
 IE.Document.getElementsByName("name")(0).Value = "n\a"         
 IE.Document.getElementsByName("address1")(0).Value = "222 address"
 IE.Document.getElementsByName("postal")(0).Value = "99999"
 IE.Document.getElementsByName("telephone")(0).Value = "111-111-1111"

But then when it comes to submitting the form, I run into trouble. As you can see, submitting the form takes you to the url that produced the form in the first place

  <form name="address" action="/webClaims/address" method="post">

So no wonder the form doesn't go anywhere when I do

 IE.Document.getElementsByName("address")(0).submit

I was looking at the javascript functions they have and also tried this

Call IE.Document.parentWindow.execScript("submitRefresh", "JavaScript")

but with the same result.

When I try

Call IE.Document.parentWindow.execScript("updateContact()", "JavaScript")

I get this error

Could not complete the operation due to error 80020101

I've also tried to click on the button that appears to submit the form

<input type="image" alt="Update" name="update_address" src="/img/en/btn_update.gif" WIDTH="51" HEIGHT="18">

but it says that the input tag is not clickable.

Am I missing something about the HTML or JavaScript that requires a different approach than the usual? Do I not understand something about child windows that makes my approach unworkable?


Solution

  • So as it turn out you can interact with the child window like so and identify it by its url

    Dim objShell, IE_count, x, my_url, childPage
    'Instead of opening the child window as if it were a web page with a url, treat it as a mere popup window
    Set objShell = CreateObject("Shell.Application")
    IE_count = objShell.Windows.Count
    For x = 0 To (IE_count - 1)
        On Error Resume Next  ' sometimes more web pages are counted than are open
        'We find which window is the popup by looking at its url
        my_url = objShell.Windows(x).Document.Location
        'Debug.Print my_url
        'The url of the popup
        If my_url = "https://wwwapps.ups.com/webClaims/address?addrtype=f&loc=en_US" Then
            'We found it, so make it a shell object for us to work with
            Set childPage = objShell.Windows(x)
            Exit For
        End If
    Next
    

    I borrowed heavily from here.

    I might have attempted to grab the child window by getting its title, but it had the same title as the parent window, so url was better for this reason. After that I could fill out the form and submit it using the childPage object and its methods and attributes.