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

Shell Function

P: 3
I am using the SHELL function in a Visual Basic Project to launch program files, such as Word, Excel, etc. I am using the function in the following way:

Shell "C:\Program Files\Microsoft Office\Office\FRONTP.EXE", vbMaximizedFocus

If the specified file does not exist there is no indication of such. Is there some method of error handling that I can use to alert the user via a msgbox or something that the file does not exist?
Nov 7 '06 #1
Share this Question
Share on Google+
11 Replies


100+
P: 267
I am using the SHELL function in a Visual Basic Project to launch program files, such as Word, Excel, etc. I am using the function in the following way:

Shell "C:\Program Files\Microsoft Office\Office\FRONTP.EXE", vbMaximizedFocus

If the specified file does not exist there is no indication of such. Is there some method of error handling that I can use to alert the user via a msgbox or something that the file does not exist?
hi

If Not DIR("C:\Program Files\Microsoft Office\Office\FRONTP.EXE",0)=vbNullString then
' file exist, you may proceed
Nov 7 '06 #2

Expert 5K+
P: 8,434
If Not DIR("C:\Program Files\Microsoft Office\Office\FRONTP.EXE",0)=vbNullString then
' file exist, you may proceed
Just a note - if you're using VB6 then I believe the Shell function is more or less obsolete. I don't recall exactly how it's supposed to work now, but give me a few hours (just started work) and I'll track it down during my lunch break.
Nov 8 '06 #3

100+
P: 1,646
If SHELL is unsuccessful it returns a zero
Expand|Select|Wrap|Line Numbers
  1. If Shell("C:\Program Files\Microsoft Office\Office\FRONTP.EXE", vbMaximizedFocus) = 0 Then
  2.    MsgBox "Failed to open MS Frontpage"
  3. End If
  4.  
  5.  
Nov 8 '06 #4

sivadhas2006
100+
P: 142
yes, that is correct.

If the shell is executed successfully it returns the task id of the executed program.

Otherwise it returns zero.

Regards,
M.Sivadhas.
Nov 9 '06 #5

Expert 5K+
P: 8,434
yes, that is correct.

If the shell is executed successfully it returns the task id of the executed program.

Otherwise it returns zero.

