Results 1 to 6 of 6

Thread: File associations... etc.

  1. #1

    Thread Starter
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    1001

    'for your applications, you may need to create file
    'associations for your saved data.
    'File associations allow your program to be run
    'when a file with a certain extension is 'double-clicked
    'in explorer. To do this in VB involves a little bit of


    Option Explicit

    Public Type mnuCommands
    Captions As New Collection
    Commands As New Collection
    End Type

    Public Type filetype
    Commands As mnuCommands
    Extension As String
    ProperName As String
    FullName As String
    ContentType As String
    IconPath As String
    IconIndex As Integer
    End TypePublic

    Const REG_SZ = 1
    Public Const HKEY_CLASSES_ROOT = &H80000000

    Public Declare Function RegCloseKey Lib _
    "advapi32.dll" (ByVal hKey As Long) As Long

    Public Declare Function RegCreateKey Lib _
    "advapi32" Alias "RegCreateKeyA" (ByVal _
    hKey As Long, ByVal lpszSubKey As String, _
    phkResult As Long) As Long

    Public Declare Function RegSetValueEx Lib _
    "advapi32" Alias "RegSetValueExA" (ByVal _
    hKey As Long, ByVal lpszValueName As String, _
    ByVal dwReserved As Long, ByVal fdwType As _
    Long, lpbData As Any, ByVal cbData As Long) As Long


    Public Sub CreateExtension(newfiletype As filetype)

    Dim IconString As String
    Dim Result As Long, Result2 As Long, ResultX As Long
    Dim ReturnValue As Long, HKeyX As Long
    Dim cmdloop As Integer

    IconString = newfiletype.IconPath & "," & _
    newfiletype.IconIndex

    If Left$(newfiletype.Extension, 1) <> "." Then _
    newfiletype.Extension = "." & newfiletype.Extension

    RegCreateKey HKEY_CLASSES_ROOT, _
    newfiletype.Extension,Result
    ReturnValue = RegSetValueEx(Result, "", 0, REG_SZ, _
    ByVal newfiletype.ProperName, _
    LenB(StrConv(newfiletype.ProperName, vbFromUnicode)))

    ' Set up content type
    If newfiletype.ContentType <> "" Then
    ReturnValue = RegSetValueEx(Result, _
    "Content Type", 0, REG_SZ, ByVal _
    CStr(newfiletype.ContentType), _
    LenB(StrConv(newfiletype.ContentType, vbFromUnicode)))
    End If

    RegCreateKey HKEY_CLASSES_ROOT, _
    newfiletype.ProperName, Result

    If Not IconString = ",0" Then
    RegCreateKey Result, "DefaultIcon", _
    Result2 'Create The Key of "ProperName\DefaultIcon"
    ReturnValue = RegSetValueEx(Result2, _
    "", 0, REG_SZ, ByVal IconString, _
    LenB(StrConv(IconString, vbFromUnicode)))
    'Set The Default Value for the Key
    End If

    ReturnValue = RegSetValueEx(Result, _
    "", 0, REG_SZ, ByVal newfiletype.FullName, _
    LenB(StrConv(newfiletype.FullName, vbFromUnicode)))
    RegCreateKey Result, ByVal "Shell", ResultX

    ' Create neccessary subkeys for each command
    For cmdloop = 1 To newfiletype.Commands.Captions.Count
    RegCreateKey ResultX, ByVal _
    newfiletype.Commands.Captions(cmdloop), Result
    RegCreateKey Result, ByVal "Command", Result2
    Dim CurrentCommand$
    CurrentCommand = newfiletype.Commands.Commands(cmdloop)
    ReturnValue = RegSetValueEx(Result2, _
    "", 0, REG_SZ, ByVal CurrentCommand$, _
    LenB(StrConv(CurrentCommand$, vbFromUnicode)))
    RegCloseKey Result
    RegCloseKey Result2
    Next

    RegCloseKey Result2
    End Sub


    ' <<<<<< Form Event Code >>>>>>

    Dim myfiletype As filetype

    myfiletype.ProperName = "MyFile"
    myfiletype.FullName = "My File Type"
    myfiletype.ContentType = "SomeMIMEtype"
    myfiletype.Extension = ".MYF"
    myfiletype.Commands.Captions.Add "Open"
    myfiletype.Commands.Commands.Add _
    "c:\windows\notepad.exe ""%1"""
    myfiletype.Commands.Captions.Add "Print"
    myfiletype.Commands.Commands.Add _
    "c:\windows\notepad.exe ""%1"" /P"
    CreateExtension myfiletype

    'The filetype type is pretty self-explanatory.
    'Extension contains the file type extension.
    'Proper name is the name of the type, which you would
    'refer to the type as (do not use spaces).
    'FullName is the description of the file type.
    'ContentType is the description that you would see
    'in your Internet browser, if you were to download a
    'file of this type. You could think of the extension
    'as the "shortcut key", the proper name as the "name",
    'and the fullname as the "caption".

    'This Commands part of the filetype contains
    'the "verbs" for the file type. When you right-click on
    'a file of this type, you would see these options.
    'Open is set as the default "verb", so when you double
    'click on the file type, it executes the command associated
    'with "Open". You can add others, such as "Print" as needed.

    'Be careful with registry editing, as it can have disasterous effects.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Hello Dennis!

    This is a sample of an association.

    Code:
    Option Explicit
    
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
    Private Const HKEY_CLASSES_ROOT = &H80000000
    Private Const MAX_PATH = 260&
    Private Const REG_SZ = 1
    
    Sub Main()
    Dim sKeyName As String   'Holds Key Name in registry.
    Dim sKeyValue As String  'Holds Key Value in registry.
    Dim ret&                 'Holds error status if any from API calls.
    Dim lphKey&              'Holds created key handle from RegCreateKey.
    
    'This creates a Root entry called "CacApp".
        sKeyName = "CacApp"
        sKeyValue = "Cac Application"
        
        ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
        ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
        
    'This creates a Root entry called .BAR associated with "CacApp".
        sKeyName = ".BAR"
        sKeyValue = "CacApp"
        ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
        ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
        
    'This sets the command line for "CacApp".
        sKeyName = "CacApp"
        sKeyValue = "C:\Program Files\Microsoft Office\Office\EXCEL.EXE %1"
        ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
        ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, sKeyValue, MAX_PATH)
    End Sub

    If you want to open a program with its default, use the API Shell Execute.

    Here is a link that I helped someone this morning on the same subject.
    http://forums.vb-world.net/showthrea...threadid=20372

    Let me know if you need anything else.
    Chemically Formulated As:
    Dr. Nitro

  3. #3
    Guest
    thanks.
    could you please repost that code with smilies disabled?

    also, where do I put the shellexecute code?

    and for the "Hello.doc" part. since I am opening the file from someone dbl clicking it... what will I put there?


  4. #4
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    I disable the smile Dennis.

    You can put the ShellExecute in a Double Click event of a listbox or a command button. Keep that separate from the above codes because it is a separate step.

    The ShellExecute is just to open your files with the associated program. It is equivalent to VB Shell command but much more powerful.

    "Hello.doc" is your file name. Replace the file name and it will open with the associate program.

    The codes above will cause association and it is just a one time deal type of thing. Once you have run it, it will be associate from that point on.

    I highly recommend you test these two steps separately before combining it together.


    If you need furthur help, I will make a small program for you Dennis.

    Have a nice day.

    [Edited by Nitro on 06-22-2000 at 03:48 PM]
    Chemically Formulated As:
    Dr. Nitro

  5. #5
    Guest
    hmmm I am not sure if you understand this correctly,
    but i want my file(lets say I associate my prog. with txt files.) to be open in the program, like a txt file in notepad, how would I know what the filename is?

  6. #6
    Lively Member
    Join Date
    Jun 2000
    Posts
    82
    If you want to know what the user double clicked on in Explorer to run your program, use the Command$ variable

    i.e. in the form load event,

    Code:
    FileNm=Command$
    Open FileNm for input as #1
    Input #1,NewText
    Close #1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width