"M!" <morten.vinther@sol.dk> wrote in message
news:3df85e63.12652693@news.tele.dk...
>
> Hej,
>
> Jeg har en bat-fil som jeg gerne vil kalde fra VBA.
>
> Mit mål er at kalde bat-filen, vente til denne er færdig hvorefter jeg
> kan teste på returkoden fra denne.
>
> Hvordan er det liiiiige man gør det?!?
Sådan her:
Funktionkald:
Result = Shell(tmpFile, vbMinimizedNoFocus))
ExitCode = WaitForProcess(Result)
If ExitCode Then
'Der er sket en fejl
End If
Tilhørende Sub:
Attribute VB_Name = "modWait"
Option Explicit
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As
Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As
Long, lpExitCode As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As
Long, ByVal dwMilliseconds As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const INFINITE = &HFFFF
Private Const WAIT_FAILED& = -1&
Private Const STILL_ACTIVE = &H103
'
Function WaitForProcess(ByVal idProc As Long, Optional ByVal Sleep As
Boolean) As Long
Dim iExitCode As Long, hProc As Long
' Get process handle
hProc = OpenProcess(PROCESS_ALL_ACCESS, False, idProc)
If Sleep Then
' Sleep until process finishes
Dim iResult As Long
iResult = WaitForSingleObject(hProc, INFINITE)
If iResult = WAIT_FAILED Then Err.Raise Err.LastDllError
' Save the return code
GetExitCodeProcess hProc, iExitCode
Else
' Get the return code
GetExitCodeProcess hProc, iExitCode
' Wait for process but relinquish time slice
Do While iExitCode = STILL_ACTIVE
DoEvents
GetExitCodeProcess hProc, iExitCode
Loop
End If
CloseHandle hProc
WaitForProcess = iExitCode ' Return exit code
End Function
mvh
///JJ