Parsi Coders
Retrieve File Association and open a file in its native application - نسخه قابل چاپ

+- Parsi Coders (http://parsicoders.com)
+-- انجمن: Software Development Programming (http://parsicoders.com/forumdisplay.php?fid=37)
+--- انجمن: Visual Basic Programming (http://parsicoders.com/forumdisplay.php?fid=39)
+---- انجمن: Visual Basic 6 (http://parsicoders.com/forumdisplay.php?fid=44)
+---- موضوع: Retrieve File Association and open a file in its native application (/showthread.php?tid=2097)



Retrieve File Association and open a file in its native application - Amin_Mansouri - 04-15-2012

کد:
Private Declare Function FindExecutable Lib "shell32" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal sResult As String) As Long
    Private Const MAX_PATH As Long = 260
    Private Const ERROR_FILE_NO_ASSOCIATION As Long = 31
    Private Const ERROR_FILE_NOT_FOUND As Long = 2
    Private Const ERROR_PATH_NOT_FOUND As Long = 3
    Private Const ERROR_FILE_SUCCESS As Long = 32 'my constant
    Private Const ERROR_BAD_FORMAT As Long = 11
    Dim mflg As Boolean
    Public ActiveClientName As String
    Function retassoc(fn As String, fpath As String) As String
    ' retrieve the associated program that uses this file
    Dim success As Long
    Dim pos As Long
    Dim sResult As String
    sResult = Space$(MAX_PATH)
    success = FindExecutable(fn, fpath, sResult)
    pos = InStr(sResult, Chr$(0))
    If pos Then
    retassoc = Left$(sResult, pos - 1)
    End If
    End Function
    Function LoadUserFile(ByVal FN2Load As String)
    On Error GoTo load_err
    ' load the file in its' native application
    For x = Len(FN2Load) To 0 Step -1
    If Mid(FN2Load, x, 1) = "\" Then
    opPath = Left(FN2Load, x)
    opFile = Right(FN2Load, Len(FN2Load) - x)
    Exit For
    End If
    Next
    Call Shell(retassoc(opFile, opPath) & " " & Chr(34) & FN2Load & Chr(34), vbNormalFocus)
    Exit Function
    load_err:
    MsgBox "The file you are trying to open apparently has no program association available" & vbCrLf & "or it is a corrupted file. I can't open " & opFile, vbInformation + vbOKOnly, "Error occured on file open"
    On Error GoTo 0
    End Function