By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,988 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,988 IT Pros & Developers. It's quick & easy.

run executable file through code in vb

P: 1
how can i run executable file through code in vb ?
Nov 9 '06 #1
Share this Question
Share on Google+
1 Reply


sashi
Expert 100+
P: 1,754
how can i run executable file through code in vb ?
Hi there,

Kindly refer to below code segment, hope it helps. Good luck & take care.

Declarations
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Declare Function ShellExecute Lib "shell32.dll" _
  4. Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
  5. String, ByVal lpFile As String, ByVal lpParameters As String, _
  6. ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  7.  
Code
Expand|Select|Wrap|Line Numbers
  1. Function Shell(Program As String, Optional ShowCmd As Long = _
  2. vbNormalNoFocus, Optional ByVal WorkDir As Variant) As Long
  3.  
  4.     Dim FirstSpace As Integer, Slash As Integer
  5.  
  6.     If Left(Program, 1) = """" Then
  7.         FirstSpace = InStr(2, Program, """")
  8.  
  9.  
  10.         If FirstSpace <> 0 Then
  11.             Program = Mid(Program, 2, FirstSpace - 2) & _
  12.               Mid(Program, FirstSpace + 1)
  13.             FirstSpace = FirstSpace - 1
  14.         End If
  15.  
  16.     Else
  17.         FirstSpace = InStr(Program, " ")
  18.     End If
  19.  
  20.     If FirstSpace = 0 Then FirstSpace = Len(Program) + 1
  21.  
  22.     If IsMissing(WorkDir) Then
  23.  
  24.         For Slash = FirstSpace - 1 To 1 Step -1
  25.             If Mid(Program, Slash, 1) = "\" Then Exit For
  26.         Next
  27.  
  28.         If Slash = 0 Then
  29.             WorkDir = CurDir
  30.         ElseIf Slash = 1 Or Mid(Program, Slash - 1, 1) = ":" Then
  31.             WorkDir = Left(Program, Slash)
  32.         Else
  33.             WorkDir = Left(Program, Slash - 1)
  34.         End If
  35.  
  36.     End If
  37.  
  38.     Shell = ShellExecute(0, vbNullString, _
  39.     Left(Program, FirstSpace - 1), LTrim(Mid(Program, _
  40.     FirstSpace)), WorkDir, ShowCmd)
  41.     If Shell < 32 Then VBA.Shell Program, ShowCmd 'To raise Error
  42.  
  43. End Function
  44.  
Nov 9 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.