-
I use this string to open other files:
Dim x
x = Shell(app.Path & "\File.Exe")
what if i want to put a file in the folder of my main program and have the main program run the file but not do anything if the file isnt there.
because with this string the file has to be there or i get an illegal function call.
Any ideas?
-
Is that the "This program has performed an illegal operation" error?
-
When using Shell it will execute the file in the current directory unless specified. As far as I know, so don't add the path to the filename you execute and you should be fine... See code below
Private Sub CmdRun_Click()
Dim AppName as String
Dim AppProcID as Integer
AppName = "MyApp.exe"
On Error Goto ErrRoutine
AppProcID = Shell(AppName)
Exit Sub
ErrRoutine:
'exe wasn't found or some internal error occured
'you can either, log it to a file, message box it or
'ignore it. if you run this code from the VB IDE without
'making this app an .exe Shell command will assume MyApp.exe
'is in the current dir for VB. example: c:\program files\DevStudio\VisualBasic\MyApp.exe
End Sub
-
If my program runs a file thats not there it will give me an illegal function call
whats the code i can use in case this happens?
-
You need to verify the .exe file is there in the first place
So, create a function like this to see if a file exists.
Code:
Public Function Exists(ByVal file_ as String) As Boolean
On Error Goto NO_FILE
'' try to get the length of the file
'' if errors occur we know the file Doesn't Exist
Call FileLen(file_)
Exists = True
Exit Function
NO_FILE:
Exists = False
Exit Function
'' Test Driver Sub
Public Sub RunFile()
Dim x as Long
'' if the file exists shell it!
If Exists(App.Path & "\File.exe") Then
x = Shell(App.Path & "\File.exe")
End if
End Sub
there are other ways of doing this but this one works. ;)