how do I execute or run an external program in Visual Basic?

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
Doesn't get much stupider than this. I am making a quick program that is turning out to be harder than I thought. I have not done any VB programming in about 4 years, so whatever I knoew flew out of my head. here is the deal:

7 check boxes.
2 buttons. one for login, one for exit.

each check box is for an external program that I want to open. (execute or run are probably better words) so when I click on the login button, it opens the external programs that are checked.

I know how to code for the checkbox: if checkbox1.value = 1 then (open the damn file). repeat 7 times under action = click for the login button.)

I just don't know the correct verbage to open the files.

For instance, one of these is a shortcut to Internet Explorer. (internet explorer.lnk) all I have to do in a batch file is "start internet explorer.lnk" and it will start. I have a batch file already made that will open all 7, but I would prefer they be selectable which is why I am writing this VB program.

If somoene could point me to a code library that would probably work too. This is for VB 5 prof. (the only VB I have licence for. )

Thanks!
 
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
If you just want to run external programs,you can use the Shell function.

Shell(pathname, windowstyle)

Where windowstyle is one of the following:
vbHide
vbNormalFocus
vbMinimizedFocus
vbMaximizedFocus
vbNormalNoFocus
vbMinimizedNoFocus



I think if you want to actually run various file types, you'll need an API call.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Const SW_SHOWNORMAL As Long = 1

Sub OpenAnyFile(filePath As String)
ShellExecute Me.hwnd, "Open", filePath, 0&, 0&, SW_SHOWNORMAL
End Sub

Paste that into your code somewhere. I usually create a separate .bas file to store these individual code snippets. I think you can just put it in your main form along with the rest of your code though.
Just call the OpenAnyFile(filepath) function to execute something.
 

bot2600

Platinum Member
May 18, 2001
2,075
0
76
actually, I think if you need to run an .exe file from within VB, you need to do something like this....

Shell ("command.com /c c:\mypath\myexe")

if the exe is in the same directory as your app currently is, then you can leave out the path...

Bot
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
Yeah. "pathname" is just the path to the file. If the file is in the current directory or a subdirectory, you can use a relative pathname.

More info
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
Originally posted by: igowerf

I think if you want to actually run various file types, you'll need an API call.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Const SW_SHOWNORMAL As Long = 1

Sub OpenAnyFile(filePath As String)
ShellExecute Me.hwnd, "Open", filePath, 0&, 0&, SW_SHOWNORMAL
End Sub

That code worked great! It automaticly sets the focus to the opened file (so I can sendkeys to it :) ). I tried the first "Shell(pathname, windowstyle)" that you listed, but if I put in one of the "vb*" calls you listed (which vb auto typesin for me as well) I get an error. I was able to do it by pasting in that code above.

THANKS!!