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

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Connect With a Mentor Infotrakker SoftwareCommented:
<The submitted code was cut and paste from another question, is it good enogh to be used as is:>

1. Yes, paste the Private Declare function into the General Declaration section of a Standard Module and you can call it from anywhere in your project.

2. The options are simply "Print" and "Open" ... and I'm not sure you need the select Case, since you can call it directly:

Dim strFileName as string
strFileName = Me!lstFiles.Column(1)
 
strFileName ="C:Folder1\" & strFileName
ShellExecute Me.hWnd, "Open", strFileName, 0&, 0&, vbMinimized

Note you can also use vbMaximized if you need to ... here's a link to the MSDN article about ShellExecute. Note the various other options you can use ("edit", "find", etc) and the various constants you can use as the final argument.

http://msdn2.microsoft.com/en-us/library/bb762153.aspx





0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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

0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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 ...
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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)
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
0
 
Rey Obrero (Capricorn1)Commented:
try using

followhyperlink  filename

filename ext could be .doc, .xls etc
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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 ...
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Very interesting. I will read to find out about all these options. Very powerful.

Thanks.
0
All Courses

From novice to tech pro — start learning today.