Solved

Using FindExecutable

Posted on 2000-03-20
5
610 Views
Last Modified: 2008-03-03
I'm using the function FindExecutable to determine the associated application of a file.
But sometimes FindExecutable truncates the result after the first space if the associated application is in a directory that has a long file name.

I've read the article in the Knowledge Base that you have to replace the NULL Characters in the file name by spaces.
But it doesn't seem to work. I still only get the file name up to the first space.
0
Comment
Question by:rainerHOLZ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Accepted Solution

by:
vbWayne earned 100 total points
ID: 2636726

'found this sometime back perhaps it will help

'find the executable of a given file


Private Declare Function FindExecutableA Lib "shell32.dll" _
(ByVal lpFile As String, ByVal lpdirectory As String, _
ByVal lpResult As String) As Long

Private Const MAX_FILENAME_LEN = 256

'  Finds the executable associated with a file
'
'  Returns "" if no file is found.
'
Public Function FindExecutable(s As String) As String
   Dim i As Integer
   Dim s2 As String
   
   s2 = String(MAX_FILENAME_LEN, 32) & Chr$(0)
   
   i = FindExecutableA(s & Chr$(0), vbNullString, s2)
   
   If i > 32 Then
      FindExecutable = Left$(s2, InStr(s2, Chr$(0)) - 1)
   Else
      FindExecutable = ""
   End If
   
End Function

Private Sub Command1_Click()
MsgBox FindExecutable("c:\my documents\try.txt")
End Sub
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2636765
Another solution would be to simply read the registry with the Registry API...
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2636898
This seems to work:

Private Sub Command1_Click()

    Dim success As Long
    Dim sResult As String
    Dim msg As String
    Dim i As Long

    sResult = Space$(MAX_PATH)
   
    success = FindExecutable("test.aaa", "c:\", sResult)

    Select Case success
    Case ERROR_FILE_NO_ASSOCIATION: msg = "no association"
    Case ERROR_FILE_NOT_FOUND: msg = "file not found"
    Case ERROR_PATH_NOT_FOUND: msg = "path not found"
    Case ERROR_BAD_FORMAT:     msg = "bad format"
    Case Is >= ERROR_FILE_SUCCESS:
        For i = 1 To Len(sResult)
            If Mid(sResult, i, 1) = Chr(0) Then
                Mid(sResult, i, 1) = " "
            End If
        Next
        msg = Trim(sResult)
   End Select
   MsgBox msg
End Sub
0
 
LVL 1

Expert Comment

by:pclement
ID: 2637087
Rather than rewrite the Registry code, I used the following functions in the code below from:

http://www.thescarms.com/VBasic/Registry.htm

Private Sub Command1_Click()

Dim sCommandLine As String

sCommandLine = GetProgCommandLine(".doc")

End Sub

Function GetProgCommandLine(sFileExt As String) As String

Dim lVal As Long
Dim sSetting As String
Dim sShellKey As String

'Get ProgID for file extension
lVal = fReadValue("HKCR", sFileExt, "", "S", "", sSetting)

If sSetting <> "" Then
   
    sShellKey = sSetting & "\Shell\Open\Command"
    lVal = fReadValue("HKCR", sShellKey, "", "S", "", sSetting)
    If sSetting <> "" Then
        GetProgCommandLine = sSetting
    Else
        'Shell command line not found
    End If
Else
    'file extension not found
End If

End Function
0
 

Author Comment

by:rainerHOLZ
ID: 2722294
Sorry that you had to wait so long time for your points but my mail server didn't work for several weeks.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question