Solved

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

Posted on 2004-09-22
7
3,938 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

790 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