I am looking to automate our reporting. We currently have 5 Tabs in Firefox that we refer to for reporting. Each Tab has a unique url that changes each morning. We then have to each morning update these URLs (from an excel speadsheet). I would like to automate this. I do have a few constraints on this though. I cannot open a new tab for each new url (so basically it needs to be a url replace in each tab). I have looked into selenium but cannot get it to work
http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-from-an-excel-spreadsheet/
and
http://www.makeuseof.com/tag/how-to-automate-firefox-or-chrome-with-vba-and-selenium/
So I was wondering if it could be done another way either using pure VBA or a batch file?
You need to add a reference to Microsoft Internet Controls and Microsoft Shell Controls and Automation. See screenshot below.
Code:
Sub Work_With_Open_IE_Instance()
Dim objShell As Shell
Dim objIE As InternetExplorer
Dim objWin As Object
Set objShell = New Shell
For Each objWin In objShell.Windows
If TypeName(objWin.Document) = "HTMLDocument" Then
Set objIE = objWin
'~~> This will display the URL of the IE which we will use to bind
Debug.Print objIE.LocationURL
'~~> Example to bind
'~~> Change URL to your existing URL
Select Case objIE.LocationURL
Case "https://www.google.com"
With objIE
.Refresh
'
'~~> Rest of the code
'
End With
Case "http://in.msn.com"
With objIE
.Refresh
'
'~~> Rest of the code
'
End With
End Select
End If
End If
Next objWin
End Sub
The Select Case is just for demonstration purpose. If you want to work with them one after the other then don't use Select Case. Use If/EndIf one after the other.
Related
I want to create a VBScript script to detect Internet Explorer opening page and opening new tab in the same window. For example, when I manually open Internet Explorer at www.google.com the VBScript will do:
detect and open new tab in same window with www.example.com
but do this only once
I tried with this code:
Set wshShell = CreateObject("WScript.Shell")
Do
page1 = wshShell.AppActivate("Blank page - Internet Explorer")
If page1 = True Then
Set page2 = CreateObject("InternetExplorer.Application")
page2.Navigate "http://www.example.com", CLng(navOpenInNewTab)
End If
WScript.Sleep 500
Loop
For one thing, you must use the existing Internet Explorer instance instead of creating a new one. Unfortunately the way one might expect to be able to do that (GetObject(, "InternetExplorer.Application")) does not work for Internet Explorer COM objects. AppActivate also doesn't help here, because it doesn't return a handle to the application object, which you need to invoke its methods. Instead you need to do it this way:
Set app = CreateObject("Shell.Application")
For Each wnd In app.Windows
If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
Set ie = wnd
Exit For
End If
Next
If you want to select an instance that has a particular page opened, you can for instance check the title of that page to make the selection:
wnd.Document.Title = "something"
or
InStr(1, wnd.Document.Title, "something", vbTextCompare) > 0
Your second problem is that VBScript doesn't recognize the symbolic constant navOpenInNewTab. You must either use the numeric value (as defined in the BrowserNavConstants enumeration):
ie.Navigate "http://www.example.com", CLng(2048)
or define the constant yourself first:
Const navOpenInNewTab = &h0800&
ie.Navigate "http://www.example.com", navOpenInNewTab
Note that you must use hexadecimal notation with a trailing ampersand here, because the value must be a Long and you must use literals in Const definitions. Expressions like calling the function CLng are not valid.
Alternatively you can open a URL in a new tab by omitting the second parameter of the Navigate method entirely and instead providing the value "_blank" for the third parameter:
ie.Navigate "http://www.example.com", , "_blank"
However, I'm not certain that this would always open a new tab in the current window (might depend on the browser's tab settings), so I'd recommend using the second parameter (flags) as described above.
So I have a really annoying set of forms at work that I have to fill out daily and spend about 20 minutes doing so, I was looking to script this and have a basic sendkeys method that works but I wanted to improve this.
Dim WshShell, objIE, ElementCol
Dim LinkHref
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Webpage Title"
Set objIE = CreateObject("InternetExplorer.Application")
wshShell.AppActivate objIE
Do While objIE.Busy
wscript.sleep 100
Loop
Set ElementCol = objIE.document.getElementsByTagName("a")
For Each Link In ElementCol
If Link.innerHTML = "*Specfic text" Then
Link.Click
Exit For
End If
Next
So I get an error on the Set ElementCol line that says null thats my first issue
For the *specific text is this allowed? Basically I know the text of the links will be Hello and world but could pop in like 1) Hello and 2) World or 1) World 2) Hello
I also want to after the click run a different .vbs script that fills out the form after that .vbs script completes it returns back to this initial page where I want to continue on this script to the next link.
Thanks a ton for any help!
I am trying to run a vbscript that will load up a html page in chrome and then do control+a which will select everything on the page, with the ultimate view to copy it and then paste it into excel.
this my script so far:
Set WshShell = WScript.CreateObject("WScript.Shell")
Dim iURL
Dim objShell
iURL = "C:\Users\Aasfasf\AppData\Local\Temp\TD_80\hpqc\52136023e30\****\121200.html"
set objShell = CreateObject("WScript.Shell")
objShell.run(iURL)
WScript.Sleep 1500
WshShell.SendKeys "^a"
when I run it loads up the html page, but i dont think the control+a command is working as nothing is selected.
Two suggestions:
The focus is probably not on the content in chrome. Try sending a Tab key press a couple of times first to see if that fixes it.
A better approach to this might be to just read the contents of the file, then delete all the markup from the document using regex.
Excel can just load your web page without needing any help from you.
Alt + D, D, W
I want to open an Internet Explorer page using vbscript, currently my script is as follows
Dim IE
Dim MyDocument
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 0
IE.navigate "https://google.com"
This works fine. The problem is that I need to get the response of my webpage (google.com) i.e its contents in string format
I know it will be mostly gibberish but I require it for what I am working on
Thanks
You're almost there already. You just need to wait until the page has finished loading
While IE.ReadyState <> 4 : WScript.Sleep 100 : Wend
then you can access the page text like this:
WScript.Echo IE.document.body.innerText
or the page HTML like this:
WScript.Echo IE.document.body.innerHTML
Is there a way that I could automate the right-clicking of a file in a Windows 7 folder and select the, "Send To -> Amazon Cloud Drive" context menu option in a simple VB script?
The answer is yes and no. There is no direct way of doing this. You could create a workaround but it would be very involved.
The "proper" approach is to use the ShellFolderItem object's InvokeVerb method. It looks like this:
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("C:")
Set objFile = objFolder.ParseName("test.vbs")
' Execute context menu item
'objFile.InvokeVerb("&Copy")
' List all possible verbs
Set colFolderItemVerbs = objFile.Verbs
For Each objFolderItemVerb in colFolderItemVerbs
WScript.Echo Chr(34) & objFolderItemVerb.Name & Chr(34)
Next
The problem is that submenu items are listed as empty strings.
As one possible workaround is to navigate to the Send To folder and grab the command line for the shortcut you want to use. You could then implement it directly.