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

Edited 5 Years Ago by mb01a: n/a

Comments
eligant piece of code
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

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

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.

Edited 4 Years Ago by mb01a: n/a

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

Edited 4 Years Ago by deades: n/a

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

Edited 4 Years Ago by deades: n/a

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.

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.

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

Edited 4 Years Ago by deades: n/a