the method document of the iwebbrowser2 object failed - vba

When I launch the site via my code, there is an error of type "method document of object iwebbrowser2 failed " at the level of my variable "oDoc"
Private Function CreerNavigateur(ByVal mails As String)
Dim IE As Object
Dim oDoc As Object
Dim Htable, maTable As Object
Dim text As String
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.navigate "https://csrtool-ssl.sso.infra.ftgroup/csrtool_web/Bricks/pg/osuit/pages/identity/IdentityAccountAndUsers?type=emailAlias&value=" & mails & "&tab_main=AccountInfo"
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set oDoc = IE.Document
Set Htable = oDoc.getElementsByTagName("div")(1)
' MsgBox Htable.innerhtml
Set maTable = Htable.getElementsByTagName("span")
'MsgBox maTable(0).href
'myData = maTable(0).innertext
'MsgBox (myData)
IE.Quit
'On libère les variables
Set IE = Nothing
Set oDoc = Nothing
End Function
thank you for helping me to see my mistake

One of the possible reasons of such errors are security restricitions. Try to add infra.ftgroup to Trusted sites zone in IE settings:
Also try to open the website in compatibility view:
And play a bit with modes on Developer tools F12 - Emulation tab:

Related

Internet scraping issue when using vba on secure site

I am trying to automate IE to pull data from a secure site how every I keep getting the same error message: "Object required"
The debugger points to the line -HTMLinput.Value = "test"
Everything before works fine. I don't know what's the problem. I have verified my ID to make sure there is no mistakes.
Sub Brows()
Dim IE As New SHDocVw.InternetExplorerMedium
Dim Policy As Object
Dim certificate As Object
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLinput As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "secure website address"
Application.Wait Now + TimeValue("00:00:02")
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLinput = HTMLDoc.getElementById("input ID")
HTMLinput.Value = "test"
End Sub
How to log into a web site using Excel and VBA.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
The program requires references to the following:
1 Microsoft Internet Controls
2. Microsoft HTML Object Library
http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html

'Busy' method of 'IUWebBrowser2' object failed

When I launch the site via my code, there is an error of type "method document of object iwebbrowser2 failed " at the level of my variable "oDoc"
Private Function CreerNavigateur(ByVal mails As String)
Dim IE As Object
Dim oDoc As Object
Dim Htable, maTable As Object
Dim text As String
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.navigate "https://csrtool-ssl.sso.infra.ftgroup/csrtool_web/Bricks/pg/osuit/pages/identity/IdentityAccountAndUsers?type=emailAlias&value=" & mails & "&tab_main=AccountInfo"
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set oDoc = IE.Document
Set Htable = oDoc.getElementsByTagName("div")(1)
' MsgBox Htable.innerhtml
Set maTable = Htable.getElementsByTagName("span")
'MsgBox maTable(0).href
'myData = maTable(0).innertext
'MsgBox (myData)
IE.Quit
'On libère les variables
Set IE = Nothing
Set oDoc = Nothing
End Function
thank you for helping me to see my mistake
formalizing my answer try maximized ie window and updated code will be
Private Function CreerNavigateur(ByVal mails As String)
Dim ie As SHDocVw.InternetExplorer
Dim oDoc As Object
Dim Htable, maTable As Object
Dim text As String
Set ie = New SHDocVw.InternetExplorer
ie.Visible = False
ie.navigate "https://csrtool-ssl.sso.infra.ftgroup/csrtool_web/Bricks/pg/osuit/pages/identity/IdentityAccountAndUsers?type=emailAlias&value=" & mails & "&tab_main=AccountInfo"
While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set oDoc = ie.Document
Set Htable = oDoc.getElementsByTagName("div")(1)
'MsgBox Htable.innerhtml
Set maTable = Htable.getElementsByTagName("span")
'MsgBox maTable(0).href
'myData = maTable(0).innertext
'MsgBox (myData)
ie.Quit
'On libère les variables
Set ie = Nothing
Set oDoc = Nothing
End Function

Macro to open multiple links in new tabs