Regards,
M.Sivadhas.
I haven't yet found the newer method of starting a program, but if you're interested here's an old demo form from VB3 or thereabouts, which shows how to start a program and wait for it to finish. If interested, just paste it into a text file, then rename/save it with a .FRM extension and load it into VB. (No guarantees, I haven't tried it in a recent version).
Expand|Select|Wrap|Line Numbers
  1. VERSION 2.00
  2. Begin Form ExecDemo 
  3.    Caption         =   "Choose Program to Execute"
  4.    Height          =   3765
  5.    Left            =   1035
  6.    LinkMode        =   1  'Source
  7.    LinkTopic       =   "Form1"
  8.    ScaleHeight     =   3360
  9.    ScaleWidth      =   6480
  10.    Top             =   1140
  11.    Width           =   6600
  12.    Begin PictureBox Picture1 
  13.       Height          =   615
  14.       Left            =   5280
  15.       ScaleHeight     =   585
  16.       ScaleWidth      =   585
  17.       TabIndex        =   4
  18.       Top             =   1680
  19.       Width           =   615
  20.    End
  21.    Begin CommandButton CmdRun 
  22.       Caption         =   "Run"
  23.       Height          =   495
  24.       Left            =   5040
  25.       TabIndex        =   3
  26.       Top             =   720
  27.       Width           =   1095
  28.    End
  29.    Begin FileListBox File1 
  30.       Height          =   2175
  31.       Left            =   2760
  32.       Pattern         =   "*.exe"
  33.       TabIndex        =   2
  34.       Top             =   720
  35.       Width           =   2055
  36.    End
  37.    Begin DirListBox Dir1 
  38.       Height          =   2175
  39.       Left            =   240
  40.       TabIndex        =   1
  41.       Top             =   720
  42.       Width           =   2295
  43.    End
  44.    Begin DriveListBox Drive1 
  45.       Height          =   315
  46.       Left            =   240
  47.       TabIndex        =   0
  48.       Top             =   240
  49.       Width           =   2295
  50.    End
  51. End
  52. '
  53. ' Run the application, minimize this application until
  54. '   the task is completed.
  55. '
  56. Sub CmdRun_Click ()
  57.     Dim hinstance%
  58.     CmdRun.Enabled = 0
  59.     ExecDemo.WindowState = 1
  60.  
  61.     hinstance% = Shell(GetExecName$(), 1)
  62.     Do
  63.         di% = DoEvents()
  64.     Loop While GetModuleUsage%(hinstance%) <> 0
  65.  
  66.     CmdRun.Enabled = -1
  67.     ExecDemo.WindowState = 0
  68.  
  69. End Sub
  70.  
  71. Sub Dir1_Change ()
  72.     File1.Path = Dir1.Path
  73.     File1.SetFocus
  74. End Sub
  75.  
  76. Sub Drive1_Change ()
  77.     Dir1.Path = Drive1.Drive
  78. End Sub
  79.  
  80. '
  81. '   When you click on the file box, the first icon in the
  82. '   specfied executable file is displayed in the picture1
  83. '   control.
  84. '
  85. Sub File1_Click ()
  86.     Dim hicon%
  87.     Dim execname$
  88.  
  89.     execname$ = GetExecName$()
  90.     hicon% = ExtractIcon(agGetInstance%(), execname$, 0)
  91.  
  92.     ' Draw the first icon for the executable file into
  93.     ' the picture1 control.
  94.     Picture1.Cls
  95.  
  96.     If (hicon%) Then
  97.         di% = DrawIcon%(Picture1.hDC, 0, 0, hicon%)
  98.         End If
  99.  
  100. End Sub
  101.  
  102. Sub File1_DblClick ()
  103.     CmdRun.Value = -1   ' Trigger the 'run' button
  104.  
  105. End Sub
  106.  
  107. '
  108. ' Retrieves the executable file name string
  109. '
  110. Function GetExecName$ ()
  111.     GetExecName$ = File1.Path + "\" + File1.FileName
  112. End Function
  113.  
  114.  
Nov 9 '06 #6

Expert 5K+
P: 8,434
Oops! Found it, a few minutes later. This one is a module. To be honest, I don't recall why I had to change over to using the CreateProcess function rather than Shell. I think it may have had something to do with starting shortcuts, or 32 bits applications, or something that didn't work with Shell. Anyway, if you do encounter any problems with Shell, keep this in mind...
Expand|Select|Wrap|Line Numbers
  1. Attribute VB_Name = "Exec"
  2. Option Explicit
  3. DefLng A-Z
  4.  
  5. Private Type STARTUPINFO
  6.   cb As Long
  7.   lpReserved As String
  8.   lpDesktop As String
  9.   lpTitle As String
  10.   dwX As Long
  11.   dwY As Long
  12.   dwXSize As Long
  13.   dwYSize As Long
  14.   dwXCountChars As Long
  15.   dwYCountChars As Long
  16.   dwFillAttribute As Long
  17.   dwFlags As Long
  18.   wShowWindow As Integer
  19.   cbReserved2 As Integer
  20.   lpReserved2 As Long
  21.   hStdInput As Long
  22.   hStdOutput As Long
  23.   hStdError As Long
  24. End Type
  25.  
  26. Private Type PROCESS_INFORMATION
  27.  
  28.       hProcess As Long
  29.       hThread As Long
  30.       dwProcessID As Long
  31.       dwThreadID As Long
  32. End Type
  33. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
  34.       hHandle As Long, ByVal dwMilliseconds As Long) As Long
  35. Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
  36.       lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
  37.       lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
  38.       ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
  39.       ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
  40.       lpStartupInfo As STARTUPINFO, lpProcessInformation As _
  41.       PROCESS_INFORMATION) As Long
  42. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
  43.       hObject As Long) As Long
  44. Private Const NORMAL_PRIORITY_CLASS = &H20&
  45. Private Const INFINITE = -1&
  46.  
  47. Public Sub ExecCmd(cmdline$)
  48.   Dim proc As PROCESS_INFORMATION
  49.   Dim start As STARTUPINFO
  50.   Dim ret&
  51.  
  52.   ' Initialize the STARTUPINFO structure:
  53.   start.cb = Len(start)
  54.  
  55.   ' Start the shelled application:
  56.   ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
  57.      NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
  58.  
  59.   ' Wait for the shelled application to finish:
  60.   ret& = WaitForSingleObject(proc.hProcess, INFINITE)
  61.   ret& = CloseHandle(proc.hProcess)
  62. End Sub
Nov 9 '06 #7

P: 3
Oops! Found it, a few minutes later. This one is a module. To be honest, I don't recall why I had to change over to using the CreateProcess function rather than Shell. I think it may have had something to do with starting shortcuts, or 32 bits applications, or something that didn't work with Shell. Anyway, if you do encounter any problems with Shell, keep this in mind...
Expand|Select|Wrap|Line Numbers
  1. Attribute VB_Name = "Exec"
  2. Option Explicit
  3. DefLng A-Z
  4.  
  5. Private Type STARTUPINFO
  6.   cb As Long
  7.   lpReserved As String
  8.   lpDesktop As String
  9.   lpTitle As String
  10.   dwX As Long
  11.   dwY As Long
  12.   dwXSize As Long
  13.   dwYSize As Long
  14.   dwXCountChars As Long
  15.   dwYCountChars As Long
  16.   dwFillAttribute As Long
  17.   dwFlags As Long
  18.   wShowWindow As Integer
  19.   cbReserved2 As Integer
  20.   lpReserved2 As Long
  21.   hStdInput As Long
  22.   hStdOutput As Long
  23.   hStdError As Long
  24. End Type
  25.  
  26. Private Type PROCESS_INFORMATION
  27.  
  28.       hProcess As Long
  29.       hThread As Long
  30.       dwProcessID As Long
  31.       dwThreadID As Long
  32. End Type
  33. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
  34.       hHandle As Long, ByVal dwMilliseconds As Long) As Long
  35. Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
  36.       lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
  37.       lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
  38.       ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
  39.       ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
  40.       lpStartupInfo As STARTUPINFO, lpProcessInformation As _
  41.       PROCESS_INFORMATION) As Long
  42. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
  43.       hObject As Long) As Long
  44. Private Const NORMAL_PRIORITY_CLASS = &H20&
  45. Private Const INFINITE = -1&
  46.  
  47. Public Sub ExecCmd(cmdline$)
  48.   Dim proc As PROCESS_INFORMATION
  49.   Dim start As STARTUPINFO
  50.   Dim ret&
  51.  
  52.   ' Initialize the STARTUPINFO structure:
  53.   start.cb = Len(start)
  54.  
  55.   ' Start the shelled application:
  56.   ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
  57.      NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
  58.  
  59.   ' Wait for the shelled application to finish:
  60.   ret& = WaitForSingleObject(proc.hProcess, INFINITE)
  61.   ret& = CloseHandle(proc.hProcess)
  62. End Sub

I used the code provided above in an application that I am developing, and it worked great. However, when I copy and paste the file to a new location, VB no longer waits for the exe to complete. Does anyone know why this might be happening?

Thanks,
J Schaeffer
Jan 11 '07 #8

P: 3
I used the code provided above in an application that I am developing, and it worked great. However, when I copy and paste the file to a new location, VB no longer waits for the exe to complete. Does anyone know why this might be happening?

Thanks,
J Schaeffer

I just realized that the problem goes away if I save the file in the new location instead of copying and pasting it to the new location. So I do not know why copy/paste does not work, but it doesn't matter.

J Schaeffer
Jan 11 '07 #9

Expert 5K+
P: 8,434
I just realized that the problem goes away if I save the file in the new location instead of copying and pasting it to the new location. So I do not know why copy/paste does not work, but it doesn't matter.
I'm still wondering which file you're talking about.
Jan 11 '07 #10

P: 3
I'm still wondering which file you're talking about.

Sorry, I am referring to the Excel file which contains the VB code.
Jan 16 '07 #11

Expert 5K+
P: 8,434
Sorry, I am referring to the Excel file which contains the VB code.
Oh well, as long as you've got it working, I suppose that's what counts.
Jan 18 '07 #12

Post your reply

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