Select Drop Down from Web Page with IE object - vba

Have the below code for a drop down selection in VBA .
When I run this I get the error on the bold line saying "Runtime error 91 - Object variable or with block variable not set " ..New to VBA ....
Sub NACDP()
' open IE, navigate to the desired page and loop until fully loaded
Set ie = CreateObject("InternetExplorer.Application")
my_url = "https://cdeployna.cognizant.com/"
With ie
.Visible = True
.Navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
' Input the userid and password
ie.Document.getElementById("loginControl_UserName").Value = ""
ie.Document.getElementById("loginControl_Password").Value = ""
' Click the "Login" button
ie.Document.getElementById("loginControl_LoginButton").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.Document.getElementById("ctl00_ddlRoles").selectedindex = 1
ie.Document.getElementById("ctl00_ddlRoles").FireEvent ("onchange")
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.Document.getElementById("ctl00_ContentBody_searchCDPList_ddlFieldName").selectedindex = 1
ie.Document.getElementById("ctl00_ContentBody_searchCDPList_ddlFieldName").FireEvent ("onchange")
ie.Document.getElementById("ctl00_ContentBody_searchCDPList_txtValue").Value = "Java"
' Click the "Search" button
ie.Document.getElementById("ctl00_ContentBody_searchCDPList_btnSearch").Click
End Sub

It seems that the "Do Until Not ie.Busy And ie.readyState = 4" loop doesn't really do what it's supposed to do. After getting the error, if you press Debug and then Continue the code, it will complete successfully. So, what is happening is that the element you're searching for still doesn't exist when the line first executes. You could patch this up with something like the following before the problem line:
Do While ie.Document.getElementById("ctl00_ContentBody_searchCDPList_ddlFieldName") Is Nothing
DoEvents
Loop
This will loop until the element is found, then the code continues successfully.

Related

Getting 438 error while entering data on textbox on IE. This text box is on another TAB

I am doing some automation on IE browser. On this i am logging into the web application and after logging when i am clicking on botton the web opens a new tab where i need to enter the data onto the textbox but here i am getting an error 438. below is my code. please help me out. i highlighted the line in bold where i am getting an error. The last one i am clicking on end tab and i am getting an error on that
Sub upload()
Dim myValue As Variant
Dim IETab1Number As Integer
Dim IETab2Number As Integer
Dim IETab3Number As Integer
' open IE, navigate to the desired page and loop until fully loaded
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://englogin.sbc.com/ELP/"
With IE
.Visible = True
.Navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
' Input the userid and password
IE.document.getElementById("GloATTUID").Value = "Zf9775"
IE.document.getElementById("GloPassword").Value = "MrsCooper$99"
' Click the "Login" button
IE.document.getElementById("GloPasswordSubmit").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
' Click the "Continue" button
IE.document.getElementById("successButtonId").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
' Click the "MIC" button
IE.document.getElementById("btnMechanizedInventoryCreation").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
Set html = IE.document
html.querySelector("a[href='#MIC']").Click
IE.document.querySelector("[name=telco11iProjectNumber]").Value = "123456"
' Enter JOB Number" button
'myValue = InputBox("Give me some input")
End Sub

Object variable or with block variable not set - error '91'

Sub Two()
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://example.com/market/listings/578080/Sneakers%20(WHITE)"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd27 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D27").Value = Srd27
IE.Navigate "http://example.com/market/listings/578080/Floral%20Shirt%20(Black)"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd28 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D28").Value = Srd28
IE.Navigate "http://example.com/market/listings/578080/Tracksuit%20Top%20(Yellow)"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd29 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D29").Value = Srd29
IE.Navigate "http://example.com/market/listings/578080/School%20Jacket"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd30 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D30").Value = Srd30
IE.Navigate "http://example.com/market/listings/578080/Leather%20Bootcut%20Pants"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd31 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D31").Value = Srd31
IE.Quit
End Sub
If i use F8 key in Visual Basic app it works sometimes. But when I use macros in Excel its saying
'object variable or with block variable not set - error '91''
I have tested it and from what I can see it's that the address "http://steamcommunity.com/market/listings/578080/Sneakers%20(WHITE)" gives you a page saying that the product could not be found.
The method getElementsByClassName generates error because the element you are looking for is not available on the loaded page.
Try something like this:
Sub Two()
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://steamcommunity.com/market/listings/578080/Sneakers%20(WHITE)"
Do: DoEvents: Loop Until IE.ReadyState = 4
on error resume next
Srd27 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
on error goto 0
If Srd27 <> "" then
ActiveSheet.Range("D27").Value = Srd27
Else
ActiveSheet.Range("D27").Value = "Product not found"
end if
'repeat for the rest of the code
IE.Quit
End Sub
And of course I am always encouraging to use Option Explicit and declare all variables.
You need to set the object and validate its state before trying to access its properties.
getElementsByClassName()
Returns a collection of objects with the same class attribute value.
Sub Two()
Dim IE As Object, Srd27 As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://steamcommunity.com/market/listings/578080/Sneakers%20(WHITE)"
Do: DoEvents: Loop Until IE.ReadyState = 4
Set Srd27 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0)
If Not Srd27 Is Nothing Then
ActiveSheet.Range("D27").Value = Srd27.innerText
End If
End Sub
Edit:
To get multiple results, you have to loop through the elements collection and get the innerText of each element.
Dim elements As Object, element as Object
Set elements = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")
If Not elements Is Nothing Then
For Each element in elements
Debug.Print element.innerText
Next
End if

