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.
Related
I am need of VBScript code to paste the content from one IE Explorer WebPage window to another IE Explorer WebPage window.
Currently i know the Process ID.
However I am not able to switch from one webpage to another. Or you can say my control does not move from one page to another.
#shellbye
Function Bmc(abc,xyz)
Set objIE = CreateObject("InternetExplorer.Application")
Call objIE.Navigate("// SOME APLLICATION") ' #opened the Web applcation
'# LOGGED INTO THE APPLCATION SUCCESSFULLY suing the VBScript
For i = 0 To objIE.Document.links.Length - 1
if objIE.Document.links(i).innerText ="Create a New Case" Then
Do Until objIE.ReadyState = PAGE_LOADED : Call WScript.Sleep(100) : Loop
objIE.Document.links(i).click '## THIS OPENS A NEW Window as link is an URL
Exit For
End If
End Function
In above, I am able to Open the new window after clicking the link i.e. at Step ##
Now I want to copy the content abc and xyz passed in the function Bmc(abc,xyz) to New Window opened through this.
However I am unable to move or us can say switch control to new window.
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 have an application that I use to play text based games. This application has scripting built into it it so you can create your own plugins. It supports various scripting languages such as JScript, VBScript, and LUA. What I want to do is create a plugin that is capable of logging into an account on a website and then perform various actions, like click a button or submit information. I've done a lot of research into this and I've made some degree of progress, but I keep running into issues. I'm not sure if what I'm trying to accomplish is even possible in the way I'm trying to do it.
So far I've been able to open up the browser and navigate to a page using CreateObject.
function WindowLoad
IE.Document.getElementByID("user").value = "testacct"
end function
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "http://www.mudconnect.com/SMF/index.php?action=login2"
Set IE.Window.OnLoad = GetRef("WindowLoad")
I'm now trying to figure out a way to fill the input fields with my information. At first I tried to use IE.Document.getElementByID(), however it was displaying this error message: Object required: 'Document.getElementByID(...)'
After doing some research I found out that this could be because it's trying to find the element before the page is loaded. So I decided to try the OnLoad method. However now I can't seem to find any way to reference the "Window." I've tried IE.Window.Onload, IE.Document.Window.Onload, and several other references, but it keeps saying: Object doesn't support this property or method: 'Window'. Anyone have any suggestions for me?
I've also tried:
Do While (IE.Busy)
Loop
IE.Document.getElementByID("user").value = "testacct"
I've managed to automatically fill out the form and submit it. My next goal was to be able to navigate while logged in and perform other actions such as voting. I wanted to set up a timer in my client to vote once a day, in case I forget to do it. However, the browsers behavior seems to be erratic once I try to perform another navigate. Here's my updated code:
dim pause
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "http://www.mudconnect.com/SMF/index.php?action=login"
while IE.Busy
pause = pause + 1
wend
world.note pause
IE.Document.Forms(1).Action = "http://www.mudconnect.com/SMF/index.php?action=login2"
IE.Document.Forms(1).Elements(0).value = "myUserName"
IE.Document.Forms(1).Elements(1).value = "myPass"
IE.Document.Forms(1).Submit
while IE.Busy
pause = pause + 1
wend
world.note pause
IE.navigate "http://www.mudconnect.com/cgi-bin/vote_rank.cgi?mud=Legends+of+the+Jedi"
while IE.Busy
pause = pause +1
wend
world.note pause
IE.Document.Forms(0).Submit
It doesn't appear to be navigating to the new page for some reason. It somehow gets sent somewhere else on the website. Another problem seems to be that it doesn't log me in if I set up a navigate to go somewhere else. I've spent hours reading up on the IE object documentation, but I can't seem to figure out what the issue is. I'm also curious if the IE.Document values get updated whenever you navigate to a new URL?
I solved this part of the problem. The best advice I have for anyone dealing with these particular issues is to pay close attention to the number of forms and elements on a page. View each form as a slot in an array of "forms." Then within each of these forms there is also an array of "elements." A lot of my problems were the result of not identifying the proper "form" or "element" within the array.
However, I still was unable to make this script login AND navigate afterwards. The solution I developed in light of this involved creating a new IE object and having it finish the task for me. It would be more elegant to have the IE object navigate a second time, but I was unable to figure that out. For now I'm going to flag this as solved, since I have found a way to make it work, but I am curious if there is a way to solve the issue I was having.
The easiest thing is to wait on the Busy property.
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "http://www.mudconnect.com/SMF/index.php?action=login2"
While IE.Busy
WScript.Sleep 1000
Wend
IE.Document.getElementsByName("user")(0).value = "testacct"
I believe WScript.Sleep will work for sleeping but if not look at this answer. I've never used VBScript but the principle is the same regardless of language.
Another problem, which I noticed when I went to that page, is that no element has the id "user." There's an input box with the name user but that's different. getElementsByName returns an array though.
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.
I'm trying to build a local HTA (HTML Application) that will send me to a website I designate. I've got most of it working, as in it currently allows me to type in an address and choose whether to open that URL in either Internet Explorer or Firefox (both in normal and in private).
What I'm asking is if there is a vbscript I can add onto my HTA that will allow me to drag and drop a shortcut that is on my desktop and click a button to do the same as typing it out. Essentially, I would love for it to be similar to Google Image search. Like you can either type in or drag and drop an image to search. This is just for me to quickly open webpages that I have a shortcut saved. I know i could use the favorites option, but I often use many different computers and I have a flash drive that has all my stuff on it.
You want to drag file (url shortcut in this case) and drop it inside open .hta window? If so then looks like what you are looking for is not yet implemented in HTML Application.
Edit
If you have recent Internet Explorer version (9+) then you can play with the HTML5 native
drag and drop feature. As I'm on XP where IE8 is the most upper version, I can't help
in that challenge.
If that so difficult or not applicable then may consider plan B, where can make a function
on start up that scan your Desktop for *.URL files and populate them in Select element, so you'll end with something like:
<select id="urlList">
<option value="" selected>---</option>
<option value="http://...">Shortcut1.url</option>
<option value="http://...">Shortcut2.url</option>
</select>
<input id="urlAddress" type="text">
...and can control it with OnChange event by including a handler into your Head section:
Sub urlList_OnChange()
Document.All.urlAddress.Value = _
Me.Options(Me.selectedIndex).Value
End Sub
Here is a quick snippet to get started.
Set WshShell = CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strDesktop)
For Each f In fld.Files
If f.Type = "Internet Shortcut" Then
MsgBox f.Path & vbNewLine & ReadURL(f.Path)
End If
Next
Function ReadURL(urlFile)
With CreateObject("Scripting.FileSystemObject")
With .OpenTextFile(urlFile, 1)
.SkipLine
ReadURL = (Split(.ReadLine, "="))(1)
End With
End With
End Function
Yes, you can, see this article from Hey, Scripting Guy (the best resource for Vbscript and Powershell stuff). Dragging something onto a shortcut is almost the same as passing parameters to a script (HTA here).
http://blogs.technet.com/b/heyscriptingguy/archive/2005/04/20/how-can-i-pass-command-line-variables-to-an-hta-when-it-starts.aspx