Solved

Close an open command prompt windows using VB when it has lost focus.

Posted on 2003-12-08
6
228 Views
Last Modified: 2008-02-01
Hi expert,
i open a command prompt windows using vb. let say, the command prompt windows has lost its focus.. meaning that i cannot use sendkeys function to write command in it anymore.. so how i gonna close the command prompt windows? If it still in focus, i just sendkeys "exit" and it will close. But since it lost focus, sendkeys won't work.

thanks..
0
Comment
Question by:Diode_Temp
  • 2
  • 2
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Store the AppID returned from your Shell() call.   Then you can use the AppActivate() function to return focus to the window each you want to use SendKeys().

Option Explicit

Dim MyAppID As Double

Private Sub Command1_Click()
    AppActivate MyAppID
    SendKeys "dir~", True
End Sub

Private Sub Form_Load()
    MyAppID = Shell("command.com", 1)
End Sub
0
 

Accepted Solution

by:
mr_avril earned 20 total points
Comment Utility
i have tried your code.. but it seem like VB cannot close the command prompt windows. it give command prompt windows a focus but it cannot close it.
using the sendkeys u use in your code.. it give something like this in the command prompt windows..

C:\PROGRA~1\MICROS~3\VB98>\
                                                 \
                                                 \
                                                 \
0
 

Expert Comment

by:mr_avril
Comment Utility
i change this..
Private Sub Form_Load()
    MyAppID = Shell("command.com", 1)
End Sub

to this..

Private Sub Form_Load()
    MyAppID = Shell("cmd", 1)
End Sub
  and it works fine.. anyway thanks..
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Glad you got it to work.  The main point was using AppActivate to give it focus before sending the keys.
0
 
LVL 27

Expert Comment

by:planocz
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
No response from Diode_Temp from 12/09/2003 comment
Award points to mr_avril is recommend.
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

planocz
EE Cleanup Volunteer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

6 Experts available now in Live!

Get 1:1 Help Now