MSC .getElementById or .getElementByClassName - excel

I'm currently having a bit of a problem, because i can't create a proper VBA code for my excel to enter data from cell to particular website. Could You please help me with it ?
Sub MSC()
Dim OrgBox As HTMLInputElement
Set objIE = New SHDocVw.InternetExplorer
objIE.navigate "http://www.mscgva.ch/tracking/index.html"
objIE.Visible = True
Do While objIE.readyState < 4: Loop
Set OrgBox = objIE.getElementById("InputBox")
OrgBox.Value = Range("a1")
OrgBox.form.submit
End Sub

The element you're trying to control is in an iframe. To control elements inside an iframe, you need to extract the src attribute from the tag and navigate to that URL
ifr_url = ie.document.getElementsByTagName("iframe")(0).src
in your case ifr_url = "http://tracking.mscgva.ch/msctracking.php"
Once on that webpage your code to insert text should work.

Related

get background color from website

Hi im trying to get the backgound color from a website to compare it for other operations. Just like the Background color of the buy button of the web site.
Sub Colorfinder()
Dim oIE As New InternetExplorer
Dim oHtml As HTMLDocument
Dim tags As Object
Dim HTMLtags As IHTMLElementCollection
Dim HTMLtag As IHTMLElement
With oIE
.Visible = True
.navigate "https://www.gdax.com/trade/LTC-EUR"
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set oHtml = .document
End With
'this is needed to wait until the page is totally loaded
Do: Set tags = oHtml.getElementsByClassName("OrderBookPanel_text_3fH-g")(0): DoEvents: Loop While tags Is Nothing
'getting the element that it is about
Do: Set HTMLtags = oHtml.getElementsByClassName("OrderForm_toggle_31S34"): DoEvents: Loop While HTMLtags.Length = 0
Set HTMLtag = HTMLtags(0).Children(0)
Debug.Print HTMLtag.innerText
'this is the problem
Debug.Print HTMLtag.Style.backgroundColor
End Sub
i got this idea from an otherstuck overflow user. but it didnĀ“t work. link stuckoverflow
Try the below code:
Sub Colorfinder()
With CreateObject("InternetExplorer.Application")
.Visible = True
.navigate "https://www.gdax.com/trade/LTC-EUR"
Do While .Busy Or .readyState < 4: DoEvents: Loop
With .Document
Do While .getElementsByClassName("OrderBookPanel_text_3fH-g").Length = 0: DoEvents: Loop
Do While .getElementsByClassName("OrderForm_toggle_31S34").Length = 0: DoEvents: Loop
With .parentWindow
.execScript "var e = document.getElementsByClassName('OrderForm_toggle_31S34')[0].children[0];"
.execScript "e.style.backgroundColor = window.getComputedStyle(e,null).getPropertyValue('background-color');"
End With
Debug.Print .getElementsByClassName("OrderForm_toggle_31S34")(0).Children(0).Style.backgroundColor ' rgb(77, 165, 60)
End With
End With
End Sub
.execScript executes JScript code within HTML document, that is why syntax uses var, ', [] and ;, etc., variable e is the target node declared in the document scope, and it's actual computed background value just put into .backgroundColor property. Using .execScript is the only way I found to get to window.getComputedStyle method, which actually do the job.
Is this really something that needs to be automated?
The background-color of the BUY button is:
(Click for more about #4da53c Strong Sap Green.)
Right-click any element on a web page and choose Inpsect Element to find the color under the style properties (specific location depends on your browser).
Or, use a color detection utility such as:
Instant Eyedropper which runs in the background. You click the icon in your system tray and then any spot on your screen to identify the color of the pixel.
Firefox add-on: colorPicker same idea, but a plug-in.
Others are available for other browsers, and there are also websites that will identify colors from uploaded images, URLs, entire website color schemes, etc.

Scraping Web VBA Excel unclear

I just started learning web scraping and I'm trying to make a code that Search for specific Data in Web page and click on search then Extract specific Data in excel sheet , I succeed to put the variable that I want to look for in the Web page but When I use the search button I receive this error
I donno how to do it or to correct it
this is my code and Button code
VBA Code
Sub clickICC()
Dim ie As Object
Dim form As Variant, button As Variant
Set ie = CreateObject("InternetExplorer.Application")
myjobtyp = InputBox("Enter type of MP,MOD,DATE")
With ie
.Visible = True
.navigate ("http://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
While ie.ReadyState <> 4
DoEvents
Wend
ie.document.getElementsbyname("searchById").Item.innertext = myjobtyp
Set form = ie.document.getElementsbytagname("form")
Set button = form(0).onsubmit
form(0).submit
Do While ie.busy: DoEvents: Loop
Set TDelements = .documents.getElementsbytagname("td")
r = 0
c = 0
For Each TDelements In TDelements
sheet1.Range("A1").Offset(r, c).Value = TDelement.innertext
r = r + 1
Next
End With
Set ie = Nothing
End Sub
the web button code
Anyone have a clue about this or light me on how fixing this ?
You could try to use a CSS selector to target the src string.
document.querySelector("input[src*=""/cmh/cmh/image/button_search.gif""]").Click
Note that there is a typo in your original post:
Set TDelements = .documents.getElementsbytagname("td")
There is no s on the end of .document.

Excel, VBA and web automation

I have a very big Excel spreadsheet that includes addresses. I want to write a code to automatically open https://www.greatschools.org/, one by one paste addresses in the search box, get the name of best schools for this address, and paste them into the Excel file.
I use this piece of code to paste one of the addresses in the web page but when it pastes and push the search button it doesn't show me any result.
Sub SearchBot()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer
'special object variable representing the IE browser
Dim aEle As HTMLLinkElement
'special object variable for an <a> (link) element
Dim y As Integer
'integer variable we'll use as a counter
Dim result As String
'string variable that will hold our result link
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.greatschools.org/"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
objIE.document.getElementsByClassName("form-control")(0).Focus
objIE.document.getElementsByClassName("form-control")(0).Value = _
Sheets("Sheet1").Range("A1").Value
'click the 'go' button
objIE.document.getElementsByClassName("input-group-btn")(0).Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
I even added the above line code to give it more time to find result but this doesn't help me at all.
At first, it pastes the address in the search box and after a while the address disappears from the search box and there is no result for me there.
Can anybody help me solving this problem?
Thank you.
These two lines between the Do While loops worked for me:
objIE.Document.getElementsByClassName("form-control")(1).Value = Sheets("Sheet1").Range("A1").Value
'click the 'go' button
objIE.Document.getElementsByClassName("btn search-btn")(0).Click
Notice the use of (1) index on form-control entry.

VBA extract text value from webpage?

I have a webpage with some text in a HTML Span like so:
<span id="ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate">Expiry Date : 16/02/2018</span>
I am trying to get this value display it as a message in excel using the below code:
Sub PullExpiry()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "https://www.brcdirectory.com/InternalSite//Site.aspx?BrcSiteCode=" & Range("J6").Value
.Visible = True
End With
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
Set getPrice = appIE.Document.getElementById("ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate")
Dim myValue As String
myValue = getPrice.innerText
appIE.Quit
Set appIE = Nothing
MsgBox myValue
End Sub
This was working on my laptop (operating windows) but it does not work on my computer (also operating windows). Both windows are the same version with the same version of IE. I cannot explain it.
I have Microsoft Office and Excel Object libraries turned on in both references.
I get an error about an active x component not being able to create something
Please can someone show me where i am going wrong?
You need to add Microsoft Internet Controls to your references to be able to create the object. You may have to register the shdocvw.dll library on your computer. It may be registered on your laptop already which is why it might be bombing on your computer.
How to register a .dll
MSDN Documentation: look at the last sentence before the C# example.
Similar Question
After playing around a little bit and ensuring everything was registered, this ran fine on my PC:
Public Sub ie()
Dim ieApp As SHDocVw.InternetExplorerMedium
Dim html As HTMLDocument
Set ieApp = New SHDocVw.InternetExplorerMedium
ieApp.Visible = True
ieApp.Navigate "http://internalAddress/"
Do While ieApp.Busy Or ieApp.ReadyState <> 4
DoEvents
Loop
Set html = ieApp.Document.getElementById("myID")
End Sub

Copy specific image from webpage and paste into Excel using VBA

I'm trying to take a character string from a cell, copy it into a free QR code generator (ex| http://goqr.me/ ), and bring the resulting image back into my spreadsheet. (I like this generator because you don't need to click "submit" - it updates the image as the text is entered. Assuming/hoping that feature makes this easier)
I'm trying to avoid the installation of new barcode fonts and the purchasing of barcode specific packages.
Using the below code, I can get the browser to open and enter the desired text. I need a way to get the image back into my spreadsheet now. The code I'm using currently just returns "[object]" (as text) in cell C1.
How can I use VBA to get this image onto my clipboard?
Public ieApp As Object
Sub Barcode()
Dim ieDoc As Object
Dim InputBox As Object
Dim qrBoxImage as Object
barcodeTerm = Range("B1").Value
If TypeName(ieApp) = "Object" Or TypeName(ieApp) = "Nothing" Then
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Navigate ("http://goqr.me/")
End If
While ieApp.ReadyState <> 4
DoEvents
Wend
ieApp.Visible = True
Set ieDoc = ieApp.Document
Set InputBox = ieDoc.getElementsByName("text")
Set qrBoxImage = ieDoc.getElementByID("qrcode-preview-image")
InputBox.Item(0).Value = barcodeTerm
Range("C1").Value = qrBoxImage
Set ieDoc = Nothing
End Sub
You can follow the steps detailed over here:
http://social.technet.microsoft.com/wiki/contents/articles/23860.print-screen-to-an-image-using-access-vba.aspx
Although the example is used in Access, it's still VBA code so it still should work in Excel

Resources