I want my macro to open each link stored in a spreadsheet in a separate IE tab. I am successful with opening the first link, but for some reason on the second iteration of the loop I get:
Automation error.The interface is unknown
error.
I suspect the macro somehow loses IE object reference after first iteration, but I am not sure why.
Range is set OK.
Here is the code:
Sub OpenCodingForms()
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim CodingFormLinks As Range
Dim IE as InternetExplorerMedium
Set wb1 = Workbooks("New shortcut.xlsm")
Set ws1 = wb1.Worksheets("Data")
Set CodingFormLinks = ws1.Range("A2", Range("A2").End(xlDown))
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
ws1.Activate
For Each link In CodingFormLinks.Cells
IE.Navigate link, CLng(2049)
Next link
End Sub
I ran into this issue before and ended up just writing a routine to get the instance. You will need to add a reference to shell controls and automation.
you may have to adjust this to look for the sURL var in the beginning of the actual URL if there is redirection.
Sub OpenCodingForms()
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim CodingFormLinks As Range
Dim IE As InternetExplorerMedium
Set wb1 = Workbooks("New shortcut.xlsm")
Set ws1 = wb1.Worksheets("Data")
Set CodingFormLinks = ws1.Range("A2", Range("A2").End(xlDown))
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
ws1.Activate
Dim sUrl As String
For Each link In CodingFormLinks.Cells
sUrl = link.Value
IE.navigate sUrl, CLng(2048)
Set IE = GetWebPage(sUrl)
Next link
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Desc: The Function gets the Internet Explorer window that has the current
' URL from the sURL Parameter. The Function Timesout after 30 seconds
'Input parameters:
'String sURL - The URL to look for
'Output parameters:
'InternetExplorer ie - the Internet Explorer window holding the webpage
'Result: returns the Internet Explorer window holding the webpage
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetWebPage(sUrl As String) As InternetExplorer
Dim winShell As Shell
Dim dt As Date
'set the timeout period
dt = DateAdd("s", 300, DateTime.Now)
Dim IE As InternetExplorer
'loop until we timeout
Do While dt > DateTime.Now
Set winShell = New Shell
'loop through the windows and check the internet explorer windows
For Each IE In winShell.Windows
'check for the url
If IE.LocationURL = sUrl Then
'set the window visible
IE.Visible = True
IE.Silent = True
'set the return value
Set GetWebPage = IE
Do While IE.Busy
DoEvents
Loop
Set winShell = Nothing
Exit Do
End If
Next IE
Set winShell = Nothing
DoEvents
Loop
End Function

Use VBA IE Automation to send message from pinkoi.com

