473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Shell Function

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
11 6528
albertw
267 100+
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
Killer42
8,435 Expert 8TB
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
willakawill
1,646 1GB
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
142 100+
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
Killer42
8,435 Expert 8TB
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
Killer42
8,435 Expert 8TB
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
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
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
Killer42
8,435 Expert 8TB
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
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
Killer42
8,435 Expert 8TB
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

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

Similar topics

3
by: John Bowling | last post by:
I'm creating a routine (not in a browser) to move multiple files on a daily basis to a backup directory. It can be done easily by call shell functions like 'mv file* newdir'. I can't find any...
2
by: Jorgen Grahn | last post by:
I couldn't think of a good solution, and it's hard to Google for... I write python command-line programs under Win2k, and I use the bash shell from Cygwin. I cannot use Cygwin's python package...
4
by: Yann.K | last post by:
Hello. Using Tkinter, i would create a widget which display a shell command return. This return is long, and i would display a real time display (like with the tail -f commande on Linux) I...
8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
6
by: Lauren Wilson | last post by:
Hi folks, In an A2K app, I have attempted to use the following command in some VBA code with IDENTICAL results with every single version of the following: Shell "outlook.exe", vbHide Shell...
8
by: Mike | last post by:
Am trying to open a Microsoft Word .doc file using Access 2000 with Shell function (on Windows XP Operating system) Here is the code : Shell "C:\Program Files\Microsoft...
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
25
by: dennijr | last post by:
ok, shell always used to be easy for me, now its starting to get annoying cause i dont know wats wrong heres the simplist code possible: Private Sub IExplorer_Click() a = Shell("C:\Program...
3
by: ppuniversal | last post by:
Hi everyone, I am making using Shell() function to run a command line tool from my VB Application. I am using it to archive a folder. So I am using the code : Shell("command line argument as...
3
by: Max Vit | last post by:
I have come across a strange issue whilst trying to use a shell command call from Access and have spent some time trying to figure this out but can't find the cause as yet. The issue is: I need...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.