Unable to extract Text using VBA, when there are multiple Classname , tagname and attributes with same name - excel

I am trying to extract the innertext under the 'span' class from a IE browser, but facing an issue. Below is the HTML code
1st html snippet
<div class="SectionStart" style="display: block;" oraload="oraDisplayNone(item,'newDev','Y','=')" oraInitDisplayStyle="block">
<label oramdlabel="CM_SN">Serial Number</label>
<!--style tag has been added-->
<span class="oraDefault" style="left: -5px; top: 4.5px; position: relative;" orafield="MSerialNumber" oraType="string;precision:20" oraErrorElement="mSerialNumber">G4A001</span>
</div>
2nd html snippet
<div class="SectionStart">
<label oramdlabel="CM_SN">Serial Number:</label>
<!--style tag has been added-->
<span class="oraDefault" style="left: -5px; top: 4.5px; position: relative;" orafield="MSerialNumber" oraType="string;precision:20" oraErrorElement="mSerialNumber">E5W807</span>
</div>
Here the issue is as per below code ,while trying to fetch the 2nd value "E5W807", I am only fetching the not required value "G4A001".
Please guide how can I tackle the problem of ONLY extracting the text "E5W807".
Set objSubCollec = objCollection(0).contentWindow.document.getElementById("Page")
Set objElement = objSubCollec.contentWindow.document.getElementById("Frame_4")
objElement.Focus
Set objElement1 = objElement.contentWindow.document
Set elm2 = objElement1.getElementsByTagName("span")
For Each e1 In elm2
If e1.getAttribute("orafield") = "MSerialNumber" Then
temp = e1.innerText
'MsgBox temp
Worksheets("Sheet1").Range("B6") = temp
Exit For
End If
Next

Maybe Try this:
It will print all the found numbers in succession from B6.
Set objSubCollec = objCollection(0).contentWindow.document.getElementById("Page")
Set objElement = objSubCollec.contentWindow.document.getElementById("Frame_4")
objElement.Focus
Set objElement1 = objElement.contentWindow.document
Set elm2 = objElement1.getElementsByTagName("span")
i = 6
For Each e1 In elm2
If e1.getAttribute("orafield") = "MSerialNumber" Then
temp = e1.innerText
Worksheets("Sheet1").Range("B & i") = temp
i = i + 1
End If
Next

Related

How to add a tick to a checkbox through excel vba ie automation?

The checkbox I am trying to add a tick to is part of an online table and doesn't seem to be coded as a checkbox.
I have tried the following to add a tick and none work:
IE.Document.getElementByID("gridcolumn-1658-titleEl").Click
IE.Document.getElementByID("gridcolumn-1658-textEl").Click
IE.Document.getElementsByClassName("x-column-header-inner x-column-header-over")(0).Click
IE.Document.getElementsByClassName("x-column-header-inner")(0).Click
When I inspect the element, I get to the following, but none of this looks anything like a checkbox (the third one is the actual reference):
<div class="x-column-header x-column-header-checkbox x-column-header-align-left x-box-item x-column-header-default x-unselectable x-column-header-first" style="border-width: 1px; width: 24px; right: auto; left: 0px; top: 0px; margin: 0px; height: 24px;" id="gridcolumn-1658"><div id="gridcolumn-1658-titleEl" class="x-column-header-inner" style="padding-top: 6px; padding-bottom: 6px;"><span id="gridcolumn-1658-textEl" class="x-column-header-text"> </span></div></div>
<div id="gridcolumn-1658-titleEl" class="x-column-header-inner" style="padding-top: 6px; padding-bottom: 6px;"><span id="gridcolumn-1658-textEl" class="x-column-header-text"> </span></div>
<span id="gridcolumn-1658-textEl" class="x-column-header-text"> </span>
The website is OptimoRoute, which can be accessed fairly quickly using a new log in, for those interested!
The button I am trying to click is the top one in the table.
Please refer to the following sample code:
Sub main()
'we define the essential variables
Dim IE As Object, Data As Object
Dim ticket As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("https://dillion132.github.io/vbacheckbox.html")
While IE.ReadyState <> 4
DoEvents
Wend
Set Data = IE.Document.getElementsByClassName("check")
Debug.Print Data.Length
If Len(Data) > 0 Then
For Each ee In Data
'Debug.Print ee.Value
'Based on the checkbox value to check/uncheck the checkbox.
If ee.Value = "Cat" Then
ee.Checked = True
End If
'check whether the checkbox is checked, then, get the checked value.
If ee.Checked Then
Debug.Print ee.Value & " is checked"
End If
Next ee
End If
End With
Set IE = Nothing
End Sub
Code in the website:
<input class="check" type="checkbox" value="Cat"> Cat </input>
<br />
<input class="check" type="checkbox" value="Dog" >Dog</input>
<br />
<input class="check" type="checkbox" value="Pig" checked ="checked" >Pig</input>
The result as below:

