×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Batch Files from Excel
3

Batch Files from Excel

Batch Files from Excel

(OP)
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

RE: Batch Files from Excel

Use following code:

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

instead of vbNormalFocus you can use different from list

Thanks,
Vlado

RE: Batch Files from Excel

(OP)
I new it was something like that, its that long since I used that command,

Thanks alot

Dave

RE: Batch Files from Excel

3
If you need to wait until the batch file is done before proceeding with your code, you can use this method.

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 "Error starting App:" & vbCrLf & _
           "App: " & sApp & vbCrLf & _
           "Err Desc: " & 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.

RE: Batch Files from Excel

(OP)
Excellent, I was just pondering over that problem.  That deserves a star.

cheers

Dave

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources