×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Opening a file and its associated application from VB

Opening a file and its associated application from VB

Opening a file and its associated application from VB

(OP)
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?

RE: Opening a file and its associated application from VB

'This example will show you how to launch a file with his associate program.
'It will do the same action that occur when you Double Click on the file.
'For Example, if when you double click on mp3 file it launch with Winamp,
'This code will do the same.
'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Insert this code to the module :

#If Win32 Then
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
Declare Function GetDesktopWindow Lib "user32" () As Long
#Else
Declare Function ShellExecute Lib "SHELL" (ByVal hwnd%, _
ByVal lpszOp$, ByVal lpszFile$, ByVal lpszParams$, _
ByVal lpszDir$, ByVal fsShowCmd%) As Integer
Declare Function GetDesktopWindow Lib "USER" () As Integer
#End If
Public Const SW_SHOWNORMAL = 1

'Insert this code to your form:

Function StartDoc(DocName As String) As Long
Dim Scr_hDC As Long
Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", DocName, "", "C:\", SW_SHOWNORMAL)
End Function

Private Sub Form_Load()
Dim r As Long
'Replace the c:\mp3\song.mp3 with the file you want to launch
r = StartDoc("c:\mp3\song.mp3")
End Sub


Eric

Eric De Decker
vbg.be@vbgroup.nl
Visual Basic Center

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close