ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help: is it possible to launch acommand prompt shell and have commands piped into it? (https://www.excelbanter.com/excel-programming/384060-help-possible-launch-acommand-prompt-shell-have-commands-piped-into.html)

Clinton M James[_2_]

Help: is it possible to launch acommand prompt shell and have commands piped into it?
 
Hi folks,

Is anybody able to tell me if and how it is possible to have vb open a
command prompt and have commands or text i specify piped into it to carry
out some tasks?

Help is appreciated.

Thank you in advance.

Clint



RB Smissaert

is it possible to launch acommand prompt shell and have commands piped into it?
 
Try this code:


Option Explicit
Private Const NORMAL_PRIORITY_CLASS = 32
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadId As Long
End Type

Private Declare Function WaitForSingleObject _
Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As
Long

Private Declare Function CreateProcessA _
Lib "kernel32" (ByVal lpApplicationName As String,
_
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long,
_
ByVal lpThreadAttributes As Long,
_
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As
String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As
PROCESS_INFORMATION) As Long

Private Declare Function GetExitCodeProcess _
Lib "kernel32" (ByVal hProcess As Long, _
lpExitCode As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Function ExecCmd(sCmdLine As String, lmSecsWait As Long) As Long

'will start an external process and
'wait till this process is finished
'returns -1 if successfull and -1 if not
'-----------------------------------------
Dim proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
Dim lReturn As Long

'Initialize the STARTUPINFO structu
With Start
.cb = Len(Start)
.dwFlags = 1 'hexadecimal would be &H1
'.wShowWindow = 0
End With

'Start the shelled application:
lReturn = CreateProcessA(sCmdLine, _
vbNullString, _
0, _
0, _
1, _
NORMAL_PRIORITY_CLASS, _
0, _
vbNullString, _
Start, _
proc)

'Wait for the shelled application to finish:
'6 seconds is enough here
lReturn = WaitForSingleObject(proc.hProcess, lmSecsWait)
GetExitCodeProcess proc.hProcess, lReturn
CloseHandle proc.hThread
CloseHandle proc.hProcess
ExecCmd = lReturn

End Function



sCmdLine can be the path of a .bat file.


RBS



"Clinton M James" wrote in message
...
Hi folks,

Is anybody able to tell me if and how it is possible to have vb open a
command prompt and have commands or text i specify piped into it to carry
out some tasks?

Help is appreciated.

Thank you in advance.

Clint



Krayten

Help: is it possible to launch acommand prompt shell and have commands piped into it?
 
I needed to do this recently and ended up creating a .cmd or .bat
file, which contained
the actions I needed then getting excel to open and run that.

Think I had about 4 scenarios (based on excel user input variations)
so just wrote a .cmd for
each scenario and it worked fine.

It was simpler than trying to get excel to "drive" the .cmd session,
though I am certain that
ppl here would be able to get that to work it was beyond me!

Rgds,

Stuart




All times are GMT +1. The time now is 11:55 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com