Open Any File

mdmilner

Well-known Member
Joined
Apr 30, 2003
Messages
1,362
Subject says it all.
Any suggestions on the most versatile way to open any file?

I'm rewriting a function for versatility and while I currently know precisely what file type I'm opening, I'd have thought there'd be a very simple way to open anything without needing to specify the exe that opens it.

I was thinking about Shell & URLs as two options.
or
Declare Function OpenFile Lib "Kernel" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Integer) As Integer

Mike
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi mdmilner

Here is the complete code I used to create a file opening application. It will open any file that has a file extention reference in the system. Just ignore all the stuff concerning controls etc. and you will see how the shellexecute API works.

Code:
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()
    End
End Sub

Private Sub Command2_Click()
    FindProg
    File1.Refresh
End Sub

Private Sub Dir1_Change()
    File1.Path = Dir1.Path
End Sub

Private Sub Drive1_Change()
    On Error GoTo Err_Handler
    Dir1.Path = Drive1.Drive
    
    
Err_Handler:
    If Err.Number = 68 Then
    MsgBox " There is no disk in the selected drive," & vbLf & " or the drive not available.", vbOKOnly + vbCritical, "Error Number : " & Err.Number
    End If
    Exit Sub
End Sub

Private Sub FindProg()
    Dim strFileName, strPath As String
    Dim vara, progName
    Dim inta As Integer
    
    strFileName = File1.FileName
    If File1.ListCount = 0 Or strFileName = "" Then
    MsgBox "There is no file selected", vbOKOnly + vbExclamation, "Amatole WIS"
    Else
    'strFileName = File1.FileName
    strPath = File1.Path
    inta = MsgBox("Do you wish to open the file named '" & strPath & "\" & strFileName & "'?", vbYesNo + vbQuestion, "GIS!")
    If inta = 6 Then
    vara = ShellExecute(0, vbNullString, strFileName, vbNullString, strPath, 1) ' vbNormalFocus)
     Else
    Exit Sub
    End If
    End If
    
    
End Sub

Private Sub File1_DblClick()
    FindProg
    File1.Refresh
End Sub

Private Sub Form_Load()
       Me.File1.Pattern = Me.Text1.Text
End Sub

Private Sub Form_Resize()
        Exit Sub
End Sub

Private Sub Text1_Change()
    Me.File1.Pattern = Me.Text1.Text
End Sub

Hop this helps

anvil19
:eek:
 
Upvote 0

Forum statistics

Threads
1,221,700
Messages
6,161,371
Members
451,700
Latest member
Eccymarge

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top