Solved

VBA: is it possible to run a console window in VBA?

Posted on 2004-09-22
7
3,877 Views
Last Modified: 2008-02-01
Hi,

Is it possible to do something like this in VBA? I want to have a console application (a batch file) run and it's output displayed within a VBA form. If not a form, then at least some kind of window created by VBA code?

However, unlike the article I want it to run synchronously if possible. I want the VBA code to wait for the batch file to finish.

"Attach a Console Window to Your Visual Basic Program"
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q171/6/54.asp&NoWebContent=1
0
Comment
Question by:ycomp
  • 3
  • 3
7 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 300 total points
ID: 12129868
Your program will be a hybrid of a Pipe redirection app and a ShellAndWait app.

Pipe redirecting:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q173/0/85.ASP&NoWebContent=1

As for ShellAndWait() routines, the most common ones wait for an infinite period until the process is complete.  This blocks the code and wouldn't allow you to redirect the pipes (from above).  You would have to use a polling ShellAndWait() instead.

Here is an example of a polling ShellAndWait from Vinnyd79 at this PAQ:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20740367.html

    Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
    Const STILL_ACTIVE = &H103
    Const PROCESS_QUERY_INFORMATION = &H400

    Private Sub ShellWait(ByVal JobToDo As String)
        Dim hProcess As Long
        Dim RetVal As Long
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(JobToDo, 1))
        Do
            GetExitCodeProcess hProcess, RetVal
            DoEvents: Sleep 100
        Loop While RetVal = STILL_ACTIVE
    End Sub

    Private Sub Command1_Click()
        ShellWait "Calc.exe"
        MsgBox "Calc.exe has closed!"
    End Sub
0
 

Author Comment

by:ycomp
ID: 12129944
thanks idle, it works.. cool.

So I guess it's not possible to run the batch file within a form control or something, no? I mean to have the output of the batch file run within a form?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12129959
Do you want the actual console (DOS) window to be a child of your form?

Idle_Mind
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:ycomp
ID: 12129983
yes, I want the console's output to be displayed within the form, as the output is coming out of the command window. I mean, as it is running the output is displayed in the form.

Or at least in a way that it looks like it is in the form or close to the form. I mean if it's not possible to actually do that then at least to resize it and place it above centered over part of the form.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12129995
I'll see if I can come up with an example for you.

~IM
0
 

Author Comment

by:ycomp
ID: 12130025
cool, thanks... 'cause I have absolutely no clue how to do it.
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 200 total points
ID: 12132265
Try this


Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "Your_fullpath_batfile", 1, true
Set wshShell = Nothing
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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

775 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