Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #123
Online Version at http://www.codeoftheweek.com/membersonly/bi/0123.html (paid subscribers only)
All content and source code is Copyright (c) 2000 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Retrieving file associations

Earn REAL money surfing the web!

If you would like to make some extra cash for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html

Requirements

In this Issue

In this issue we discuss how to retrieve the executable name for any file.

If you have any questions about using this module, let us know at questions@codeoftheweek.com

basAssociations

This module makes it easy to get the executable associated with a particular extension. This routine is most useful for applications that have to launch filenames by their extensions. One great use for this function is to automatically launch a browser based on a particular HTML file that might exist in your application. It could also be used to intelligently use the existing file viewers/editors already installed on a users machine (such as looking for the default editor for the DOC extension).

Functions

Public Function FindAssociation(sFilename As String, Optional bWithParams As Boolean = False) As String

This routine does all the work of calling the FindExecutable API call to determine the program/executable associated with the extension specified in sFilename. This function requires the file to exist to determine the executable name. The bWithParams is an optional parameter that allows you to control what kind of data gets returned. If bWithParams is not specified or False only the executable path and filename will be returned. If it is True then any additional parameters or command line information will be returned. Typically the full executable filename along with any startup parameters and the file to launch will be returned as a single string.

NOTE: The FindExecutable API call seems to have a problem returning the full parameters when there is a space (blank character) in the executable filename. Use the bWithParams with caution in this case.

Public Function ReverseInstr(sStringToSearch As String, _
                                sStringToFind As String, _
                                Optional Compare As VbCompareMethod = vbBinaryCompare) As Long

See Issue #61 for details - http://www.codeoftheweek.com/issues/0061.html

Public Function ReplaceChars(sItemData As String, sSrc As String, sDest As String) As String

See Issue #9 for details - http://www.codeoftheweek.com/issues/0009.html

Sample Usage

Shows how to retrieve the executable associated with JPG files:

    Debug.Print FindAssociation("C:\WINDOWS\HELP\iis\htm\tutorial\nyc.JPG", False)
    ' will return:
    '     C:\PROGRA~1\INTERN~1\iexplore.exe

    Debug.Print FindAssociation("C:\WINDOWS\HELP\iis\htm\tutorial\nyc.JPG")
    ' will return:
    '     C:\PROGRA~1\INTERN~1\iexplore.exe   "C:\WINDOWS\HELP\IIS\HTM\TUTORIAL\NYC.JPG"


Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0123.html


This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Get paid to surf the web!

If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com