Macro to print selection of cells from another workbook

I'd need to click one button in one Excel workbook and automatically opens another workbook from a website and prints a selection of cells from this workbook.
Until now I have the code that opens me the workbook form the website, but I would need to include the part of printing a selection of cells.
Thank you in advance!
Here is the code I have:
Sub Project4000()
'open IE, navigate to the desired page and loop until fully loaded
Set IE = CreateObject("InternetExplorer.Application")
my_url = "Link to the file in the website"
With IE
.Visible = True
.Navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
' Input the userid and password
IE.Document.getElementById("uid").Value = "user"
IE.Document.getElementById("password").Value = "passw"
' Click the "Search" button
IE.Document.getElementById("enter").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End Sub

How Do I select from DropDown Menu that Does not have an ID

My Code:
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
................
IE.Navigate "https://www./CTMT"
Do While IE.Busy Or IE.ReadyState <> 4: Loop
Application.Wait (Now + TimeValue("00:0:03"))
Set HTMLDoc2 = IE.document
HTMLDoc2.getElementsByName("merchID")(0).Value = "aa"
HTMLDoc2.getElementByName("selDate")(0).SelectedIndex = 2
'HTMLDoc2.getElementByName("selDate")(0).FireEvent ("onchange")
'HTMLDoc2.getElementsByName("Search")(0).Submit
HTMLDoc2.Forms("onlineReportFormBean").Submit
Do While IE.Busy Or IE.ReadyState <> 4: Loop
End Sub
Q) I am able to fill the text box next to Merchant ID but I am Unable to select the second Index...May2016 using the above code.
HTMLDoc2.getElementByName("selDate")(0).SelectedIndex = 2
Above line gives the error : Object doesnt support this property or method
Q2) I am also unable to click on "search".
HTMLDoc2.Forms("onlineReportFormBean").Submit
Line gives automation error..

"Run-time automation error -2147417848 (80010108)"

I use VBA excel to parse a long list of local .htm files. The problem is that I get an error even before the programm starts to parse the HTM-files.
Error is:
VBA code:
<!-- language: lang-html -->
Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub ImportHTM()
'Dim ie As InternetExplorer
Dim ie As InternetExplorerMedium
Dim html As HTMLDocument
Set ie = New InternetExplorerMedium
'Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "d:\Cloud\Dropbox\3.htm"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Loading Profile..." 'PROBLEM SEEMS TO BE HERE SOMEWHERE!
DoEvents
Loop
Set html = ie.document
Set ie = Nothing
Application.StatusBar = ""
'code code code --> which at this point isn't executed because the error occures before
Do you have any ideas what could cause the problem? Do you have any solution suggestions?
Also the command:
ie.Visible = False
doesn't seem to have any effect whatsoever since it opens the HTM-file in a new IE window.
Move the status bar update out of the loop.
Application.StatusBar = "Loading Profile..."
Do While ie.Busy Or ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Application.StatusBar = vbNullString
There is no need to rewrite the same message into the Application.StatusBar property hundreds if not thousands of times while you are waiting on a page load.
Regarding a new Internet.Explorer 'window' not inheriting the .Visible = False attribute, I recommend you switch to .Navigate2 and ShellWindows.
Addendum: Don't destroy your ie object until you are finished with the html associated with ie.document.