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?
11 6528
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
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.
If SHELL is unsuccessful it returns a zero -
If Shell("C:\Program Files\Microsoft Office\Office\FRONTP.EXE", vbMaximizedFocus) = 0 Then
-
MsgBox "Failed to open MS Frontpage"
-
End If
-
-
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.
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).
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... - Attribute VB_Name = "Exec"
-
Option Explicit
-
DefLng A-Z
-
-
Private Type STARTUPINFO
-
cb As Long
-
lpReserved As String
-
lpDesktop As String
-
lpTitle As String
-
dwX As Long
-
dwY As Long
-
dwXSize As Long
-
dwYSize As Long
-
dwXCountChars As Long
-
dwYCountChars As Long
-
dwFillAttribute As Long
-
dwFlags As Long
-
wShowWindow As Integer
-
cbReserved2 As Integer
-
lpReserved2 As Long
-
hStdInput As Long
-
hStdOutput As Long
-
hStdError As Long
-
End Type
-
-
Private Type PROCESS_INFORMATION
-
-
hProcess As Long
-
hThread As Long
-
dwProcessID As Long
-
dwThreadID As Long
-
End Type
-
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
-
hHandle As Long, ByVal dwMilliseconds As Long) As Long
-
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
-
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
-
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
-
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
-
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
-
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
-
PROCESS_INFORMATION) As Long
-
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
-
hObject As Long) As Long
-
Private Const NORMAL_PRIORITY_CLASS = &H20&
-
Private Const INFINITE = -1&
-
-
Public Sub ExecCmd(cmdline$)
-
Dim proc As PROCESS_INFORMATION
-
Dim start As STARTUPINFO
-
Dim ret&
-
-
' Initialize the STARTUPINFO structure:
-
start.cb = Len(start)
-
-
' Start the shelled application:
-
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
-
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
-
-
' Wait for the shelled application to finish:
-
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
-
ret& = CloseHandle(proc.hProcess)
-
End Sub
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... - Attribute VB_Name = "Exec"
-
Option Explicit
-
DefLng A-Z
-
-
Private Type STARTUPINFO
-
cb As Long
-
lpReserved As String
-
lpDesktop As String
-
lpTitle As String
-
dwX As Long
-
dwY As Long
-
dwXSize As Long
-
dwYSize As Long
-
dwXCountChars As Long
-
dwYCountChars As Long
-
dwFillAttribute As Long
-
dwFlags As Long
-
wShowWindow As Integer
-
cbReserved2 As Integer
-
lpReserved2 As Long
-
hStdInput As Long
-
hStdOutput As Long
-
hStdError As Long
-
End Type
-
-
Private Type PROCESS_INFORMATION
-
-
hProcess As Long
-
hThread As Long
-
dwProcessID As Long
-
dwThreadID As Long
-
End Type
-
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
-
hHandle As Long, ByVal dwMilliseconds As Long) As Long
-
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
-
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
-
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
-
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
-
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
-
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
-
PROCESS_INFORMATION) As Long
-
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
-
hObject As Long) As Long
-
Private Const NORMAL_PRIORITY_CLASS = &H20&
-
Private Const INFINITE = -1&
-
-
Public Sub ExecCmd(cmdline$)
-
Dim proc As PROCESS_INFORMATION
-
Dim start As STARTUPINFO
-
Dim ret&
-
-
' Initialize the STARTUPINFO structure:
-
start.cb = Len(start)
-
-
' Start the shelled application:
-
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
-
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
-
-
' Wait for the shelled application to finish:
-
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
-
ret& = CloseHandle(proc.hProcess)
-
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
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
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.
I'm still wondering which file you're talking about.
Sorry, I am referring to the Excel file which contains the VB code.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |