Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-12
12
Medium Priority
?
6,349 Views
Last Modified: 2015-02-02
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?
0
Comment
Question by:ARampton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39714795
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
 
LVL 17

Expert Comment

by:JezWalters
ID: 39717439
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
 
LVL 17

Accepted Solution

by:
JezWalters earned 1200 total points
ID: 39717476
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ARampton
ID: 39732327
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
 
LVL 17

Expert Comment

by:JezWalters
ID: 39733634
Can you post a sample script, that demonstrates the problem you are experiencing with AppActivate?
0
 

Author Comment

by:ARampton
ID: 39779928
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 40402816
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
 

Author Comment

by:ARampton
ID: 40402813
I am still seeking a solution to this issue I have been unable to do myself
0
 

Author Comment

by:ARampton
ID: 40402817
I am still seeking a solution to this issue as I have been unable to do myself
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 300 total points
ID: 40404025
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
 

Author Closing Comment

by:ARampton
ID: 40584752
Not a solution I can use yet
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question