Solved

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

Posted on 2013-12-12
12
4,675 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
  • 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 400 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 69

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 69

Assisted Solution

by:Qlemo
Qlemo earned 100 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
excel 2016 program to loop through scripts to apply filename 2 19
VBScript Write Column Headers 3 32
Passing Credentials into a command line 13 32
Clear Filter 8 30
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now