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?
ARamptonAsked:
Who is Participating?
 
JezWaltersConnect With a Mentor Commented:
I mean like this:

Option Explicit

Dim objExplorer
Dim objShell

Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Visible = True

Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate objExplorer.Name

Set objShell = Nothing
Set objExplorer = Nothing

Open in new window

0
 
käµfm³d 👽Commented:
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?
0
 
JezWaltersCommented:
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.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ARamptonAuthor Commented:
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
0
 
JezWaltersCommented:
Can you post a sample script, that demonstrates the problem you are experiencing with AppActivate?
0
 
ARamptonAuthor Commented:
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

0
 
QlemoBatchelor and DeveloperCommented:
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".
0
 
ARamptonAuthor Commented:
I am still seeking a solution to this issue I have been unable to do myself
0
 
ARamptonAuthor Commented:
I am still seeking a solution to this issue as I have been unable to do myself
0
 
QlemoConnect With a Mentor Batchelor and DeveloperCommented:
kaufmed is correct. You need access to Win32 API functions, and you can't without writing a wrapper DLL (in another language like C++ or the like).

With PowerShell it is possible, and since VBS and PS are similar (at least for controlling IE), it should be an option to consider.
0
 
ARamptonAuthor Commented:
Not a solution I can use yet
0
All Courses

From novice to tech pro — start learning today.