Automate pressing "Buttons" on web pages - vba

Can someone help please, i can't seem to get the below to work
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As InternetExplorer
Dim objElement As Object
Dim objCollection As Object
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "https://Whatever" 'Not putting the URL up here
IE.Navigate URL
Do Until IE.ReadyState = 4: DoEvents: Loop
IE.Document.getElementbyid("menuoptionCell_Excel2007+").Click '(Fails here)
'Set Button = IE.Document.getElementbyid("menuoptionCell_Excel2007+")
'Button.Click
End Sub
i have also tried
IE.Document.getElementsbyClass("contextMenuOptionTextCell")(0).Click
It is saying i need an object. I have tried different methods but none seem to work. I will say i am using the word "Button" loosely as its not actually a button, but not sure what it is called. Below are the identities used on the web page, and i'm not totally sure which i need to press, but neither seem to work
<td class="contextMenuOptionTextCell">
<span class="contextMenuOptionText">Excel 2007+</span></td>
<td id="menuoptionCell_Excel2007+" style="vertical-align: top; width: 16px;"></td>
thank you
Mike

Related

VBA code to login and click a button/link within a <p> tag

I am VERY new to VBA and have been watching a ridiculous amount of youtube videos (specifically wise owl tutorials) and have searched here to try and find the answer to my question. I am writing a code to automate part of my job. I have the code to log in to a site that I can't share because I work with classified docs but I will share what I can. Here is the code so far...
Sub Login_and_click_link()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLDiv As MSHTML.IHTMLElement
Dim HTMLAs As MSHTML.IHTMLElementCollection
Dim HTMLA As MSHTML.IHTMLAnchorElement
'Open Internet Explorer and Navigate to PDDA
With IE
.Visible = True
.Navigate "http://.TheWebsiteINeed.com"
End With
'Loop until PDDA page has fully loaded
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
'Search DOM for element by ID and input username
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("userName")
HTMLInput.Value = Worksheets("sheet1").Range("letternumber").Text
'Search DOM for an element by ID and input password
Set HTMLInput = HTMLDoc.getElementById("Password")
HTMLInput.Value = Worksheets("sheet1").Range("letternumber").Text
'Search DOM for element by ID and click submit
Set HTMLInput = HTMLDoc.getElementById("submit")
HTMLInput.Click
'Navigate to the tab I need
??????
End Sub
There are two links on the page, each will take me to the same page that I need next. So whichever is easier to write code for you can use.
The link at the top is this(within a tr tag):
<span> class="topNavoff" style="cursor: hand;" onmouseover="window.status='Process
Management';this.className='topNavon';"
onmouseout="window.status='';this.className='topNavoff';"
onclick="setTask('pmMain');">Process</span>
The link at the bottom is this (within a p tag):
<span> class="contentSubtitleLink" id="process" style="cursor: hand;"
onmouseover="window.status='Process Management';" onmouseout="window.status='';"
onclick="setTask('pmMain');">Process</span>
Let me know if there is anything I can send to help make this more clear. ANY help would be GREATLY appreciated!

VBA Object doesn't support property or method when filling webform

I'm trying to use the Excel VBA to fill a web form and then scrape some data.
I am stuck on how to put in the value that I want to search for.
In the source code, there is no id or name that I can use.
I tried with a class name but it is not working.
" Source code:
<input type="text" style="height: 28px; width: 170px; padding-left: 5px;" ng-model="model.FieldValue" placeholder="Enter a Value" ng-show="model.searchOptions.length && model.searchOptions != 'SQE Quote Id'" class="ng-pristine ng-valid ng-touched">
Sub Macro1()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim html As HTMLDocument
Set IE = New InternetExplorerMedium
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "xxxx"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'
Set html = IE.Document
IE.Document.getElementById("searchOptions").Click (2)
IE.Document.getElementById("searchOptions").Value = "Access Qref"
IE.Document.getelemenetbyclassname("ng-pristine ng-valid ng-touched").Value = "00000"
'
End Sub
Try to put '.' between class names like below is working on my side.
IE.Document.querySelector("input.ng-pristine.ng-valid.ng-touched").Value = "00000"
Modified example:
Sub Macro1()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim html As HTMLDocument
Set IE = New InternetExplorerMedium
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "D:\Backup20190913\tests\353.html"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
Set html = IE.Document
IE.Document.querySelector("input.ng-pristine.ng-valid.ng-touched").Value = "00000"
End Sub
Output:
The error is as it says. The method you are trying does not exist for that node. You are fine using a multi-valued class (more than one class name for the element; denoted by several strings separated by spaces within the class attribute of the element) inside of the correct method getElementsByClassName, rather than getelemenetbyclassname then you need to index into the returned collection in order to exercise the .click method.
IE.Document.getElementsByClassName("ng-pristine ng-valid ng-touched")(0).Value = "00000" '0 for first node
However, as in other answer, if using querySelector/querySelectorAll to match by css class selector then a multi-valued class must have the spaces between replaced with ".". Better still, try to select as few of those class names inside the multi-value as possible and the most selective which still match on the required node(s)
And please, I write this a lot on SO, use a proper page load wait:
While IE.busy Or IE.readystate <> 4: DoEvents: Wend

Error when pressing IE button Object Variable or With block variable not set

I know there are other questions that are similar to mine, but I am quite new to this, and a lot of them don't make sense to me.
That being said, I have researched this error and it says I need to Declare an Object Variable, Set the Object and then use With at the correct points, I have as far as I am aware tried that, but still get the error
Object variable or With block variable not set
My Code is
Public Sub a()
Dim Button4 As Object
Dim ie As New InternetExplorer
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "https://reporting.med.siemens.com/sap(bD1lbiZjPTEwMSZkPW1pbg==)/bc/bsp/sie/med_cm/start.htm"
ie.Visible = True
Do While ie.ReadyState <> READYSTATE_COMPLETE Or ie.Busy: DoEvents: Loop
With ie.Document
Set Button4 = ie.Document.getElementById("seite-itm-2")
Button4.Click
End With
MsgBox "complete"
End Sub
What I am trying to do is press the below "Button" on an internet page,
<TD onclick="return htmlbSL(this,7,'seite:sel_tab_2','','3','sel_tab_2')" id=seite-itm-2 class=urTbsLabelOff onkeydown="if (sapUrMapi_checkKey(event,'keydown',new Array('32'))){return htmlbSL(this,7,'seite:sel_tab_2','','3','sel_tab_2')};sapUrMapi_TabStrip_keySelect('seite',2,4,event);" noWrap><SPAN id=seite-itm-2-txt title="Bookmarks " class=urTbsTxtOff>Bookmarks </SPAN></TD>
Can some one please help?
Thank You

Clicking link within a HTML List

I'm trying use Excel's IE automation tools to click a website link within a users profile of this site. Using the following in VBA:
ie.Document.getElementsByClassName("website")(0).getElementsByTagName("a")(0).Click
But keep getting the Runtime error '438' when ran. Any advice on how to fix this or if clicking this link is even possible please? Thanks.
EDIT (Full Code):
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "site"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set objA = ie.Document.getElementsByClassName("website")(0) 'GETTIN ERROR HERE
objA.getElementsByTagName("a")(0).Click
End Sub
It would appear that there were a few minor issues here.
One being that .Click doesn't always function properly, I would suggest using .getAttribute("href") combined with ie.Navigate()
Another being that you seem to define html then never use it again.
The following code works:
Sub test()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://beverlyhills.yourkwoffice.com/mcj/user/AssociateSearchSubmitAction.do?orgId=5058&lastName=&firstName=&rows=100"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = ie.Document
Set objA = html.getElementsByClassName("website")(0).getElementsByTagName("a")(0)
ie.Navigate (objA.getAttribute("href"))
End Sub

VBA Internet Explorer clicking on Text Error

I am new to VBA and it would be great if you can help me on resolving this issue.
I am trying to click on a Text on an IE SharePoint webpage. I am am able to navigate to IE browser, but I am getting a VBA error for clicking the text "Americas" highlighted in Yellow in attached Screenshot. I need help with the IE.Document part of the code at the end of VBA code below. I assume GetElementbyID and GetElementByTagName are correct from HTML code below.
Error - Method Document of Object "IEwebBrowser"Failed
VBA Code:
Private Sub UploadFile()
Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Dim buttonCollection As Object
Dim AllSpanElements
Dim Span
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
' Send the form data To URL As POST binary request
IE.navigate "URL"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
' I AM GETTING ERROR HERE
Set AllSpanElements = IE.Document.getElementById("ext-gen1271").getElementsByTagName("div")
AllSpanElements.Click
Set IE = Nothing
Set objElement = Nothing
End Sub
HTML CODE
<table class="x-grid-table x-grid-table-resizer" border="0" cellspacing="0" cellpadding="0" style="width:10000px;"><tbody>
<tr class="x-grid-row x-grid-row-selected x-grid-row-focused" id="ext-gen1271">
<td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1030 x-grid-cell-first">
<div class="x-grid-cell-inner " style="text-align: left; ;"><img src="data:image/gif;base64,R0lGODlhAQABAID/AMDA" class="x-tree-elbow-plus x-tree-expander">
<img src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAA" class="x-tree-icon x-tree-icon-parent ">
Americas</div>
</td>
</tr>
</tbody>
</table>
Give this a shot, I cleaned up the code slightly and I'm trying a slightly different approach. Basically I'm iterating over each element on the page, then clicking it when the InnerText has "Americas" contained with in it.
It may not be the InnerText Property you want to check, it might be the Value or Title so you will need to check that.
Here's the code:
Private Sub UploadFile()
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
Dim Elements As Object
Dim Element As Object
With IE
.Visible = True
' Send the form data To URL As POST binary request
.navigate "URL"
' Wait while IE loading...
While .Busy Or .readystate <> 4
Application.Wait (Now() + TimeValue("00:00:01"))
DoEvents
Wend
End With
' I AM GETTING ERROR HERE
Set Elements = IE.Document.getElementsByTagName("*") ' * is all elements
For Each Element In Elements
If InStr(1, Element.innerText, "Americas") > 0 Then ' If the element has the word Americas...click it
Element.Focus
Element.Click
Element.FireEvent ("OnClick")
End If
Next
'Clean up
Set IE = Nothing
End Sub