Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch Files from Excel 3

Status
Not open for further replies.

gb433054

Aerospace
Joined
Jul 24, 2003
Messages
28
Location
GB
Does anyone know if it is possible to run a batch file from an excel macro? I've tried using

Application.Run "C:\temp\MyBatchFile.bat"

But that didnt work

anyone got any ideas
 
Use following code:

Shell "C:\temp\MyBatchFile.bat",vbNormalFocus

instead of vbNormalFocus you can use different from list

Thanks,
Vlado
 
I new it was something like that, its that long since I used that command,

Thanks alot

Dave
 
If you need to wait until the batch file is done before proceeding with your code, you can use this method.
Code:
Option Explicit

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&

Sub Main()
    Dim sPrgm As String
    
    sPrgm = "C:\Winnt\notepad.exe"
    
    RunUntilFinished sPrgm
    
    MsgBox "App is finished!"

    End
End Sub

Public Sub RunUntilFinished(ByVal sApp As String)
    Dim lProcID As Long
    Dim hProc As Long

    ' Start the App
    On Error GoTo ErrHndlr
    lProcID = Shell(sApp, vbNormalFocus)
    On Error GoTo 0

    DoEvents

    ' Wait for the App
    hProc = OpenProcess(SYNCHRONIZE, 0, lProcID)
    If hProc <> 0 Then
        WaitForSingleObject hProc, INFINITE
        CloseHandle hProc
    End If
    Exit Sub

ErrHndlr:
    MsgBox &quot;Error starting App:&quot; & vbCrLf & _
           &quot;App: &quot; & sApp & vbCrLf & _
           &quot;Err Desc: &quot; & Err.Description
    Err.Clear
End Sub

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Excellent, I was just pondering over that problem. That deserves a star.

cheers

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top