Forgive my English. Using VBA Automation of Internet Explorer, I want to send message from pinkoi.com.
Now I can show the send message form,But I don't know how to input the subject and body and select file and send it.
the send form photo
Sub test()
Dim ie As Object 'SHDocVw.InternetExplorer
Set ie = CreateObject("InternetExplorer.Application")
Call login(ie) 'just for login
Call show_the_form(ie) 'My problem in here
End Sub
Sub login(ie)
ie.Visible = True
ie.Navigate "https://www.pinkoi.com/user/testpkko"
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
Dim anchorTags As Object 'MSHTML.IHTMLElementCollection
Set anchorTags = ie.document.GetElementsByTagName("A")
Dim oAnchorLoop As Object 'MSHTML.IHTMLAnchorElement
For Each oAnchorLoop In anchorTags
Dim anchorText As String
anchorText = oAnchorLoop.Text
If anchorText = ChrW(20659) & ChrW(36865) & ChrW(35338) & ChrW(24687) Then
Dim oAnchorLogon As Object 'MSHTML.IHTMLAnchorElement2
Set oAnchorLogon = oAnchorLoop
oAnchorLogon.Click
Exit For
End If
Next
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
'* so now logon form should be visible
On Error Resume Next
Dim oUserNameInput As Object 'MSHTML.IHTMLInputElement
Set oUserNameInput = ie.document.getElementById("n-login-id")
nowtime = Timer
Do While ie.Busy Or ie.readyState <> 4 Or oUserNameInput Is Nothing
DoEvents
If Timer - nowtime > 1 Then Exit Do 'Already logoin
Set oUserNameInput = ie.document.getElementById("n-login-id")
Loop
oUserNameInput.Value = "testpkko"
Dim oUserPassword As Object 'MSHTML.IHTMLInputElement
Set oUserPassword = ie.document.getElementById("n-login-password")
oUserPassword.Value = "abc123"
Dim oListElement As Object 'MSHTML.HTMLLIElement
Set oListElement = oUserNameInput.parentElement
Dim oUnorderedList As Object 'MSHTML.IHTMLUListElement
Set oUnorderedList = oListElement.parentElement
Dim oForm As Object 'MSHTML.IHTMLFormElement
Set oForm = oUnorderedList.parentElement
Dim oSubmitInputElememt As Object 'MSHTML.HTMLInputElement
Set oSubmitInputElememt = Nothing
Dim lFormChildrenLoop As Long
For lFormChildrenLoop = 1 To oForm.all.Length
If oForm.elements.Item(lFormChildrenLoop).Type = "submit" Then
Set oSubmitInputElememt = oForm.elements.Item(lFormChildrenLoop)
Exit For
End If
Next lFormChildrenLoop
If Not oSubmitInputElememt Is Nothing Then
'Stop '* get ready .....
oSubmitInputElememt.Click
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
End If
End Sub
Sub show_the_form(ie)
Application.Wait Now + TimeValue("00:00:03")
ie.Navigate "https://en.pinkoi.com/user/testpkko2" 'After login we can trip to other people website
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
With ie.document
On Error Resume Next
For Each E In .GetElementsByTagName("A")
a = E.Text
If CStr(a) = "Message" Then
E.Click 'After Click I want to send a message
'how can I input Subject and body and select file to upload?
'And I want to learn how to do this , Very thanks.
End If
Next
End With
End Sub
Sorry we downvoted you, try this. If we click on the anchor you specified but I needed to use Unicode character logic. The rest is just navigating the DOM. I have put default username and password of "hello" and "world".
Option Explicit
Private Sub test()
'use ie open https://www.pinkoi.com/
'Please use FB login then web
'* Might want to consider using the Tools References to give yourself Intellisense
'* Tools->References Microsoft Internet Controls
'* Tools->References Microsoft HTML Object Library
Dim IE As Object 'SHDocVw.InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.pinkoi.com/user/testpkko"
Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
Dim anchorTags As Object 'MSHTML.IHTMLElementCollection
Set anchorTags = IE.document.GetElementsByTagName("A")
Dim oAnchorLoop As Object 'MSHTML.IHTMLAnchorElement
For Each oAnchorLoop In anchorTags
Dim anchorText As String
anchorText = oAnchorLoop.Text
If anchorText = ChrW(20659) & ChrW(36865) & ChrW(35338) & ChrW(24687) Then
Dim oAnchorLogon As Object 'MSHTML.IHTMLAnchorElement2
Set oAnchorLogon = oAnchorLoop
oAnchorLogon.Click
Exit For
'oAnchorLoop.Click 'here can show the form,but i can put message and select file from it
End If
Next
Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
'* so now logon form should be visible
On Error Resume Next
Dim oUserNameInput As Object 'MSHTML.IHTMLInputElement
Set oUserNameInput = IE.document.getElementById("n-login-id")
Do While IE.Busy Or IE.readyState <> 4 Or oUserNameInput Is Nothing
DoEvents
Set oUserNameInput = IE.document.getElementById("n-login-id")
Loop
oUserNameInput.Value = "hello"
Dim oUserPassword As Object 'MSHTML.IHTMLInputElement
Set oUserPassword = IE.document.getElementById("n-login-password")
oUserPassword.Value = "world"
Dim oListElement As Object 'MSHTML.HTMLLIElement
Set oListElement = oUserNameInput.parentElement
Dim oUnorderedList As Object 'MSHTML.IHTMLUListElement
Set oUnorderedList = oListElement.parentElement
Dim oForm As Object 'MSHTML.IHTMLFormElement
Set oForm = oUnorderedList.parentElement
Dim oSubmitInputElememt As Object 'MSHTML.HTMLInputElement
Set oSubmitInputElememt = Nothing
Dim lFormChildrenLoop As Long
For lFormChildrenLoop = 1 To oForm.all.Length
If oForm.elements.Item(lFormChildrenLoop).Type = "submit" Then
Set oSubmitInputElememt = oForm.elements.Item(lFormChildrenLoop)
Exit For
End If
Next lFormChildrenLoop
If Not oSubmitInputElememt Is Nothing Then
Stop '* get ready .....
oSubmitInputElememt.Click
End If
End Sub
I alreay know how to input subject and body and send the file
ie.document.all("title").Value = "subject"
ie.document.all("description").Value = "Body"
For Each e In ie.document.GetElementsByTagName("INPUT")
a = e.Type
If CStr(a) = "submit" Then e.Click
Next
But after click select file , it will no response and program will stop
did anybody can use vba to select file?
ie.document.all("file").Click

Scraper has stopped working, probably a small issue but cannot find the issue

This scraper should return the data suggesting how many properties show up in the search.
It was working until I opened it this morning, the class hasn't changed but for some reason, it will not return any data into the cell stated.
Sub ZPLA2()
Const READYSTATE_COMPLETE = 4
Dim j As Integer
Dim ie As InternetExplorer
Dim Doc As IHTMLDocument
Dim xcolElements As IHTMLElementCollection
Dim ell As IHTMLElement
Dim pn As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://www.zoopla.co.uk/for-sale/property/london/w1/west-end-mayfair-soho-marylebone-south/?beds_max=0&beds_min=0&include_retirement_homes=true&include_shared_ownership=true&new_homes=include&price_max=200000&price_min=50000&q=w1&radius=20&results_sort=newest_listings&search_source=refine"
' Do
'DoEvents
'Loop Until Not ie.Busy And ie.readyState = READYSTATE_COMPLETE
Application.Wait (Now + TimeValue("0:00:07"))
Set Doc = ie.Document
Set xcolElements = Doc.getElementsByClassName("split3l result-count")
For Each ell In xcolElements
Sheet2.Range("d2").Value = ell.innerText
On Error GoTo skip
Next
skip:
ie.Quit
Set el = Nothing
Set xcolElements = Nothing
Set Doc = Nothing
Set ie = Nothing
End Sub
Replace
Set xcolElements = Doc.getElementsByClassName("split3l result-count")
with:
Set xcolElements = Doc.getElementsByClassName("listing-results-utils-count")
Remember to use IE Developer Tools not Chrome etc. as you are in fact utilizing the IE browser in VBA in the code above.