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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you close a window from VBA, eg Word or IE

Status
Not open for further replies.

cresbydotcom

Technical User
May 22, 2006
234
OK this code will start IE before dinner time but how do you close that same window when the eating stops?
Just to take temptation way from the guys - if they are smart enough they will find how to get round it.

Code:
Public closer As Long
Private Sub Workbook_Open()
 Application.OnTime TimeValue("12:20:00"), "load_expl"
End Sub
Sub load_expl()
 closer = Shell("C:\Program Files\Internet Explorer\IEXPLORE.EXE")
 Application.OnTime TimeValue("13:10:00"), "unload_expl"
End Sub
Sub unload_expl()
 
End Sub

So what is needed in the unload subroutine?

FWIW I have found various FAQ's and threads but they don't seem to apply in this instance.
 
cresbydotcom,

Try this:

Code:
Public OpenIt As Long, CloseIt as Long
OpenIt = Shell("C:\Program Files\Internet Explorer\IEXPLORE.EXE")
CloseIt = OpenIt.Quit

Private Sub Workbook_Open()
 Application.OnTime TimeValue("12:20:00"), OpenIt
 Application.OnTime TimeValue("13:10:00"), CloseIt
End Sub

I haven't been able to test this due to Excel being committed to something I can't stop, but I believe it will work.

[glasses]


----------------------------------------------------------------------------------
"A committee is a life form with six or more legs and no brain." -- L. Long
 
You may try this:
Code:
Sub unload_expl()
 AppActivate closer
 SendKeys "%{F4}"
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ah sendkeys - forgot that one - SendKeys can't work in dilaog boxes and I have come against that in the past. I was looking fo the clever solution.
Let you know which works best.
 
cresbydotcom,
Sounds like you are trying to automate a process? Why not use an Object reference to the application you are trying to spawn?
One (major) benefit is you can detect when the spawned application has completed the task it was opened to perform. You can then close it and move on without relying on static wait times.

[tt]Public gobjChildApp As Object
Private Sub Workbook_Open()
Application.OnTime TimeValue("12:20:00"), "load_expl"
End Sub
Sub load_expl()
Set gobjChildApp = CreateObject("InternetExplorer.Application")
Application.OnTime TimeValue("13:10:00"), "unload_expl"
End Sub
Sub unload_expl()
gobjChildApp.Quit
Set gobjChildApp = Nothing
End Sub[/tt]

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Tried
Code:
OpenIt = Shell("C:\Program Files\Internet Explorer\IEXPLORE.EXE")
but Excel 2002 VB6. doesn't like it and assigning the shell ID to a long variable only produces a single property - itself.

The SendKeys works but I have had problems in the past typing and moving around windows when the SendKeys are sent. Castch it wrong and it closes the wrong window or something more bizarre.

Will try childapp - I have seen references to Child Apps but couln't put it all together.

I will be back.
 
moving around windows when the SendKeys are sent
I suggested AppActivate to avoid this behaviour.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
AppActivate - do it, it is the slim chance of fast typing catching the gap - then things lock up and the source code may be visible.

Code:
 Set gobjChildApp = CreateObject("InternetExplorer.Application")
does everything except load IE! Cild Apps are a description or an Object in the example. No property relates - even .Hwnd that I have come across using API's.
Here at home I am running on Win98 but I think it either doesn't do the job or not with Excel 2002, VB 6.3, Win98 anyways.

found this originally but it is Excel within IE
I am perusing it.
 
cresbydotcom said:
does everything except load IE!
Check the Task Manager, I'll bet you will see an instance of Internet Explorer. When you spawn an application (IE, Excel...) using [tt]CreateObject()[/tt] the visible property is usually false. Add the following line after you create the object:
Code:
 gobjChildApp.Visible = True

When you are done with the object you should be able to call [tt]Quit[/tt] to destroy the object but you will need to confirm this for the application you are trying to automate.

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
I was wondering but dodn't see a visible property - maybe I was looking for less obvious things. - never thought to look at the task manager

Now I bit the bullet and tried the code I found in the FAQ.
In it's original it does the job so I put my time constraints and it it just fine. The added advantage is that it can be run when there are no IE windows so i run it 20 times and clean-out all IE windows.

The original problem still requires I hide links and IE instances - we will see.

Will look at the task manager. I wanted to get the simpler code to run. I have used API's aplenty but the target system is Win XP so I don't want to have to develop on that machine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top