Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a file and its associated application from VB

Status
Not open for further replies.

Denk

Programmer
Jan 4, 2000
22
0
0
GB
I need a quick and easy way to open a specific file from VB which also opens the files associated application at the same time. Can anyone help?
 
'This example will show you how to launch a file with his associate program.<br>
'It will do the same action that occur when you Double Click on the file.<br>
'For Example, if when you double click on mp3 file it launch with Winamp,<br>
'This code will do the same.<br>
'Add a module to your project (In the menu choose Project -&gt; Add Module, Then click Open)<br>
'Insert this code to the module :<br>
<br>
#If Win32 Then<br>
Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteA&quot; _<br>
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile _<br>
As String, ByVal lpParameters As String, ByVal lpDirectory As String, _<br>
ByVal nShowCmd As Long) As Long<br>
Declare Function GetDesktopWindow Lib &quot;user32&quot; () As Long<br>
#Else<br>
Declare Function ShellExecute Lib &quot;SHELL&quot; (ByVal hwnd%, _<br>
ByVal lpszOp$, ByVal lpszFile$, ByVal lpszParams$, _<br>
ByVal lpszDir$, ByVal fsShowCmd%) As Integer<br>
Declare Function GetDesktopWindow Lib &quot;USER&quot; () As Integer<br>
#End If<br>
Public Const SW_SHOWNORMAL = 1<br>
<br>
'Insert this code to your form:<br>
<br>
Function StartDoc(DocName As String) As Long<br>
Dim Scr_hDC As Long<br>
Scr_hDC = GetDesktopWindow()<br>
StartDoc = ShellExecute(Scr_hDC, &quot;Open&quot;, DocName, &quot;&quot;, &quot;C:\&quot;, SW_SHOWNORMAL)<br>
End Function<br>
<br>
Private Sub Form_Load()<br>
Dim r As Long<br>
'Replace the c:\mp3\song.mp3 with the file you want to launch<br>
r = StartDoc(&quot;c:\mp3\song.mp3&quot;)<br>
End Sub<br>
<br>
<br>
Eric <p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Visual Basic Center</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top