In codeforexcelandoutlook, Jimmy Peña demostrated internet explorer automation, sending to and retrieving information from the web using VBA. In some cases that you might require a code to fill up string/numbers values to a web dialog form, here I will demonstrate how to add another possibility in automating internet explorer.
Some web forms make use of dialog boxes for users to fill up with string and/or number values. Most dialog boxes created are modal in nature, waiting for user to click on the ‘Ok’ or ‘Cancel’ buttons. In internet explorer automation, calling the modal dialog box with the element click would not suffice. The function or procedure would stop and wait for user to close the dialog box so as to continue the next code/s.
Open a webpage, open a web dialog window
Sub WebFormAutomation() Dim ie As Object 'SHDocVw.InternetExplorer Dim addlINFO As Object 'MSHTML.IHTMLElement Set ie = CreateObject("InternetExplorer.Application") With ie .navigate "http://www.websitehere.com/webform.aspx" .Visible = True End With '...codes before dialog box fill up... 'opens the dialog box Set addINFO = .getElementById("imgAdditional") addINFO.Click '...codes after user manually closes the dialog box... End Sub
A work around for this is to take a look on the attributes of the hidden input element associated with the calling of the dialog box. Before filling up the dialog box, the hidden input element has three attributes, namely: type, name, id.
<img src=”./Online System_files/icon_addition.gif” id=”DetailsList_imgAdditional” onclick=”ShowDialog(‘Additional.aspx?’,’DetailList_hfAdditional’,270,650)”>
<input type=”hidden” name=”DetailList$hfAdditional” id=”DetailList_hfAdditional”>
</td>
After manually filling up the dialog box, notice the change in the hidden input element. An attribute named “value”, with its corresponding string value, is added.
This gives the idea on what values the dialog box returns to the main form. To automate filling up the dialog box then, set up an attribute “value”, fill its value, then add to the main form’s hidden input element.
Open a webpage, fill in web dialog window fields
Option Explicit Sub WebFormAutomation() Dim ie As Object 'SHDocVw.InternetExplorer Dim inputElem As Object 'MSHTML.HTMLInputElement Dim addValue As Object 'MSHTML.IHTMLDOMAttribute Set ie = CreateObject("InternetExplorer.Application") With ie .navigate "http://www.websitehere.com/webform.aspx" .Visible = True End With '...code/s before dialog box fill up... 'option 1: using createAttribute With ie.document Set inputElem = .getElementById("hfAdditional") Set addValue = .createAttribute("value") addValue.nodeValue addlDetails inputElem.setAttributeNode addValue End With 'option 2: using setAttribute With ie.document Set inputElem = .getElementById("hfAdditional") inputElem.setAttribute "value", addlDetails End With '...code/s after dialog box fill up... End Sub
Then, create your custom addlDetails function/s, making use of URLEncode function to change some strings to valid url characters.
Function addlDetails() As String addlDetails = _ "<value><place>" & _ URLEncode("Manila, Philippines") & _ "</place><date>" & _ URLEncode("2011/04/15") & _ "</date><starttime>" & _ URLEncode("8:00") & _ "</starttime><endtime>" & _ URLEncode("17:00") & _ "</endtime><remarks>" & _ URLEncode("no comment") & _ "</remarks></value>" End Function