Library tutorials & articles

File Assocation

File Associaton

To get the tmg (or any other) extension to be recognised in Explorer (or any other program), you need to do two things:
1) Register the tmg extension so that explorer knows to run your application when a file of that type is clicked on
2) Provide a description for the extension (ie Word Document)
3) and finally, once your program has been run, get it to open the file.

So, for #1, you can use the following code. Read the comments to see  what you need to change. Note that you will need to compile your program to an EXE for this to work (so that explorer can call your program)

'// Registry windows api calls
Private Declare Function RegCreateKey& Lib "advapi32.DLL" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpszSubKey As String, lphKey As Long)
Private Declare Function RegSetValue& Lib "advapi32.DLL" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpszSubKey As String, ByVal fdwType As Long, ByVal lpszValue As String, ByVal dwLength As Long)
'// Required constants
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const MAX_PATH = 256&
Private Const REG_SZ = 1

'// procedure you call to associate the tmg extension with your program.
Private Sub MakeDefault()
    Dim sKeyName  As String  '// Holds Key Name in registry.
    Dim sKeyValue As String  '// Holds Key Value in registry.
    Dim ret       As Long    '// Holds error status if any from API calls.
    Dim lphKey    As Long    '// Holds created key handle from RegCreateKey.
   
    '// This creates a Root entry called "TextMagic"
    sKeyName = "TextMagic" '// Application Name
    sKeyValue = "TextMagic Document" '// File Description
    ret = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)
    ret = RegSetValue&(lphKey&, Empty, REG_SZ, sKeyValue, 0&)

    '// This creates a Root entry called .tmg associated with "TextMagic".
    sKeyName = ".tmg" '// File Extension
    sKeyValue = "TextMagic" '// Application Name
    ret = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)

    ret = RegSetValue&(lphKey, Empty, REG_SZ, sKeyValue, 0&)

    '//This sets the command line for "TextMagic".
    sKeyName = "TextMagic" '// Application Name
    If Right$(App.Path,1) = "\" Then
        sKeyValue = App.Path & App.EXEName & ".exe %1" '// Application Path
    Else
        sKeyValue = App.Path & "\" & App.EXEName & ".exe %1" '// Application Path
    End If
    ret = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)
    ret = RegSetValue&(lphKey, "shell\open\command", REG_SZ, sKeyValue, MAX_PATH)
End Sub

Private Sub Form_Load()
    '// ensure we only register once. When debugging etc, remove the SaveSetting line, so your program will
    '// always attempt to register the file extension
    If GetSetting(App.Title, "Settings", "RegisteredFile", 0) =  0 Then
        '// associate tmg extension with this app
        MakeDefault
        SaveSetting App.Title, "Settings", "RegisteredFile", 1
    End If
End Sub

Now, try compiling your program and then run it. This will associate the file extension with your program. Now create a new text document, and name it test.tmg. Go into explorer, and you will see that explorer recognises test.tmg as a TextMagic Document. Click on the file, and your program will run.

Comments

  1. 19 Sep 2006 at 22:17

    Hi Can you please also tell how to do it in .Net using VB.Net or C# ...

  2. 16 Aug 2005 at 23:14

    This looks like it could be the answer to my problem, but in case i stuff it up somehow could you tell me how to reverse the changes that are made to the registry by this code please?
    Thanks

  3. 09 Aug 2005 at 08:36

    please help me..,
    i have problem, i can't load file word (*.doc) from vb, can u tell me how to do it??
    thx b4.
    please send ur coding to niel_onggan@yahoo.com

  4. 29 Apr 2003 at 15:21
    That's your procedure that does whatever when its been given a filename (ie loading the file into a textbox or something)
  5. 24 Apr 2003 at 19:32

    The code is working fine, except for the OpenFile bit.
    The comment says to call your openfile procedure.
    What is the OpenFile command?

  6. 20 Apr 2003 at 13:03

    In using implimens this process into an application, I noticed the following line:


    ret = RegSetValue&(lphKey, "shellopencommand", REGSZ, sKeyValue, MAXPATH)


    did not properly register the association. I am using Windows 98 and had to change the above line to:


    ret = RegSetValue&(lphKey, "shell\open\command", REGSZ, sKeyValue, MAXPATH)


    to get it to work properly.

  7. 01 Jan 1999 at 00:00

    This thread is for discussions of File Assocation.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback