Solved

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

Posted on 2003-12-08
6
231 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
ID: 9896633
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
ID: 9908653
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
ID: 9909264
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
ID: 9909548
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
ID: 10217813
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

808 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