Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open selected file using file dialog

Status
Not open for further replies.

ICCIIT

Technical User
Jul 21, 2003
62
GB
Access 2010

I am wishing to allow a user to navigate and select a file to open from within a form command button, the filedialog code below works fine but I cannot get the selected file to open! - all that happens is the file is selected and OK'd and then it all closes down.

Thanks in advance


Code:
Function selectFile()
Dim fd As FileDialog, fileName As String
 
On Error GoTo ErrorHandler
 
Set fd = Application.FileDialog(msoFileDialogFilePicker)
 
fd.AllowMultiSelect = False
 
If fd.Show = True Then
    If fd.SelectedItems(1) <> vbNullString Then
        fileName = fd.SelectedItems(1)
     
    End If
    
Else
    'Exit code if no file is selected
    End
End If
 
'Return Selected FileName
selectFile = fileName

Set fd = Nothing
 
Exit Function
 
ErrorHandler:
Set fd = Nothing
MsgBox "Error " & Err & ": " & Error(Err)
 
End Function
 
So your user points to file:
ABCD.txt
or
XYZ.doc
or
MyFile.xls
or
OtheFile.exe

What do you want to happen then?

Open ABCD.txt in Notepad?
Open XYZ.slc in Excel?
Open MyFile.doc in Word?
Open OtherFile.exe in ...?

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Andrzejek

The file could be of any format, I just want it to open once they select it...
 
Try this:

Code:
Public Sub OpenDocument(strDocPath As String)
Dim G As Long
G = Shell("RUNDLL32.EXE URL.DLL,FileProtocolHandler " & strDocPath, vbNormalFocus)
End Sub

Just pass the full path with the file name.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top