Solved

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

Posted on 2013-12-12
12
4,085 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:ARampton
Comment Utility
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
Comment Utility
Can you post a sample script, that demonstrates the problem you are experiencing with AppActivate?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:ARampton
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
I am still seeking a solution to this issue I have been unable to do myself
0
 

Author Comment

by:ARampton
Comment Utility
I am still seeking a solution to this issue as I have been unable to do myself
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
Comment Utility
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
Comment Utility
Not a solution I can use yet
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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