Associate




Private Declare Sub SHChangeNotify Lib "shell32.dll" _
(ByVal wEventId As Long, ByVal uFlags As Long, _
ByVal dwItem1 As Long, ByVal dwItem2 As Long)

Const SHCNE_ASSOCCHANGED = &H8000000
Const SHCNF_IDLIST = 0

' Create the new file association

' Extension is the extension to be registered (eg ".cad"

' ClassName is the name of the associated class (eg "caddoc")

' Description is the textual description (eg "CAD Document"

' ExeProgram is the app that manages that extension (eg "c:\Cad\MyCad.exe")

'

' NOTE: requires CreateRegistryKey and SetRegistryValue functions

'(these routines can be downloaded from vb2themax's Code Bank


Sub CreateFileAssociation(ByVal Extension As String, _
ByVal ClassName As String, ByVal Description As String, _
ByVal ExeProgram As String)
Const HKEY_CLASSES_ROOT = &H80000000

' ensure that there is a leading dot

If Left(Extension, 1) <> "." Then
Extension = "." & Extension
End If

' create a new registry key under HKEY_CLASSES_ROOT

CreateRegistryKey HKEY_CLASSES_ROOT, Extension
' create a value for this key that contains the classname

SetRegistryValue HKEY_CLASSES_ROOT, Extension, "", ClassName
' create a new key for the Class name

CreateRegistryKey HKEY_CLASSES_ROOT, ClassName & "\Shell\Open\Command"
' set its value to the command line

SetRegistryValue HKEY_CLASSES_ROOT, _
ClassName & "\Shell\Open\Command", "", ExeProgram & " ""%1"""

' notify Windows that file associations have changed

SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0
End Sub










( associate.html )- by Paolo Puglisi - Modifica del 17/12/2023