I tryed to make a script to detect the IE window that is opened on Facebook and hit the Like button.
Let's say I have 10 IE opened. Only one of them is on a page on Facebook.
I want my script to detect that IE window, click this button:
IE.Document.getElementById("pagesHeaderLikeButton")
(The above button is the Like button)
and close that IE window.
I tryed to get that IE window by:
For Each wnd In CreateObject("Shell.Application").Windows
If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
Set IE = wnd
Exit For
End If
Next
But this will only set my VBscript to the first opened IE and it will not find the Facebook window.
I tryed this:
Dim objInstances, item
Set objInstances = CreateObject("Shell.Application").windows
For Each item In objInstances
If Item.Name Like "*Internet*" And Item.document.URL Like "*facebook.com*" Then
IE.Document.getElementById("pagesHeaderLikeButton").Click
End If
Next
But I get "Sub or function not defined"
The following code will search for open IE windows that have "facebook.com" in its URL and save them in a collection:
Dim getIE As New Collection
For Each Item In CreateObject("Shell.Application").Windows
If Item.Name Like "*Internet*" And Item.document.URL Like "*facebook.com*" Then
getIE.Add Item
End If
Next
Then you can loop through the collection and do what you want with the IE items:
For Each itemIE In getIE
itemIE.Document.getElementById("pagesHeaderLikeButton").Click ' For example
Next itemIE
Hope this does what you wanted! ;)
Next code snippet could help:
Set shApp = CreateObject( "shell.application")
With shApp
For Each wnd In .Windows
If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
If InStr(1, wnd.document.URL, "facebook.com", vbTextCompare) > 0 Then
Wscript.Echo "THIS:"
End If
Wscript.Echo Left( wnd.document.URL, 70)
End If
Next
End With
Output example (with more facebook.com
matches):
==>cscript D:\VB_scripts\SO\30717779a.vbs
http://www.msn.com/?ocid=iehp
THIS:
https://www.facebook.com/login.php?api_key=127760087237610&skip_api_lo
THIS:
https://www.facebook.com/literarnifestival1
THIS:
https://cs-cz.facebook.com/mgvsetin
http://www.bing.com/search?q=Xmaster+Official&qs=n&form=QBRE&pq=xmaste
http://www.bing.com/search?q=%22Xmaster+Official%22&qs=n&form=QBRE&pq=
==>
For Each wnd In CreateObject("Shell.Application").Windows
If InStr(wnd.Name,"Internet") Then
if InStr(wnd.Document.URL,"facebook.com") Then
Set IE2 = wnd
Exit For
End If
End If
Next
So to press the button will be like this:
Set Butlike = IE2.Document.getElementsByTagName("button")
For Each btn In Butlike
If btn.type = "submit" Then btn.Click()
Next