Launch file and associated program

By | 2002-06-01

How do I launch a file in its associated program?
The Shell statement unfortunately only supports launching an EXE file directly. If you want to be able to launch, i.e. Microsoft Word by calling a .DOC file only, you can make your VB application launch the associated program with the document using the following method:

Original Author: VB FAQ

API Declarations



#IF WIN32 THEN
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 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
Private Const SW_SHOWNORMAL = 1

Code

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_Click()
?á?áDim r As Long
?á?ár = StartDoc("c:my documentswordmyletter.doc")
?á?áDebug.Print "Return code from Startdoc: "; r
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.