Solved

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

Posted on 2004-09-22
7
3,979 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 86

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 86

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

726 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