Solved

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

Posted on 2004-09-22
7
3,829 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 56
SSRS expression Issue finding a string 10 66
I need help formating a date variable in my VB.Net application 2 44
How to Add / Edit Windows Menu 4 52
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 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…

867 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

16 Experts available now in Live!

Get 1:1 Help Now