Multiple Apps

Open Any File (Non Office Files as Well)

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro demonstrates how to open any file from your Office application. For example, you can open a text file with Notepad from Excel. 

Discussion:

At times you may want to open a file from Excel, Word etc. But that file may be opened with a different application. For example, maybe you have a help file for Excel in Text format or it may be a PDF. This macro shows how to open the file with the default program associated with the file. 

Code:

instructions for use

			

Option Explicit 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 Function OpenAnyFile(FileToOpen As String) Call ShellExecute(0, "Open", FileToOpen & vbNullString, _ vbNullString, vbNullString, 1) End Function Sub Example() Call OpenAnyFile(ThisWorkbook.Path & "\Example Text File.txt") End Sub

How to use:

  1. Open Excel, or Word etc.
  2. Open the VBE (Alt + F11)
  3. Insert | Module
  4. Paste the code in the Code Window that opens up.
  5. Note: The FileToOpen Argument in this example uses ThisWorkbook.Path as part of the path. This is an excel example. If you are not using Excel, change this to the valid path for the file that you want to open.
  6. Close the VBE.
 

Test the code:

  1. Tools | Macro | Macros...
  2. Select OpenAnyFile and click Run.
 

Sample File:

Open Any File.zip 7.07KB 

Approved by mdmackillop


This entry has been viewed 181 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express