VB: Open with default program

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply

Is this helpful? (Answer truthfully)

Definately Yes
1
50%
Yes
0
No votes
Sure
0
No votes
Maybe
0
No votes
Nah
0
No votes
No
0
No votes
Definately No
1
50%
 
Total votes: 2

HaloKiller





Posts: 59
Joined: Sat Jul 03, 2004 2:35 am
Location: Hmmmmmm... That's a toughie... What is "AT MY COMPUTER"?
Contact:

VB: Open with default program

Post by HaloKiller »

Ok. This tutorial will teach you how to open an item with the default program, for instance this could be used for if you you dont know what program they use to open a file. They might have a microsoft Word document but open it with StarOffice.


1. So, first lets make a new app.
a. Open up Visual BASIC (I use 6 here)
b. Goto File->New Project
c. Click Standard EXE


2. Add a command button
a. Goto the toolbox and click command button
b. Drag a command button out onto the screen


3. Add code to the button
a. Double click on the button. The code window will appear
you will see

Code: Select all

Private Sub Command1_Click()

End Sub
b. Above that add

Code: Select all

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
Dim file As String
You should now have

Code: Select all

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
____________________________________________________________________________________________________________________________


Private Sub Command1_Click()

End Sub
c. In the code perimeter for the button (Private Sub Command1_Click()) type

Code: Select all

file = ".\file.extension"
    ShellExecute 0, vbNullString, file, vbNullString, vbNullString, vbNormalFocus
where file is the filename and extension is the file's extension (eg .exe, .txt, etc)
You should now have

Code: Select all

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
____________________________________________________________________________________________________________________________


Private Sub cmd1_Click()
    file = ".\file.extension"
    ShellExecute 0, vbNullString, file, vbNullString, vbNullString, vbNormalFocus
End Sub

4. Build your application
a. Goto File->Make (project name).exe


5. Test
a. Put a file with the same name and extension in the directory with your built app. If you followed directly, it will open with it's default program!



This can also be used for websites and email as seen in the download.
(just give me a minute)




Have fun and feel free to use this code anytime you want.



DOWNLOAD


~HaloKiller
Post Reply