How to use click function? - excel

Everything works in my code except for the .click at the end.
It disables the button from clicking and it disables the operators ability to press 'enter' with the pasted text.
Only after I click in the text box and enter characters, can I hit enter (or click the button) to search.
Sub Part_Information()
'
' Part_Information Macro
'
' Keyboard Shortcut: Ctrl+a
'
ActiveCell.Select
Selection.Copy
Dim IE As Object
Dim MyURL As String
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
'''''''''''''''''''''''''''''''
'Switching to correct page
'If it can't be found, ends the sub
'If it is found, then switches to correct search bar and searches for information
'''''''''''''''''''''''''''''''
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
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
If my_title = "Parts Intelligence" Then
Set IE = objShell.Windows(x)
marker = 1
Exit For
End If
Next
If marker = 0 Then
MsgBox ("A matching webpage was NOT found")
Else
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
''''Change name (case sensitive)
If objCollection(i).class = "simple-search-text form-control short ng-valid ng-dirty ng-touched" Then
' Set text for search
objCollection(i).Value = ActiveCell.Value
Else
''''Change Type & Name (case sensitive)
If objCollection(i).class = "btn btn-icon" Then
objCollection(i).Click
' "Search" button is found
End If
End If
i = i + 1
Wend
End If
End Sub
This is from the webpage:
Search Button & Search Text Box:

Okay, so first things first. Do not create a new IE object if you are trying to find one that already exists. This will eventually start to bog down your computer having a hundred hidden Internet Explorers open in the background.
So, get rid of this
Set IE = CreateObject("InternetExplorer.Application")
'''''''''''''''''''''''''''''''
'Switching to correct page
'If it can't be found, ends the sub
'If it is found, then switches to correct search bar and searches for information
'''''''''''''''''''''''''''''''
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
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
If my_title = "Parts Intelligence" Then
Set IE = objShell.Windows(x)
marker = 1
Exit For
End If
Next
If marker = 0 Then
MsgBox ("A matching webpage was NOT found")
Else
and instead use something like this function - which will return the IE object that matches a URL and Title.
Dim IE As Object
Dim MyURL As String
Dim objElement As Object
Dim objCollection As Object
Set IE = getIE("https://myurl.com", "Parts Intelligence")
If IE Is Nothing Then
Rem: Add what happens if browser isn't found
End If
Function GetIE(sLocation As String, sDocTitle As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String, sTitle As String
Dim RetVal As Object
Set RetVal = Nothing
Set objShell = CreateObject("shell.application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.document.Location
sTitle = o.document.Title
On Error GoTo 0
If sURL Like "*" sLocation & "*" And sTitle Like sDocTitle & "*" Then
Set RetVal = o
Exit For
End If
Next o
Set GetIE = RetVal
End Function
Now, as for your issue. It's difficult to say exactly what is causing this problem without having access to the website in question. However, I had a very similar experience before and what allowed me to get past it was activating the textbox by code.
So, for your textbox, try using:
yourTextBoxObject.setActive
Then filling in the box. This should (hopefully) solve your problem with the button being disabled. With your code, it should look something like:
While i < objCollection.Length
''''Change name (case sensitive)
If objCollection(i).class = "simple-search-text form-control short ng-valid ng-dirty ng-touched" Then
' Set text for search
objCollection(i).setActive
objCollection(i).Value = ActiveCell.Value
Else
''''Change Type & Name (case sensitive)
If objCollection(i).class = "btn btn-icon" Then
objCollection(i).Click
' "Search" button is found
End If
End If
i = i + 1
Wend
Your full code:
Sub Part_Information()
'
' Part_Information Macro
'
' Keyboard Shortcut: Ctrl+a
'
ActiveCell.Select
Selection.Copy
Dim IE As Object
Dim MyURL As String
Dim objElement As Object
Dim objCollection As Object
Dim IE As Object
Dim MyURL As String
Dim objElement As Object
Dim objCollection As Object
Set IE = getIE("https://myurl.com", "Parts Intelligence")
If IE Is Nothing Then
Rem: Add what happens if browser isn't found
End If
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
''''Change name (case sensitive)
If objCollection(i).class = "simple-search-text form-control short ng-valid ng-dirty ng-touched" Then
' Set text for search
objCollection(i).Value = ActiveCell.Value
Else
''''Change Type & Name (case sensitive)
If objCollection(i).class = "btn btn-icon" Then
objCollection(i).Click
' "Search" button is found
End If
End If
i = i + 1
Wend
End Sub
Function GetIE(sLocation As String, sDocTitle As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String, sTitle As String
Dim RetVal As Object
Set RetVal = Nothing
Set objShell = CreateObject("shell.application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.document.Location
sTitle = o.document.Title
On Error GoTo 0
If sURL Like "*" sLocation & "*" And sTitle Like sDocTitle & "*" Then
Set RetVal = o
Exit For
End If
Next o
Set GetIE = RetVal
End Function

Related

Copy Data from Web page and paste into Excel

I have been trying to scrape the web data using EXCEL VBA. Below code paste the date from excel to wen then initiated the submit button to go to the result page. which looks like this:
I want to copy and paste the first and the second line into excel like this:
if any individual gets first dose then put details of first dose, and return empty for the second dose if its empty. If both dose are not available then return empty for both.
I am not able to develop this last thing and struggling since couple of hours to achieve this (copy data and paste into excel)
it would required a ID number and date to submit for the result that i can provide in comments. I have been using following code to accomplish this your help will be much appreciated.
Option Explicit
Sub Newfunction()
Const Url As String = ""
Dim LogData As Worksheet
Set LogData = ThisWorkbook.Worksheets("Sheet1")
Dim IdNumber As String
Dim openDate As Date
IdNumber = LogData.Cells(3, "A").Value
openDate = LogData.Cells(3, "B").Value
Set LogData = Nothing
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Navigate Url
Do While .Busy Or .ReadyState <> 4
DoEvents
Loop
.Visible = True
Dim ieDoc As Object
Set ieDoc = .Document
End With
'Enter the CNIC
Dim IDdata As Object
Set IDdata = ieDoc.getElementById("checkEligibilityForm:cnic")
If Not IDdata Is Nothing Then IDdata.Value = IdNumber
Set IDdata = Nothing
'Enter Date
Dim puttdate As Object
Set puttdate = ieDoc.getElementById("checkEligibilityForm:issueDate_input")
If Not puttdate Is Nothing Then puttdate.Value = Format(openDate, "dd-mm-yyyy")
Set puttdate = Nothing
'Answering the captcha question
'Split the innerText to string array to determine the equation
Dim captchaQns As Object
Set captchaQns = ieDoc.getElementsByClassName("submit__generated")(0)
If Not captchaQns Is Nothing Then
Dim mathEq() As String
mathEq = Split(captchaQns.innerText, " ")
Set captchaQns = Nothing
'mathEq(0) = first number
'mathEq(1) = math operator
'mathEq(2) = second number
If IsNumeric(mathEq(0)) Then
Dim firstNum As Long
firstNum = CLng(mathEq(0))
If IsNumeric(mathEq(2)) Then
Dim secondNum As Long
secondNum = CLng(mathEq(2))
'Select Case statement used here in case you encounter other form of math question (e.g. - X /), expand cases to cater for other scenario
Dim mathAnswer As Long
Select Case mathEq(1)
Case "+": mathAnswer = firstNum + secondNum
End Select
End If
End If
If mathAnswer <> 0 Then
'Enter the answer to the box
Dim captchaAns As Object
Set captchaAns = ieDoc.getElementsByClassName("submit__input")(0)
If Not captchaAns Is Nothing Then captchaAns.Value = mathAnswer
Set captchaAns = Nothing
'Get the submit button element, remove "disabled" attribute to allow clicking
Dim submitBtn As Object
Set submitBtn = ieDoc.getElementsByName("checkEligibilityForm:j_idt79")(0)
submitBtn.removeAttribute "disabled"
submitBtn.Click
Set submitBtn = Nothing
End If
End If
Dim tbls, tbl, trs, tr, tds, td, r, c
Set tbl = ie.Document.getElementsByTagName("table")(0)
Set trs = tbl.getElementsByTagName("tr")
For r = 0 To trs.Length - 1
Set tds = trs(r).getElementsByTagName("tr")
'if no <td> then look for <th>
If tds.Length = 0 Then Set tds = trs(r).getElementsByTagName("td")
For c = 0 To tds.Length - 1
ActiveSheet.Range("C4").Offset(r, c).Value = tds(c).innerText
Next c
Next r
End Sub
Try this:
It will enter the data into Range("C4:F4") for first dose and Range("G4:J4") for second dose.
Sub Newfunction()
Const Url As String = "https://nims.nadra.gov.pk/nims/certificate"
Dim LogData As Worksheet
Set LogData = ThisWorkbook.Worksheets("Sheet1")
Dim lastRow As Long
lastRow = LogData.Range("A" & Rows.Count).End(xlUp).Row
Dim currentRow As Long
For currentRow = 3 to lastRow
Dim IdNumber As String
Dim openDate As Date
IdNumber = LogData.Cells(currentRow, 1).Value
openDate = LogData.Cells(currentRow, 2).Value
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate Url
Do While .Busy Or .readyState <> 4
DoEvents
Loop
.Visible = True
Dim ieDoc As Object
Set ieDoc = .document
End With
'Enter the CNIC
Dim IDdata As Object
Set IDdata = ieDoc.getElementById("checkEligibilityForm:cnic")
If Not IDdata Is Nothing Then IDdata.Value = IdNumber
Set IDdata = Nothing
'Enter Date
Dim puttdate As Object
Set puttdate = ieDoc.getElementById("checkEligibilityForm:issueDate_input")
If Not puttdate Is Nothing Then puttdate.Value = Format(openDate, "dd-mm-yyyy")
Set puttdate = Nothing
'Answering the captcha question
'Split the innerText to string array to determine the equation
Dim captchaQns As Object
Set captchaQns = ieDoc.getElementsByClassName("submit__generated")(0)
If Not captchaQns Is Nothing Then
Dim mathEq() As String
mathEq = Split(captchaQns.innerText, " ")
Set captchaQns = Nothing
'mathEq(0) = first number
'mathEq(1) = math operator
'mathEq(2) = second number
If IsNumeric(mathEq(0)) Then
Dim firstNum As Long
firstNum = CLng(mathEq(0))
If IsNumeric(mathEq(2)) Then
Dim secondNum As Long
secondNum = CLng(mathEq(2))
'Select Case statement used here in case you encounter other form of math question (e.g. - X /), expand cases to cater for other scenario
Dim mathAnswer As Long
Select Case mathEq(1)
Case "+": mathAnswer = firstNum + secondNum
End Select
End If
End If
Erase mathEq
If mathAnswer <> 0 Then
'Enter the answer to the box
Dim captchaAns As Object
Set captchaAns = ieDoc.getElementsByClassName("submit__input")(0)
If Not captchaAns Is Nothing Then captchaAns.Value = mathAnswer
Set captchaAns = Nothing
'Get the submit button element, remove "disabled" attribute to allow clicking
Dim submitBtn As Object
Set submitBtn = ieDoc.getElementsByName("checkEligibilityForm:j_idt79")(0)
submitBtn.removeAttribute "disabled"
submitBtn.Click
Set submitBtn = Nothing
End If
End If
With ie
Do While .Busy Or .readyState <> 4
DoEvents
Loop
Set ieDoc = .document
End With
Dim resultTbl As Object
Set resultTbl = ieDoc.getElementsByTagName("table")
If resultTbl.Length <> 0 Then
Dim resultRows As Object
Set resultRows = resultTbl(0).getElementsByTagName("tr")
If resultRows.Length > 1 Then
'Get the 2nd row (1st row is header so ignore)
Dim firstDose As Object
Set firstDose = resultRows(1).getElementsByTagName("td")
LogData.Cells(currentRow, 3).Value = firstDose(0).innerText
LogData.Cells(currentRow, 4).Value = firstDose(1).innerText
LogData.Cells(currentRow, 5).Value = firstDose(2).innerText
LogData.Cells(currentRow, 6).Value = firstDose(3).innerText
Set firstDose = Nothing
'If there are totals of 3 TR elements then there are 2nd dose
If resultRows.Length = 3 Then
Dim secondDose As Object
Set secondDose = resultRows(2).getElementsByTagName("td")
LogData.Cells(currentRow, 7).Value = secondDose(0).innerText
LogData.Cells(currentRow, 8).Value = secondDose(1).innerText
LogData.Cells(currentRow, 9).Value = secondDose(2).innerText
LogData.Cells(currentRow, 10).Value = secondDose(3).innerText
Set secondDose = Nothing
End If
'Else
'Do something here if there is only a header row i.e. no dose (assumption)
End If
Set resultRows = Nothing
End If
Set resultTbl = Nothing
Set ieDoc = Nothing
ie.Quit 'Remove if you don't want to close IE
Set ie = Nothing 'Remove if you don't want to close IE
Next currentRow
Set LogData = Nothing
End Sub

VBA to copy website data by putting details one by one

I have this website and i have been trying to create an function which collects ID Number from Col"A" and its Date of initiation in Col"B".
then adds the Sum of two boxes into 3rd one like below image.
after that it will go for the result it will be like
If the individual gets first dose then insert in Col"C" "1st Dose Done" If the individual gets second dose then insert in Col"D" "second dose done" if the individual has not taken both or single dose result will be empty.
then go for next until Col"A" used range. I have tried to create function but could not, Your help will be appreciated in this regards.
Option Explicit
Sub Newfunction()
Const Url$ = ""
Dim IdNumber As String, openDate As Date, LogData As Worksheet
Set LogData = ThisWorkbook.Worksheets("Sheet1")
IdNumber = LogData.Cells(2, "A").Value
openDate = LogData.Cells(2, "B").Value
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate Url
ieBusy ie
.Visible = True
Dim IDdata As Object, puttdate As Object, submitbut As String
Set IDdata = .document.getElementsByName("checkEligibilityForm:cnic")(0)
Set puttdate = .document.getElementsByName("checkEligibilityForm:issueDate_input")(0)
Set submitbut = .document.getElementsByClassName("submit__generated")(0).innerText
IDdata.Value = IdNumber
puttdate.Value = Format(openDate, "dd/mm/yyyy")
submitbut.Value = .document.getElementsByClassName("submit__input")(0)
Debug.Print .document.getElementsByClassName("submit__input")(0)
End With
End Sub
Sub ieBusy(ie As Object)
Do While ie.Busy Or ie.readyState < 4
DoEvents
Loop
End Sub
Try this code below - This should do the following:
Enter the CNIC
Enter the date
Answer the Captcha question
Click the button and the page should load.
As I can't proceed to the result page, I can't guarantee that it will produce the result page so please test it out with a proper data to see if it works. I have also tried to explain what each block of codes is doing in the comment.
Option Explicit
Sub Newfunction()
Const Url As String = "https://nims.nadra.gov.pk/nims/certificate"
Dim LogData As Worksheet
Set LogData = ThisWorkbook.Worksheets("Sheet1")
Dim IdNumber As String
Dim openDate As Date
IdNumber = LogData.Cells(2, "A").Value
openDate = LogData.Cells(2, "B").Value
Set LogData = Nothing
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate Url
Do While .Busy Or .readyState <> 4
DoEvents
Loop
.Visible = True
Dim ieDoc As Object
Set ieDoc = .Document
End With
'Enter the CNIC
Dim IDdata As Object
Set IDdata = ieDoc.getElementById("checkEligibilityForm:cnic")
If Not IDdata Is Nothing Then IDdata.Value = IdNumber
Set IDdata = Nothing
'Enter Date
Dim puttdate As Object
Set puttdate = ieDoc.getElementById("checkEligibilityForm:issueDate_input")
If Not puttdate Is Nothing Then puttdate.Value = Format(openDate, "dd-mm-yyyy")
Set puttdate = Nothing
'Answering the captcha question
'Split the innerText to string array to determine the equation
Dim captchaQns As Object
Set captchaQns = ieDoc.getElementsByClassName("submit__generated")(0)
If Not captchaQns Is Nothing Then
Dim mathEq() As String
mathEq = Split(captchaQns.innerText, " ")
Set captchaQns = Nothing
'mathEq(0) = first number
'mathEq(1) = math operator
'mathEq(2) = second number
If IsNumeric(mathEq(0)) Then
Dim firstNum As Long
firstNum = CLng(mathEq(0))
If IsNumeric(mathEq(2)) Then
Dim secondNum As Long
secondNum = CLng(mathEq(2))
'Select Case statement used here in case you encounter other form of math question (e.g. - X /), expand cases to cater for other scenario
Dim mathAnswer As Long
Select Case mathEq(1)
Case "+": mathAnswer = firstNum + secondNum
End Select
End If
End If
If mathAnswer <> 0 Then
'Enter the answer to the box
Dim captchaAns As Object
Set captchaAns = ieDoc.getElementsByClassName("submit__input")(0)
If Not captchaAns Is Nothing Then captchaAns.Value = mathAnswer
Set captchaAns = Nothing
'Get the submit button element, remove "disabled" attribute to allow clicking
Dim submitBtn As Object
Set submitBtn = ieDoc.getElementsByName("checkEligibilityForm:j_idt79")(0)
submitBtn.removeAttribute "disabled"
submitBtn.Click
Set submitBtn = Nothing
End If
End If
End Sub

How to upload values to a website and select a button using Excel VBA

I am trying to automate the sending of SMSes from a company website but I do not know how to upload the message, the cellphone number and select the button to send the message.
Sub smssend()
Dim appIE As Object
Dim e As Object
Dim wb As Workbook
Dim ws As Worksheet
Dim cellno As String
Dim mess As String
Dim strPattern As String: strPattern = "^((?:\+27|0[0-9]{9}"
Dim regEx As New RegExp
Dim linecount As Long
linecount = 2
Set wb = Application.Workbooks("SMSmacro")
Set ws = wb.Worksheets("Sheet1")
Set appIE = CreateObject("internetexplorer.application")
With appIE
.navigate "http://cadde.abgza.co.za/SMS/CreateSMS/CreateSms"
Do While appIE.busy
DoEvents
Application.Wait (Now + TimeValue("0:00:03"))
Loop
.Visible = True
End With
Do While appIE.busy
DoEvents
Application.Wait (Now + TimeValue("0:00:03"))
Loop
Do While ws.Cells(linecount, 1) <> ""
cellno = ws.Cells(linecount, 1)
mess = ws.Cells(linecount, 2)
a = Len(mess)
If Len(mess) > 160 Then
ws.Cells(linecount, 4).Value = "Message Too Long"
GoTo nxt
End If
With regEx
If regEx.Test(cellno) Then
With appIE
Set e = appIE.document.getElementById("cellNumber")
e = cellno
End With
Else
ws.Cells(linecount, 3).Value = "Incorrect Cell Number"
End If
End With
nxt:
linecount = linecount + 1
Loop
End Sub
I've rewritten your code. Never use GoTo, except for error handling.
The three variables nodeCellNo, nodeMess and nodeSubBut are not mandatory. I have introduced them to make the code more comprehensible for you. Without variables you can access a node directly this way:
appIE.document.getElementById("cellNumber").Value = cellNo
I can't tell from your HTML code whether the change event of the textarea tag needs to be triggered to recognize the text of the message.
Please read the comments in the code carefully:
Sub SendSMS()
'If you have constant values use constants in your code
Const url As String = "http://cadde.abgza.co.za/SMS/CreateSMS/CreateSms"
Const strPattern As String = "^((?:\+27|0[0-9]{9}"
'If you use late binding, no Excel reference is required
'Advantage: The code runs immediately on every computer
'Disadvantage: No IntelliSense is available during programming
Dim appIE As Object
Dim nodeCellNo As Object
Dim nodeMess As Object
Dim nodeSubBut As Object
Dim wb As Workbook
Dim ws As Worksheet
Dim cellNo As String
Dim mess As String
Dim regEx As Object 'Changed to late binding
Dim currentRow As Long
'Initialize variables
currentRow = 2
Set wb = Application.Workbooks("SMSmacro.xlsm") 'Full name needed
Set ws = wb.Worksheets("Sheet1")
Set regEx = CreateObject("VBScript.RegExp")
Set appIE = CreateObject("internetexplorer.application")
'Use the following line if you are in an intranet and
'the IE lost connection to the remote server
'Set appIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
'Load page to IE
appIE.Visible = True
appIE.navigate url
Do While appIE.busy: DoEvents: Loop
'The following line is only needed if the web page loads dynamic
'content after IE has reported that it is no longer busy
'The length of the break can be adjusted to your needs
Application.Wait (Now + TimeValue("0:00:03"))
'If the page looks like the SMS sending page after sending,
'the loop can start here. If another page is loaded, one
'could navigate again within the loop to the SMS URL. For
'many SMS I would set the loop further up and restart IE
'for each SMS. Unfortunately, if you navigate a lot in one
'instance of IE, it becomes unstable.
Do While ws.Cells(currentRow, 1) <> ""
cellNo = ws.Cells(currentRow, 1).Value
mess = ws.Cells(currentRow, 2).Value
If Len(mess) > 160 Then
ws.Cells(currentRow, 4).Value = "Message Too Long"
Else
If regEx.Test(cellNo) Then
'Insert cellphone number to html form
Set nodeCellNo = appIE.document.getElementById("cellNumber")
nodeCellNo.Value = cellNo
'Insert message to html form
'The textarea tag has no value attribute
'You can set the text by innertext to set it
'between the opening and the closing tectarea tag
Set nodeMess = appIE.document.getElementById("typedMessage")
nodeMess.innertext = mess
'Click submit button
Set nodeSubBut = appIE.document.getElementById("btnSend")
nodeSubBut.Click
'Wait to send the SMS
Application.Wait (Now + TimeValue("0:00:03"))
Else
ws.Cells(currentRow, 3).Value = "Incorrect Cell Number"
End If
End If
'Next SMS
currentRow = currentRow + 1
Loop
End Sub

Importing/scraping an website into excel

I am trying to scrape some data from a database, and I have it pretty much set. I look in IE for a tab that has me logged in into the database, and paste the query link there through vba. But how do I extract the data that it returns from the IE tab and put that into an excel cell or array.
This is the code I have for opening my query:
Sub import()
Dim row As Integer
Dim strTargetFile As String
Dim wb As Workbook
Dim test As String
Dim ie As Object
Call Fill_Array_Cultivar
For row = 3 To 4
Sheets.Add.Name = Cultivar_Array(row, 1)
strTargetFile = "https://www3.wipo.int/pluto/user/jsp/select.jsp?fl=app_date%2Cden_info%2Cden_final&hl=false&json.nl=map&wt=json&type=upov&start=0&qi=3-nNCXQ6etEVv184O9nnd5yg%3D%3D&q=cc%3AIT%20AND%20latin_name%3A(zea%20mays)%20AND%20den_info%3A" & Trim(Cultivar_Array(row, 1)) & "&facet=false"
Set ie = GetIE("https://www3.wipo.int" & "*")
If Not ie Is Nothing Then
ie.navigate (strTargetFile)
Else
MsgBox "IE not found!"
End If
Next row
End Sub
And this is the appropriate function:
'Find an IE window with a matching (partial) URL
'Assumes no frames.
Function GetIE(sAddress As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim retVal As Object, sURL As String
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
'see if IE is already open
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.document.Location
On Error GoTo 0
If sURL <> "" Then
If sURL Like sAddress & "*" Then
Set retVal = o
Exit For
End If
End If
Next o
Set GetIE = retVal
End Function
What the website returns to me is a white page with a line of text. Here is an example:
{"response":{"start":0,"docs":[{"den_final":"Abacus","app_date":"1998-01-13T22:59:59Z"}],"numFound":1},"qi":"3-nNCXQ6etEVv184O9nnd5yg==","sv":"bswa2.wipo.int","lastUpdated":1436333633993}
PS. I also tried using the importxml function, it will import the website, but only an error page, as it does not recognize me as logged in.
I found the solution, which was fairly simple but hard to find.
I can just grab the ie.Document.body.innertext which is all the text I need.
See the code I updated below:
Sub import()
Dim row As Integer
Dim strTargetFile As String
Dim wb As Workbook
Dim test As String
Dim ie As Object
Dim pageText As String
Call Fill_Array_Cultivar
For row = 3 To 4
Sheets.Add.Name = Cultivar_Array(row, 1)
strTargetFile = "https://www3.wipo.int/pluto/user/jsp/select.jsp?fl=app_date%2Cden_info%2Cden_final&hl=false&json.nl=map&wt=json&type=upov&start=0&qi=3-nNCXQ6etEVv184O9nnd5yg%3D%3D&q=cc%3AIT%20AND%20latin_name%3A(zea%20mays)%20AND%20den_info%3A" & Trim(Cultivar_Array(row, 1)) & "&facet=false"
Set ie = GetIE("https://www3.wipo.int" & "*")
If Not ie Is Nothing Then
ie.navigate (strTargetFile)
Do Until ie.ReadyState = 4: DoEvents: Loop
pageText = ie.Document.body.innertext
ActiveSheet.Cells(1, 1) = pageText
pageText = Empty
Else
MsgBox "IE not found!"
End If
Next row
End Sub

Pulling Data from an embedded web page with VBA Excel

I am trying to use VBA in Excel to access data in a webpage that is embedded in a webpage. I know how to do this if the table is on a non-embedded page. I also know how to navigate to this product's page using VBA. I cannot just navigate to the embedded page because there is a product id look up that converts a part number to an id and i don't have access to that database.
Here is the link to the page: http://support.automation.siemens.com/WW/view/en/7224052
I would have posed a picture of the element for clarity but I don't have 10 rep points...
The table I need to get information from is the "Product Life Cycle" table.
I can see the correct url in a property called src under the corresponding item if I save the page as an HTMLDocument in VBA using the following code:
For Each cell In Selection
link = "http://support.automation.siemens.com/US/llisapi.dll?func=cslib.csinfo&lang=en&objid=" & cell & "&caller=view"
ie.navigate link
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Is there a way to index this table with VBA or will I have to contact the company and attempt to get access to the product ID so I can navigate to the page directly?
Regarding my comment below, here is the code that a recorded macro yeilds:
ActiveCell.FormulaR1C1 = _
"http://support.automation.siemens.com/WW/llisapi.dll?func=cslib.csinfo&lang=en&objid=6ES7194-1AA01-0XA0&caller=view"
Range("F9").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://support.automation.siemens.com/WW/llisapi.dll?func=ll&objid=7224052&nodeid0=10997566&caller=view&lang=en&siteid=cseus&aktprim=0&objaction=csopen&extranet=standard&viewreg=WW" _
, Destination:=Range("$F$9"))
.FieldNames = True
.RowNumbers = False
I know where to find the string: URL;http://support.automation.siemens.com/WW/llisapi.dll?func=ll&objid=7224052&nodeid0=10997566&caller=view&lang=en&siteid=cseus&aktprim=0&objaction=csopen&extranet=standard&viewreg=WW, but I don't know how to save it to a variable.
Not sure I exactly understand your question, but here is some code that will get the source code behind the table of interest. You can extract the data of interest using functions like "instr" and "mid"
' open IE, navigate to the website of interest and loop until fully loaded
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate "http://support.automation.siemens.com/WW/view/en/7224052"
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not ie.Busy And ie.ReadyState = 4
DoEvents
Loop
End With
' Assign the source code behind the page to a variable
my_var = ie.document.frames(3).document.DocumentElement.innerhtml
' Extract the url for the "Product life cycle" table
pos_1 = InStr(1, my_var, "product life cycle", vbTextCompare)
pos_2 = InStr(pos_1, my_var, "/WW/llisapi", vbTextCompare)
pos_3 = InStr(pos_2, my_var, """><", vbTextCompare)
pos_4 = InStr(pos_3, my_var, """/>", vbTextCompare)
table_url = Mid(my_var, pos_2, pos_3 - pos_2)
table_url = Replace(table_url, "amp;", "", 1, -1, vbTextCompare)
table_url = "http://support.automation.siemens.com" & table_url
' navigate to the table url
ie.navigate table_url
Do Until Not ie.Busy And ie.ReadyState = 4
DoEvents
Loop
' assign the source code for this page to a variable and extract the desired information
my_var2 = ie.document.body.innerhtml
pos_1 = InStr(1, my_var2, "ET 200X, basic modules,", vbTextCompare)
' close ie
ie.Quit
I have had problems getting ron's code to work, I think becuase IE doesn't work easily with frames. Below is some code that will extract some of the data from the table you have mentioned, it so far doesn't handle the diagrams.
Sub FrameStrip()
Dim oFrames As Object
Dim tdelements As Object
Dim tdElement As Object
Dim oFrame As MSHTML.HTMLFrameElement
Dim oElement As Object
Dim sString As String
Dim myVar As Variant
Dim sLinks() As String
Dim i As Integer
Dim bfound As Boolean
Dim url As String
Dim oIE As InternetExplorer
Set oIE = New InternetExplorer
url = "http://support.automation.siemens.com/WW/view/en/7224052"
'Set address for use with relative source names
myVar = Split(url, "/")
sString = myVar(0) & "//" & myVar(2)
oIE.navigate url
oIE.Visible = True
Do Until (oIE.readyState = 4 And Not oIE.Busy)
DoEvents
Loop
Set oFrames = oIE.document.getElementsByTagName("frame")
ReDim sLinks(oFrames.Length)
'Get the source locations for each frame
i = 0
For Each oFrame In oFrames
sLinks(i) = sString & (oFrame.getAttribute("src"))
i = i + 1
Next oFrame
'Go through each frame to find the table
i = 0
bfound = False
Do While i < UBound(sLinks) And bfound = False
oIE.navigate sLinks(i)
Do Until (oIE.readyState = 4 And Not oIE.Busy)
DoEvents
Loop
Set oElement = oIE.document.getElementById("produktangaben")
bfound = IsSet(oElement)
i = i + 1
Loop
Set tdelements = oElement.getElementsByTagName("td")
'Display information about table
sString = ""
For Each tdElement In tdelements
Debug.Print tdElement.innerText
sString = sString & tdElement.innerText
Next tdElement
End Sub
Function IsSet(ByRef oElement As Object) As Boolean
Dim tdelements As Object
Dim bSet As Boolean
bSet = True
On Error GoTo ErrorSet
Set tdelements = oElement.getElementsByTagName("td")
On Error GoTo 0
Cleanup:
On Error Resume Next
Set tdelements = Nothing
On Error GoTo 0
IsSet = bSet
Exit Function
ErrorSet:
bSet = False
GoTo Cleanup:
End Function

Resources