Link to home
Start Free TrialLog in
Avatar of ARampton
ARamptonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to bring an Internet Explorer window to the front with VB Script

I am using an IE window to write progress of a VB Script which works well but does not make the IE window the foreground application so it can get lost behind other active applications
eg
set objExplorer = WScript.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.Document.Title = "Project Creation Utility"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 600
objExplorer.Height = 900
objExplorer.Left = 300
objExplorer.Top = 0

objExplorer.Document.Body.InnerHTML = "<HTML><BODY>"                                    
objExplorer.Document.Body.InnerHTML = objExplorer.Document.Body.InnerHTML & "<H1>Project Creation </H1><B>New Structure   v2.0</B>"
WriteIE("<H2>Getting Project Details</H2>")
WriteIE("Reading Project Number ... ")

What do I need to add to ensure this IE window is in front of every other window on my screen?
Avatar of kaufmed
kaufmed
Flag of United States of America image

I don't think VBScript will give you access to the Windows API, which, AFAIK, is what you would need to bring a window--that is not associated with the code you are running--to the foreground. Can you use another scripting language like AuthoHotkey or AutoIT?
Are you able to use the AppActivate method instead?

http://msdn.microsoft.com/en-us/library/wzcddbek(v=vs.84).aspx

It should work, but you need to know the unique title of the window you want to give the focus to.
ASKER CERTIFIED SOLUTION
Avatar of Jez Walters
Jez Walters
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ARampton

ASKER

JezWalters

Using objShell.AppActivate works after the IE window has been selected with a mouse and is then hidden behind another application  - IE seems to have some other state while my script runs that makes objShell.AppActivate inactive
Can you post a sample script, that demonstrates the problem you are experiencing with AppActivate?
Sample of my code below writes progress to IE window but won't bring IE window in front of Windows Explorer window where script is run
' Stubbs Rich Folder Creation Utility v2.1


' Set up IE window for output.
Set ieShell = WScript.CreateObject("WScript.Shell")
Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.Document.Title = strIEWindowTitle 	'"Stubbs Rich Project Creation Utility"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 600
objExplorer.Height = 900
objExplorer.Left = 300
objExplorer.Top = 0

Do While (objExplorer.Busy)
    Wscript.Sleep 200
Loop

' For WriteIE and WriteLnIE subs
Const HTMLCR = "<BR>"
Set WshShell = CreateObject("WScript.Shell")

objExplorer.Visible = 1  
       
objExplorer.Document.Body.InnerHTML = "<HTML><BODY>"															' HTML Headers
objExplorer.Document.Body.InnerHTML = objExplorer.Document.Body.InnerHTML & "<H1>Stubbs Rich Project Creation </H1><B>New Structure   v2.0</B>"
' START 

	' GET INPUT				
	ieShell.AppActivate "Stubbs Rich Project Creation Utility  - Internet Explorer"
	WriteIE("<H2>Getting Project Details</H2>")
	WriteIE("Reading Project Number ... ")
	strProjNo = InputBox("Please enter the Project Number of the new Project.","Project Number")

	If strProjNo = "" Then
		WriteIE("Cancelled</BODY></HTML>")
		Wscript.Sleep 10000
		objExplorer.Quit
		Wscript.Quit
	End If
	
	strProjNoError = ""	
	If IsNumeric(strProjNo) Then
		If strProjNo = CStr(CLng(strProjNo)) Then	
			If strProjNo > 0 And strProjNo < 10000 Then
				WriteLnIE(strProjNo)
			Else
				strProjNoError = "Error"
			End If
		Else
			strProjNoError = "Error"
		End If
	Else
		strProjNoError = "Error"
	End If

	If strProjNoError = "Error" Then
	MsgBox(strProjNo & " format not Valid")
		WriteIE("<H2><B>Aborted</B></H2></BODY></HTML>")
		WriteIE("<H2><B>Project Number " & strProjNo & " format not Valid</B></H2></BODY></HTML>")
		Wscript.Sleep 10000
		objExplorer.Quit
		Wscript.Quit
	End If

	WriteIE("Reading Project Name ... ")

	strProjName = InputBox("Please enter the Project Name of the new Project.","Project Name")
	If strProjName = "" Then
		WriteIE("Cancelled</BODY></HTML>")
		Wscript.Sleep 10000
		objExplorer.Quit
		Wscript.Quit
	Else
		strProjNameValid = True		
		arrIllegalCharacters = Array("/", "\", ":", "*", "?", """", "<", ">", "|")
 
		For i = LBound(arrIllegalCharacters) To UBound(arrIllegalCharacters)
			If InStr(1, strProjName, arrIllegalCharacters(i),1) > 0 Then
				strInvalid = strInvalid & arrIllegalCharacters(i)
				strProjNameValid = False
			End If
		Next
 	
		If strProjNameValid = True Then
			WriteLnIE(strProjName)
		Else
			MsgBox(strProjName & " not Valid")
			WriteIE("<H2><B>Aborted</B></H2></BODY></HTML>")
			WriteIE("<H2><B>Project Name  " & strProjName & "  is using invalid characters  " & strInvalid & "</B></H2></BODY></HTML>")
			Wscript.Sleep 10000
			objExplorer.Quit
			Wscript.Quit
		End If
	End If

	WriteLnIE("")
	WriteIE("Confirm creation of new Project in <B>" & strRootFolder & "</B> ... ")

	' Check user is sure they want to continue.
	intReply = 0
	intReply = MsgBox("Create new Project in " & strRootFolder & " ?", 4, "Confirm Action")

	' If user chooses 'No' then exit script.
	If intReply = 7 Then
		WriteIE("Cancelled</BODY></HTML>")
		Wscript.Sleep 10000
		objExplorer.Quit
		Wscript.Quit
	Else
		WriteLnIE("OK")
	End If


	WriteIE("Completed")
	
	
	WriteLnIE("")
	WriteLnIE("")
	WriteIE("<H2>Finished</H2>")

	Wscript.Sleep 1000
	Msgbox("Finished")
	objExplorer.Quit

Wscript.Quit

' Write output to IE window with carriage return.
Sub WriteLnIE(MsgIn)
	objExplorer.Document.Body.InnerHTML = objExplorer.Document.Body.InnerHTML & MsgIn & HTMLCR
	WshShell.AppActivate (strIEWindowTitle)
	WshShell.SendKeys "{PGDN}"
End Sub

	' Write output to IE window without carriage return.
Sub WriteIE(MsgIn)
	objExplorer.Document.Body.InnerHTML = objExplorer.Document.Body.InnerHTML & MsgIn
	WshShell.AppActivate (strIEWindowTitle)
	WshShell.SendKeys "{PGDN}"
End Sub

Open in new window

I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
I am still seeking a solution to this issue I have been unable to do myself
I am still seeking a solution to this issue as I have been unable to do myself
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not a solution I can use yet