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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I set the working directory of a program I want to execute from VBA?

Executing Files

How do I set the working directory of a program I want to execute from VBA?

by  dds82  Posted    (Edited  )
You use Windows' built-in ShellExecute() API function. Just copy and paste this declaration into a module:

Code:
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, _
     ByVal lpOperation As String, _
     ByVal lpFile As String, _
     ByVal lpParameters As String, _
     ByVal lpDirectory As String, _
     ByVal nShowCmd As Integer) As Long

To execute c:\foo\foo.exe, you would say:

Code:
'5 is SW_SHOW, to show the window
ShellExecute(0&,"open","c:\foo\foo.exe",vbNullString,"C:\foo",5)

If the program takes command-line arguments, you can pass them in using the fourth parameter (instead of vbNullString).
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top