Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DoEvents Emulation

Status
Not open for further replies.

walkingseed

Technical User
Jan 1, 2006
12
US
I have a program that has report templates that are stored in XML format. The CDATA section has VBSCRIPT contained within it. I need to be able to insert data into the report dynamically at execution time. What I need to be able to do is pause execution of the script without pinning the CPU until the Internet Explorer Application closes. I tried instantiating the InternetExplorer object using Wscript.CreateObject("InternetExplorer.Application", "IE_")

so that I can trap events. but I get and Error 5800 Object Required error. I figure if I can trap events I can launch a shell using the Object.Run method, halting the script, which effectively emulates the DoEvents. When I close the IE object I should then be able to detect that event and close the shell thereby resuming the script. I need to detect the termination of IE so that I can pull data from htm before it is closed.

Is it possible to do event detection without Wscript?
Can I pull the data from the Internet Explorer form when using the Object.Run method? This would work too if it is possible but I haven't been able to figure out how.

'Do some stuff to create a new htm file form

' Launch Internet Explorer, and connect event handler.

Set objIntExplorer = CreateObject("InternetExplorer.Application")

' Format the Internet Explorer Form
objIntExplorer.Left = 50 ' Window position and other properties
objIntExplorer.Top = 100
objIntExplorer.Height = 500
objIntExplorer.Width = 600
objIntExplorer.MenuBar = 0 ' No menu
objIntExplorer.ToolBar = 0
objIntExplorer.StatusBar = 0
objIntExplorer.navigate "C:\PipTrackerStds\PipStds.htm" ' Load form.
objIntExplorer.Visible = 1 ' Keep visible.

' Wait until Internet Explorer is ready.
Set cmdShell = CreateObject("Wscript.Shell")
cmdShell.Run "cmd", 2, True

'Trash Collection
Set cmdShell = Nothing
Set objExplorer = Nothing

'Detect the Quit Event on the IE object
Sub IE_Quit()

'Do the reporting stuff
'...
'Deleted reporting code
'...

cmdShell.AppActivate("cmd.exe")
cmdShell.SendKeys("exit~")

End Sub
 
I see quite a few of conceptual errors.

[11 By fortune or misfortune, the ie application quit event is called "onquit", contrary to some other applications from the same ms! Hence, the function should be named accordingly if you want the callback to succeed.
>Sub IE_Quit()
[tt]Sub IE_[red]on[/red]Quit()[/tt]

[2] You should continue to use wscript native createobject method to profit on the capturing the callback: there is no reason of what you describe to make it faulty as far as I know. If it fails, it is only because you have not made use of it correctly.
>Set objIntExplorer = CreateObject("InternetExplorer.Application")
[tt]Set objIntExplorer = wscript.CreateObject("InternetExplorer.Application","IE_")[/tt]

[3] You've a synchronous cmd process. If you want to profit the callback, you somehow should let to behave asynchronously.
>cmdShell.Run "cmd", 2, True
[tt]cmdShell.Run "cmd", 2, [red]false[/red][/tt]

[4] In order the script to sitting there stand-ready for the onquit event to be triggered by the application (and this may be the central part of your question) you can do it like this.
[4.1] Set up a global variable, say, b_closed, initialized to false.
[4.2] Set up a loop (emulating doevent) checking the global variable see if it is changed to true, if not continue to wait.
[4.3] In the onquit event, once being called, add a line to change the global b_closed to true, so that the script itself knows the onquit is actually happened.

Since those are quite a bit of tightly correlating and easily got lost, this is how the layout looks like.
[tt]
[blue]dim b_closed
b_closed=false[/blue]
'Do some stuff to create a new htm file form
' Launch Internet Explorer, and connect event handler.
Set objIntExplorer = [blue]wscript.[/blue]CreateObject("InternetExplorer.Application"[blue],"IE_"[/blue])
' Format the Internet Explorer Form
objIntExplorer.Left = 50 ' Window position and other properties
objIntExplorer.Top = 100
objIntExplorer.Height = 500
objIntExplorer.Width = 600
objIntExplorer.MenuBar = 0 ' No menu
objIntExplorer.ToolBar = 0
objIntExplorer.StatusBar = 0
objIntExplorer.navigate "C:\PipTrackerStds\PipStds.htm" ' Load form.
objIntExplorer.Visible = 1 ' Keep visible.

' Wait until Internet Explorer is ready.
Set cmdShell = CreateObject("Wscript.Shell")
cmdShell.Run "cmd", 2, [red]false[/blue]

[blue]do while not b_closed
wscript.sleep 1000
loop[/blue]

[blue]'continue some other tasks if need to[/blue]

'Trash Collection
Set cmdShell = Nothing
Set obj[red]Int[/red]Explorer = Nothing

'Detect the Quit Event on the IE object
Sub IE_[red]on[/red]Quit()

'Do the reporting stuff
'...
'Deleted reporting code
'...

[red]'I don't like that kind of operation which is bound to fail more often than not, but it is not mine[/red]
cmdShell.AppActivate("cmd.exe")
cmdShell.SendKeys("exit~")

[blue]b_closed=true[/blue]

End Sub
[/tt]



 
Thanks, but I had already tried everything that you recommended.

This script must run without Wscript because I do not have access to the Wscript root object from within the application that the script is launched. In fact, trying to use Wscript causes errors. This means that I do not have access to the CreateObject, Sleep or DoEvents methods. I have access to the CreateObject function... but that does not raise events.

I did discover a solution however. Rather than use an Internet Explorer Form, I created a form in Excel. The Run method pauses execution of the script whenver the RunForm macro within Excel opens the UserForm. This little bit of code does exactly what I need it to do and executes about five times faster than dynamically building and opening an html file:


set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\PipTrackerStds\PipStds.xls")

objExcel.Run "RunForm"

objExcel.Quit
set objExcel = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top