Retrieve File Association and open a file in its native application

mb01a 1 Tallied Votes 1K Views Share

Have you ever needed to open a file with your VB program?

Here's a way to easily do it. (code copied from an original program written by Mike B)

If the program that uses the file is installed on your workstation, the file will be opened in that application.

I use this a lot in many programs.

:)
Mike

ChrisPadgham commented: eligant piece of code +5
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
hkdani 39 Posting Pro in Training

Function retassoc(fn As String, fpath As String) As String You might want to include ByVal in front of your variable parameters. By default, if you leave ByVal or ByRef out of the parameter declaration, the function will be looking for a variable. Sometimes functions intend to change the variables that are passed as parameters. If that's not your intention, then you might want to consider adding ByVal. That would force the user to enter a value, a string, integer, etc. for the parameter. Otherwise, the user has to declare a variable to pass to the function. Function retassoc(ByVal fn As String, ByVal fpath As String) As String

mb01a 18 Light Poster

I'm not going to change things just to be changing them. It works fine as-is. If someone else wants to change it to suit themselves, please do.

If you'll notice, the function -

retassoc(fn As String, fpath As String) As String

is asking for strings. If you pass strings to the function it will accept them as strings and go find the file association. If you pass something other than strings to the function .. it won't work. Please notice also that the function returns a string.

ChrisPadgham 113 Posting Whiz

Nice piece of code, I could have used this a number of times, and will from now on.

mb01a 18 Light Poster

I'm happy I could help.

deades 0 Newbie Poster

Very nice piece of code. Im having trouble receiving the file association however. I keep hitting the error trap on every type of file im attempting to open. What exactly does the 'success' variable in the code do? retassoc ends up being an empty string on each attempt. An exampple of the string im sending to LoadUserFile (FN2Load) is "o:\P1263_1.txt". This file exists and is able to be opening through explorer as is all the other files ive attemted to open.

Any help is appreciated. Thank you.

Your code unchanged:

Function retassoc(ByVal fn As String, ByVal 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
deades 0 Newbie Poster

Thought id create project with just the code from your original post. Came up with same result.

ChrisPadgham 113 Posting Whiz

the windows function is returning a C string. These strings indicate the end of the string with a ASC 0. The instr is finding the end of the string and retassoc is then set to the string to the left of it.

deades 0 Newbie Poster

Thanks for the info Chris. However I still cannot get this code to return the application association for all the files I attempt to open. All these files exist and are able to be opening through explorer. 'retassoc' ends up being an empty string on each attempt.

deades 0 Newbie Poster

Got it! My fault, code was working as it should. The path to my files was incorrect.

However, i cannot open jpg or bmp files...i can open gif's. Ive tried word, excel, pdf(adobe), text files, and they all open without a problem also. just cant open jpeg's and bmp's

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.