Read straight web content with Excel VBA

there are many article on this site on how to read tags and tables in web sites with Excel VBA, but I am stuck here.
This website gives me business locations after entering a Zip code.
("Where is the closest location relative to my Zip Code")
I managed to navigate to the site, enter the Zip code and click Submit:
Dim Browser As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Set Browser = New SHDocVw.InternetExplorer ' create a browser
Browser.Visible = True ' make it visible
Application.StatusBar = ".... opening page"
Browser.navigate "https://www.thewebsite.com" ' navigate to page
WaitForBrowser Browser, 1 ' wait for completion or timeout
Application.StatusBar = "gaining control over DOM object"
Set HTMLDoc = Browser.document ' load the DOM object
WaitForBrowser Browser, 1
HTMLDoc.getElementById("ZipCode").Value = "28278"
HTMLDoc.getElementById("localTeamZipSubmit").Click
The site opens and the relevant content looks like this:
<div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 70px;">
Location:
</div>
<div class="column paddingTopFive">CHARLOTTE</div>
</div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 120px;">
Location Number:
</div>
<div class="column paddingTopFive">102340</div>
</div>
<div class="columns">
<div class="column boldText paddingTopFive paddingLeftTwenty" style="vertical-align: top;">
Address:
</div>
<div class="column paddingTopFive paddingLeftTwenty">
<div>8848 Main St.</div>
<div>Suite F</div>
<div></div>
<div>Charlotte, NC 27218</div>
</div>
</div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 70px;">
Phone:
</div>
<div class="column paddingTopFive">(704) 911-4440</div>
</div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 70px;">
Fax:
</div>
<div class="column paddingTopFive">(704) 911-4441</div>
</div>
</div>
As you can see, this section has no table, no named tags and classes that are use over and over.
I was not able to read this information yet. I would be happy to get the whole blob into a String and parse it"
"Text = HTMLDoc.getEverything()"
Thanks a lot for your help!!!
In the meantime I found another code snippet that I modified but I am getting stuck at the same point:
Post and submit works but how to get the answer....
{ Private Sub PostalCodes()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
On Error GoTo errHandler
ie.Visible = 1
With ie
.navigate "https://www.pattersondental.com/ContactUs/MyLocalTeam"
Do While .busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
With .document.Forms("GetBranchFromZipForm")
.ZipCode.Value = "28273"
.submit
End With
' Do While Not CBool(InStrB(1, .document.URL, _
' "cp_search_response-e.asp"))
' DoEvents
' Loop
Do While .busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
' MsgBox .document.all.tags("Colums").Item(1).Rows(1).Cells(1).innerText
MsgBox .document.all.tags("Colums").innerText
' MsgBox .document}
I guess I have to search no for "how to dissect a HTML document"...
Add on:
It seems that while ie is a valid item (in the watch window) IE.Document is empty... why can this be, The website is still there with new data.
I even tried another code snippet that looks for open websites in IE, it finds the site (with the correct data) but the document is still empty and getelementBY... does not find anything of course.
I am about to start drinking...
I can't believe it.
After 3 days of poking I found this:
With ActiveSheet.QueryTables.Add(Connection:="URL;
https://www.pattersondental.com/ContactUs/MyLocalTeam",
Destination:=Range("A1"))
.PostText = "ZipCode=70032"
.RefreshStyle = xlOverwriteCells
.SaveData = True
.Refresh
I don't pretend to understand why it works, but is does.
John, I will still check out, what you suggested. Thanks

Trying to select an item in kendogrid kendoDropDownList using Excel VBA

I'm trying to select an item on a dropdown list on an internal company web page. I am able to set text entry items and get the dropdownlist to open, but I' having a hard time figuring out how to make a selection
In other area I'm able to make a selection using the ID and td/tr tags, but this routine doesn't have any tr/td tags with entry names.
Here'a couple of things I've tried so far:
These two items perform the same function and work fine for opening the first dropdown
ie.Document.getElementById("FromDistrict").Click
ie.Document.parentWindow.execScript "$('#FromDistrict').kendoDropDownList('open');"
I've tried several variations of these:
ie.Document.parentWindow.execScript "$('#FromDistrict').data('kendoGrid').dataItem($('transport').data('kendoDropDownList').select('KILGORE'));"
ie.Document.parentWindow.execScript "$('#FromDistrict').data('kendoGrid').data('kendoDropDownList').select('KILGORE'));"
ie.Document.parentWindow.execScript "$('#FromDistrict').data('kendoGrid').data('kendoDropDownList').select('eq:0'));"
ie.Document.parentWindow.execScript "$('#FromDistrict').data('kendoDropDownList').select('KILGORE'));"
ie.Document.parentWindow.execScript "$('#FromDistrict').select('KILGORE'));"
Viewing the source code for this particular segment is:
</div>
<form action="/TransferLoad/Add" method="post"><input name="__RequestVerificationToken" _
type="hidden" value="IP80d5XM-Qi0XQ1-IgGKGmhLVNGdtDAyM-r7lJ6yQCI1RIdJJph0uPnz-DzEHx12_booO4xwvcWg6EUWPiLnHv7ww6PD-aqfhiVxPcy-VYm6mnBRHsba3H7Hembliybo0" /> _
<div class="k-block k-info-colored">
<div class="k-header">
<span>Add Transfer Load Details</span>
</div>
<div class="k-content">
<div class="infocontainer">
<table>
<tr>
<td class="columnLabel">
<label for="From_District:">From District:</label>
</td>
<td class="columnData">
<input id="FromDistrict" name="FromDistrict" style="width: 200px" type="text" />
<script>
jQuery(function(){jQuery("#FromDistrict").kendoDropDownList({"dataSource" _
:{"transport":{"read": {"url":"/DistrictProfiles/GetUserDistricts","data": _
function() { return kendo.ui.DropDownList.requestData(jQuery("#FromDistrict")); }}, _
"prefix":""}, "serverFiltering":true,"filter":[],"error":OnError, _
"schema": {"errors":"Errors"}}, "dataTextField":"DistrictName","autoBind":true, _
"dataValueField":"DistrictCode", "optionLabel":"Select District..."});});
</script>
</td>
<td class="columnLabel"> 'Next dropdown section starts here
<label for="To_District:">To District:</label>
</td>
When the dropdown opens, it has 2 items to choose from, but nowhere in the code can I find those 2 items listed, so I'm assuming they're pulled from this line: return kendo.ui.DropDownList.requestData(jQuery("#FromDistrict")) , but I'm not sure. Can someone point out what I'm missing here ?
I did not post the "view element" because of the difficulty in copying it. All selections dynamically change other selection options.
IN the browser DOM explorer (which shows the markup 'computed/sanitized' by the rendering engine) you should see that
<input id="FromDistrict" name="FromDistrict" style="width: 200px" type="text" />
has been changed to include the datalist attribute. eg.
<input id="FromDistrict" name="FromDistrict" style="width: 200px" type="text" datalist="DistrictName" />
..... and further down the DOM, you should see the datalist element that has been injected into the DOM by the Kendo code.
<datalist id="DistrictName">
<option value="Kent">Kent</option>
<option value="Surry">Surry</option>
</datalist>
You should be able to automate the field, by just assigning a valid comma-separated list to the FromDistrict.
eg. FromDistrict.value='Kent, Surry';
I was able to accomplish what I wanted in a crude sort of way by using the following, but I'm working on a better more efficient way.
'Choose the FROM district
ie.Document.parentWindow.execScript "$('#FromDistrict').kendoDropDownList('open');"
Dim FrDist, li
Set FrDist = ie.Document.getElementById("FromDistrict-list").getElementsByTagName("li")
Dim fd
fd = 0
For Each li In FrDist
'MsgBox ("li.innertext is - " & li.innerText & " fd value is: " & fd)
If li.innerText Like "*KILGORE*" Then
FrDist(fd).Click
Else
'Do Nothing
End If
fd = fd + 1
'Application.Wait (Now + TimeValue("0:00:02"))
Next
Application.Wait (Now + TimeValue("0:00:02"))
'Choose the TO district
ie.Document.parentWindow.execScript "$('#ToDistrict').kendoDropDownList('open');"
Dim ToDist
Set ToDist = ie.Document.getElementById("ToDistrict-list").getElementsByTagName("li")
Dim tod
tod = 0
For Each li In ToDist
'MsgBox ("li.innertext is - " & li.innerText & " tod value is: " & tod)
If li.innerText Like "*KILGORE*" Then
ToDist(tod).Click
Else
'Do Nothing
End If
tod = tod + 1
Next

How to set the value of the input name "PickupDate" using VBA?

I have tried the following code but obtain an Object error:
Dim g As IHTMLElement
Set g = appIE.getElementsByName("PickupDate")
g.Value = b
<div class="date date-time-field" id="pickup-date">
<div class="date-icon icon"></div>
<input name="PickupDate" class="short picker__input" id="P666421526" aria-expanded="false" aria-haspopup="true" aria-readonly="false" aria-owns="P666421526_root" type="text" readonly="" placeholder="Date">
</div>
Give this a shot:
For a particular value:
Set post = html.getElementsByName("PickupDate")
MsgBox post(0).ID
Looping through multiple values:
For Each post In html.getElementsByName("PickupDate")
MsgBox post.ID
Next post

VBA IE 11 Runtime

I have coded VBA in IE 11 before, and have been able to work around problems, but on this I keep getting an Error 91 or Error 424. While sometimes it works perfectly.
Unfortunately I cannot share the exact site since its password protected, but I am including the source code that's relevant.
I do option explicit, so there should be no problem with my variables.
These are all the different ways I have tried to fill the box with yesterday's date, and none of them work.
html.getElementById("begin")(1).Value = Format(Date - 1, "mm/dd/yyyy") '<-error 91
Set StartDate = html.getElementById("begin") ‘<- Error 424 or Erorr 80010108
Set EndDate = html.getElementById("end")
Set CampaignID = html.getElementById("campaign_id")
These are the 4 different things I am trying, but they all work periodically.
StartDate.Item(0).Value = Format(Date - 1, "mm/dd/yyyy") ‘<-Error 91
StartDate.Value = Format(Date - 1, "mm/dd/yyyy")
StartDate.Value(0) = Format(Date - 1, "mm/dd/yyyy")
Set StartDate(0).Value = Format(Date - 1, "mm/dd/yyyy
Source Code:
<div class="row form-group">
<div class="col-xs-12">
<div class="row">
<div class="pull-left text-left" style="width:115px;margin-left:20px">
<label for="">From :</label>
</div>
div class="col-xs-3">
<input type="text" name="begin" value="" class="form-control input-sm datepicker" id="begin" />
</div>
<div class="col-xs-1 text-right">
<label for="">To :</label>
</div>
<div class="col-xs-3">
<input type="text" name="end" value="" class="form-control input-sm datepicker" id="end" />
</div>
</div>
</div>
</div>
I figured it out.
I was refreshing the page after I signed in. This reset the permissions and was blocking me from changing any of the text boxes. Not sure why, but it was half of the problem.
Then, I changed the variable editing to run through the whole list of "input"s on the page, as seen below:
Dim CampaignId, StartDate, EndDate, RunReport As Object
Set StartDate = appIE.Document.getelementbyid("begin")
Set EndDate = appIE.Document.getelementbyid("end")
Set CampaignId = appIE.Document.getelementbyid("campaign_id")
Set RunReport = appIE.Document.getElementsByName("_action_report")
ButtonNum = 0
Set objCollection = appIE.Document.getElementsByTagName("input")
While ButtonNum < objCollection.Length
If objCollection(ButtonNum).Name = "begin" Or _
objCollection(ButtonNum).Name = "end" Then
Set objElement = objCollection(ButtonNum)
objCollection(ButtonNum).Value = Format(Date - 1, "mm/dd/yyyy")
End If
ButtonNum = ButtonNum + 1
Wend
This way found the buttons on the page where the getElementById failed.
Thanks for looking!

Resources