Excel

Select Program to Open a File

Ease of Use

Easy

Version tested with

2000, 2002 

Submitted by:

Justinlabenne

Description:

Select the program you want to open a certain file with 

Discussion:

When opening a certain file, you may want to select a certain program to open the file with. This code shows the "Open With" dialog allowing the user to select what program to use when opening a pre-selected file. The example workbook contains the code workbook, and an example text file which designated as the file to open. 

Code:

instructions for use

			

Option Explicit Sub OpenHow() ' String for the file path: Dim szFileToOpen As String szFileToOpen = ThisWorkbook.Path & "\tester.txt" ' String for RunDll: Dim szOpenWith As String szOpenWith = "rundll32.exe shell32.dll,OpenAs_RunDLL " & szFileToOpen ' Variable for the shell: Dim vRun As Variant vRun = Shell(szOpenWith, vbMaximizedFocus) End Sub

How to use:

  1. Open an Excel Workbook
  2. Copy the code
  3. Press Alt + F11 to open the Visual Basic Editor (VBE)
  4. Select INSERT > MODULE from the menubar
  5. Paste code into the right pane
  6. Press Alt+Q to return to Excel
  7. Save workbook before any other changes
 

Test the code:

  1. The example uses a text file that is assumed to be in the same location as the Excel workbook and is named {tester.txt}
  2. You can incorporate any fixed file path or browse for file routine into the code to allow for selecting of other files.
  3. Go to TOOLS > MACRO > MACROS
  4. When the dialog appears, select {OpenHow}
  5. Press Run
 

Sample File:

RunWithIt.zip 6.5KB 

Approved by mdmackillop


This entry has been viewed 275 times.

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