Or use the Win32 api function called ShellExecute. It's more complicated, but it also allows you to do things like open web pages, Word documents, etc. just by giving the name of the URL/file.<br>
<br>
Chip H.<br>
Along the same lines, if you're looking to shell to a program and wait for the shelled program to finish before returning to VB, here's a class that will do that for you.<br>
<br>
Option Explicit<br>
<br>
Private Type STARTUPINFO<br>
cb As Long<br>
lpReserved As String<br>
lpDesktop As String<br>
lpTitle As String<br>
dwX As Long<br>
dwY As Long<br>
dwXSize As Long<br>
dwYSize As Long<br>
dwXCountChars As Long<br>
dwYCountChars As Long<br>
dwFillAttribute As Long<br>
dwFlags As Long<br>
wShowWindow As Integer<br>
cbReserved2 As Integer<br>
lpReserved2 As Long<br>
hStdInput As Long<br>
hStdOutput As Long<br>
hStdError As Long<br>
End Type<br>
<br>
Private Type PROCESS_INFORMATION<br>
hProcess As Long<br>
hThread As Long<br>
dwProcessID As Long<br>
dwThreadID As Long<br>
End Type<br>
<br>
Private Declare Function WaitForSingleObject Lib _<br>
"kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds _<br>
As Long) As Long<br>
<br>
Private Declare Function CreateProcessA Lib "kernel32" _<br>
(ByVal lpApplicationName As Long, ByVal lpCommandLine As _<br>
String, ByVal lpProcessAttributes As Long, ByVal _<br>
lpThreadAttributes As Long, ByVal bInheritHandles As Long, _<br>
ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _<br>
ByVal lpCurrentDirectory As Long, lpStartupInfo As _<br>
STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) _<br>
As Long<br>
<br>
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject _<br>
As Long) As Long<br>
<br>
Private Const NORMAL_PRIORITY_CLASS = &H20&<br>
Private Const INFINITE = -1&<br>
<br>
Public Function ExecCmd(strCommandLine) As Long<br>
<br>
'Returns: Nonzero on success, zero on failure.<br>
<br>
Dim tProcess As PROCESS_INFORMATION<br>
Dim tStartup As STARTUPINFO<br>
Dim lReturn As Long<br>
<br>
On Error GoTo ExecCmdErr<br>
<br>
' Initialize the STARTUPINFO structure:<br>
tStartup.cb = Len(tStartup)<br>
<br>
' Start the shelled application:<br>
lReturn = CreateProcessA(0&, strCommandLine, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, tStartup, tProcess)<br>
<br>
' Wait for the shelled application to finish:<br>
lReturn = WaitForSingleObject(tProcess.hProcess, INFINITE)<br>
lReturn = CloseHandle(tProcess.hProcess)<br>
<br>
ExecCmd = lReturn<br>
<br>
Exit Function<br>
<br>
ExecCmdErr:<br>
'Return an error.<br>
ExecCmd = 0<br>
End Function<br>
<p>Steve Meier<br><a href=mailto:sdmeier@jcn1.com>sdmeier@jcn1.com</a><br><a href= > </a><br>
I tried this routing which works fine for notepad. However when using it with wordpad, control would not return to the calling program when exiting the program. (I do not want control to return until the wordpad file is closed) Any suggestions?<br><br>Dim hprog, hProc, RetVal As Long 'Used for Notepad routine<br>Private Declare Function OpenProcess Lib "kernel32" _<br> (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _<br> ByVal dwProcessId As Long) As Long<br>Private Declare Function WaitForSingleObject Lib "kernel32" _<br> (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long<br>Private Declare Function CloseHandle Lib "kernel32" _<br> (ByVal hObject As Long) As Long<br><br> ' This routing works fine<br>Public Sub call notepad()<br> Const PROCESS_ALL_ACCESS = 0<br> hprog = Shell("NotePad.Exe c:\bas\vb\ballot.txt", vbMaximizedFocus) 'returns taskID<br> 'Get process handle<br> hProc = OpenProcess(PROCESS_ALL_ACCESS, False, hprog)<br> 'wait until the process terminates<br> If hProc <> 0 Then<br> RetVal = WaitForSingleObject(hProc, INFINITE)<br> CloseHandle hProc<br> End If<br>End Sub<br><br> ' This routing doesn't work right<br>Public Sub call wordpad()<br> Const PROCESS_ALL_ACCESS = 0<br> hprog = Shell("c:\program files\accessories\WordPad.Exe c:\bas\vb\ballot.txt", vbMaximizedFocus) 'returns taskID<br> 'Get process handle<br> hProc = OpenProcess(PROCESS_ALL_ACCESS, False, hprog)<br> 'wait until the process terminates<br> If hProc <> 0 Then<br> RetVal = WaitForSingleObject(hProc, INFINITE)<br> CloseHandle hProc<br> End If<br>End Sub<br><br>Thanks in advance,<br>Dan
Dan,<br><br>Look at the 5th post in this thread, it should do what you want. Just copy and paste it into VB.<br><br> <p>Steve<br><a href=mailto:tribesaddict@swbell.net>tribesaddict@swbell.net</a><br><a href= > </a><br>
Remember - the Shell function is for registered programs. if the program is not registered you may have to call the full path, such as: "C:\windows\notepad.exe"<br><br>My two cents. (Worth half a penny!)<br> <p>Cody ford<br><a href=mailto:codyford@yahoo.com>codyford@yahoo.com</a><br><a href= > </a><br>VB, VBS, Seagate Info/Crystal Reports<br>
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.