Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Open any type of file... VBA

Listbox lstFiles displays files with various extension types. To open a file on dbl-click I have the submitted code.

Question: What is the best way to open any selected file? At this time I am able to open pdf files only.

Thank you.
Dim strFileName as string
strFileName = Me!lstFiles.Column(1)
 
strFileName ="C:Folder1\" & strFileName 
 
                               v-- file extention type
Select lstFiles.Column(2)
Case "pdf":Shell ("C:\Acrobat3\Reader\AcroRd32.exe " & strFileName), vbMaximizedFocus
Case "gif"
Case "doc"
Case "xls"
Case "zip"
' may others not listed here
'
End Select

Open in new window

Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

If your filetypes are associated with a program on the machine, then just opening the file with Shell will launch the associated program and load that file ... so you could just do this:

strFileName = "C:\Folder1\" & strFileName
Shell strFileName

Note also if your filetype is NOT associated with a program, then you'd not be able to open that file without user intervention anyway ... in other words, if I'm trying to open a file with the .abc extension, and i have no program associated with the .abc extension, then Windows will either (a) do nothing or (b) popup a dialog asking me what to use to open the file ...
Avatar of Mike Eghtebas

ASKER

LSMConsulting,

I think there are some other methods of opening a file other than Shell and those method(s) require only a file name. We don't have to tell where the exe file of that file is located. That is what I am looking for.
-----------
BTW, it should read:

Select Case lstFiles.Column(2)
re:> Note also if your filetype is NOT associated with a program

I was working on a application 2004, where I was using some lines of code without specifying the exe file and its location.

re:> popup a dialog asking me what to use to open the file ...

If cannot find exe for a particula extension, it is acceptable to bring up such interface.

Mike

something like I think:

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

ShellExecute Me.hWnd, "Print", "TheFileName", 0&, 0&, vbMinimized
try using

followhyperlink  filename

filename ext could be .doc, .xls etc
Shell only requires the filename IF the file is associated with a program.

The ShellExecute function is basically a more "powerful" version of Shell .. you can get return information about it i.e. if the file won't open, it can return an error code, etc etc ...
capricorn1,

re:> followhyperlink  filename

Not clear what you meant by this comment.
------------------
LSMConsulting,
The submitted code was cut and paste from another question, is it good enogh to be used as is:

Q1: Do I put following in a module?
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

Q2: How would I include the following line with my Select Case routine?

ShellExecute Me.hWnd, "Print", "TheFileName", 0&, 0&, vbMinimized

Or jus use:

Dim strFileName as string
strFileName = Me!lstFiles.Column(1)
 
strFileName ="C:Folder1\" & strFileName
                                         
                                          v-- I am not printing. Just opening
ShellExecute Me.hWnd, "Print", strFileName, 0&, 0&, vbMinimized

I want have the option of both print and opening. What constants I will be using?

Thanks,

Mike
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi LSMConsulting,

Thank you very much for the information. This looks very good. I am excited about edit and find option. I will check to find more abour them.

I will try this tomorrow morning at work.

Mike
Very interesting. I will read to find out about all these options. Very powerful.

Thanks.