Link to home
Start Free TrialLog in
Avatar of patrickvisser
patrickvisser

asked on

Listview Execution off sellected Items

Hi,
I Have a listview and i want to execute files in it when a user bubbelclick the sellected item in it. Is there a easy code for it..

Thanks, Folks,,,
Avatar of patrickvisser
patrickvisser

ASKER

Need it Badley
what's mean? open a file with default application ( txt with notepad, htm with IE, etc.)?
Yes Yes, thats is. Just like the explorer does with files. When you dubbelklick a *.txt file the the notpad starts and when a *.doc is dubbelclk then word is starting..
Avatar of Éric Moreau
Use this:

Option Explicit

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 Const SW_SHOWNORMAL = 1
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&
Private Const SE_ERR_ACCESSDENIED = 5
Private Const SE_ERR_ASSOCINCOMPLETE = 27
Private Const SE_ERR_DDEBUSY = 30
Private Const SE_ERR_DDEFAIL = 29
Private Const SE_ERR_DDETIMEOUT = 28
Private Const SE_ERR_DLLNOTFOUND = 32
Private Const SE_ERR_FNF = 2
Private Const SE_ERR_NOASSOC = 31
Private Const SE_ERR_OOM = 8
Private Const SE_ERR_PNF = 3
Private Const SE_ERR_SHARE = 26

Private Sub Command1_Click()
Dim lngResult As Long

    lngResult = ShellExecute(Me.hwnd, "open", List1.List(list1.listindex), vbNullString, "", SW_SHOWNORMAL)

    'If the function fails, the return value is an error value that is less than or equal to 32.
    If lngResult >= 33 Then Exit Sub
   
    Select Case lngResult
        Case 0
            MsgBox "The operating system is out of memory or resources."
        Case ERROR_FILE_NOT_FOUND
            MsgBox "Le document est introuvable."
        Case ERROR_PATH_NOT_FOUND
            MsgBox "Le chemin d'accès est introuvable."
        Case ERROR_BAD_FORMAT
            MsgBox "The .EXE file is invalid (non-Win32 .EXE or error in .EXE image)."
        Case SE_ERR_ACCESSDENIED
            MsgBox "The operating system denied access to the specified file."
        Case SE_ERR_ASSOCINCOMPLETE
            MsgBox "The filename association is incomplete or invalid."
        Case SE_ERR_DDEBUSY
            MsgBox "The DDE transaction could not be completed because other DDE transactions were being processed."
        Case SE_ERR_DDEFAIL
            MsgBox "The DDE transaction failed."
        Case SE_ERR_DDETIMEOUT
            MsgBox "The DDE transaction could not be completed because the request timed out."
        Case SE_ERR_DLLNOTFOUND
            MsgBox "The specified dynamic-link library was not found."
        Case SE_ERR_FNF
            MsgBox "The specified file was not found."
        Case SE_ERR_NOASSOC
            MsgBox "Ce type de document n'est pas ASSOCIÉ à une application dans Windows."
        Case SE_ERR_OOM
            MsgBox "There was not enough memory to complete the operation."
        Case SE_ERR_PNF
            MsgBox "The specified path was not found."
        Case SE_ERR_SHARE
            MsgBox "A sharing violation occurred."
    End Select
End Sub
??
Case SE_ERR_NOASSOC
    MsgBox "Ce type de document n'est pas ASSOCIÉ à une application dans Windows."
Use the Shell function and pass it the Text property (or Tag property, whichever you're using to store the filename). Keep in mind this won't work with non-executable files, and your VB program will have no way of knowing when the called program finishes execution.

chrisblevins changed the proposed answer to a comment
'the function ShellOpen will use defautl application open a file, if not success will show "Open With..." dialog

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 GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Sub ShellOpen(strFile As String)
    Dim lngRet As Long
    Dim strDir As String
    lngRet = ShellExecute(GetDesktopWindow, "open", strFile, vbNullString, vbNullString, vbNormalFocus)
    If lngRet < 32 Then
        ' no association exists
        strDir = Space(260)
        lngRet = GetSystemDirectory(strDir, Len(strDir))
        strDir = Left(strDir, lngRet)
        ' show the Open with dialog box
        Call ShellExecute(GetDesktopWindow, vbNullString, "RUNDLL32.EXE", "shell32.dll,OpenAs_RunDLL " & strFile, strDir, vbNormalFocus)
    End If
End Sub
Erick37,

"Ce type de document n'est pas ASSOCIÉ à une application dans Windows."

Means: File type is not associated.
Please Engles No FRNS..
As everyboy has said use
Use the following:
(uses a textbox for filename, with path, and a command button to start the file)

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()
Call ShellExecute(0&, vbNullString, Text1.Text, vbNullString, _
vbNullString, vbNormalFocus)
End Sub
 
That's it patrickvisser - I have to agree.  All 3 of them work (some even in French).
Whel the Code from emoreau works just fine. and i can finnnish my work..
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial