Open different file types

A

AdmSteck

Is it possible to code a line that opens a file with the default viewer for
that file type? I can test for the type (.xls, .doc) and use and if or
select case to choose the appropriate .exe to run, but this only works with
the file types that are hard coded in the module. Can I just tell it the
file name and let Windows decide how to open it just like you double clicked
on it in Explorer?
 
R

Robert_L_Ross

This code doesn't seem to work in my situation.

I have a list of files in a table. I want the code to open the file in a
field on my form:
Private Sub Long_File_Path_DblClick(Cancel As Integer)
DoCmd.OpenFunction fHandleFile(Me![Long File Path], WIN_NORMAL)
End Sub

I pass the field to the function but I get the following error:
Run-time error '7874':
Microsoft Access can't find the object '2, Error: File not found. Couldn't
Execute!.'

The file I'm trying to open is:
"G:\A F S\Monthly Reports\HiPath\SR\MonthlyS_GrpTime_091906_090053.txt"

When I go to the Immediate window and enter:

?fhandlefile("G:\A F S\Monthly
Reports\HiPath\SR\MonthlyS_GrpTime_091906_090053.txt", win_normal)

I get the same error, but when I enter:

?fHandleFile("C:\Documents and Settings\RRoss\Desktop\AFAA1Resume.txt",
Win_Normal)

This file opens. Any idea what would cause it to NOT open on a network
server but to open on my local drive?
 
D

Douglas J. Steele

No need for the DoCmd.OpenFunction. All you need is:

Private Sub Long_File_Path_DblClick(Cancel As Integer)
Call fHandleFile(Me![Long File Path], WIN_NORMAL)
End Sub

or

Private Sub Long_File_Path_DblClick(Cancel As Integer)
fHandleFile Me![Long File Path], WIN_NORMAL
End Sub


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Robert_L_Ross said:
This code doesn't seem to work in my situation.

I have a list of files in a table. I want the code to open the file in a
field on my form:
Private Sub Long_File_Path_DblClick(Cancel As Integer)
DoCmd.OpenFunction fHandleFile(Me![Long File Path], WIN_NORMAL)
End Sub

I pass the field to the function but I get the following error:
Run-time error '7874':
Microsoft Access can't find the object '2, Error: File not found.
Couldn't
Execute!.'

The file I'm trying to open is:
"G:\A F S\Monthly Reports\HiPath\SR\MonthlyS_GrpTime_091906_090053.txt"

When I go to the Immediate window and enter:

?fhandlefile("G:\A F S\Monthly
Reports\HiPath\SR\MonthlyS_GrpTime_091906_090053.txt", win_normal)

I get the same error, but when I enter:

?fHandleFile("C:\Documents and Settings\RRoss\Desktop\AFAA1Resume.txt",
Win_Normal)

This file opens. Any idea what would cause it to NOT open on a network
server but to open on my local drive?


Douglas J. Steele said:
 
D

Douglas J. Steele

The only thing that comes to mind is that there may be permissions issues:
perhaps you can't actually read that file using the security context under
which you're running Access.

Try:

Private Sub Long_File_Path_DblClick(Cancel As Integer)
If Len(Dir(Me![Long File Path])) > 0 Then
fHandleFile Me![Long File Path], WIN_NORMAL
Else
MsgBox Me![Long File Path] & " doesn't appear to exist."
End If
End Sub



--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Robert_L_Ross said:
Now it really gets weird.

This works for the following files:
"G:\A F S\DATABASES\Notepad Functions\DirectoryPrinter\setting.ini"
"G:\A F S\Cindy DeBord\SLATE\SLATE Queries\Loans Guaranteed with LID that
Is
Not In SLATE.sql"
"G:\A F S\Monthly Reports\ProdAnl\Chuck\Shortcut to 2006-2007 Q1 DM TM
Daily.lnk"

But it does not work with these:
"G:\A F S\Contracts & Invoices\National Student Clearinghouse
Invoices\06-07
NSC.xls"
"G:\A F S\Weekly Updates FY 2006-07\11-03-06.doc"
"G:\A F S\Cindy DeBord\SLATE\SLATE Queries\Universal Query.sql"

I've verified the files exist...any ideas why it 'picks and chooses' what
files it will open?

PS, the 2nd example worked, the first didn't do anything.

THX!




Douglas J. Steele said:
No need for the DoCmd.OpenFunction. All you need is:

Private Sub Long_File_Path_DblClick(Cancel As Integer)
Call fHandleFile(Me![Long File Path], WIN_NORMAL)
End Sub

or

Private Sub Long_File_Path_DblClick(Cancel As Integer)
fHandleFile Me![Long File Path], WIN_NORMAL
End Sub


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Robert_L_Ross said:
This code doesn't seem to work in my situation.

I have a list of files in a table. I want the code to open the file in
a
field on my form:
Private Sub Long_File_Path_DblClick(Cancel As Integer)
DoCmd.OpenFunction fHandleFile(Me![Long File Path], WIN_NORMAL)
End Sub

I pass the field to the function but I get the following error:
Run-time error '7874':
Microsoft Access can't find the object '2, Error: File not found.
Couldn't
Execute!.'

The file I'm trying to open is:
"G:\A F S\Monthly Reports\HiPath\SR\MonthlyS_GrpTime_091906_090053.txt"

When I go to the Immediate window and enter:

?fhandlefile("G:\A F S\Monthly
Reports\HiPath\SR\MonthlyS_GrpTime_091906_090053.txt", win_normal)

I get the same error, but when I enter:

?fHandleFile("C:\Documents and Settings\RRoss\Desktop\AFAA1Resume.txt",
Win_Normal)

This file opens. Any idea what would cause it to NOT open on a network
server but to open on my local drive?


:

Take a look at http://www.mvps.org/access/api/api0018.htm at "The
Access
Web"

--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)


Is it possible to code a line that opens a file with the default
viewer
for
that file type? I can test for the type (.xls, .doc) and use and if
or
select case to choose the appropriate .exe to run, but this only
works
with
the file types that are hard coded in the module. Can I just tell
it
the
file name and let Windows decide how to open it just like you double
clicked
on it in Explorer?
 
T

Toys

Hi,

The code works great where the file type is known. I'm really looking for
ways to open files where the file type could be different. I'll explain.

I'm saving documents as per the ID, but the file could be a .doc, .pdf,
..xls, .html, .txt. I'm currently storing the file type against the record
and then using: Application.Followhyperlink("foldername\" & ID & "." & TYPE).
This works, but I'd rather not have to store the type because this could
lead to false error, i.e. the doc is there, but it was file type was
incorrectly assigned.
 
D

Douglas J. Steele

I don't understand. Assuming "foldername\" & ID & "." & TYPE results in the
full path to the file,

Call fHandleFile(("foldername\" & ID & "." & TYPE, WIN_NORMAL)

should open it.
 
Top