Solved

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

Posted on 2004-09-22
7
3,772 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now