Set RTF file handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D & J G

    Set RTF file handling

    Is there a way to instruct Windows file-handling - 'Opens with:' - from
    within VB6? I want to ensure that files with the .rtf (rich text) extension
    always go to Word Pad, no matter which future computer uses my program.

    TIA
    Don


  • J French

    #2
    Re: Set RTF file handling

    On Sat, 25 Feb 2006 20:57:16 +1000, "D & J G"
    <donandjane@opt usnet.com.au> wrote:
    [color=blue]
    >Is there a way to instruct Windows file-handling - 'Opens with:' - from
    >within VB6? I want to ensure that files with the .rtf (rich text) extension
    >always go to Word Pad, no matter which future computer uses my program.[/color]

    Yes

    You set up the file association for RTF in the registry

    Here is some code, you would be wise to check the API error results,
    as it is likely that 'permissions' will not be set up on some
    machines.

    Form code, followed by Class code

    Option Explicit

    Private Sub Command1_Click( )
    Dim C As New cFleAssn
    Dim Ext$
    Dim Handler$
    Dim FileType$, ContentType$, ExeName$

    Me.AutoRedraw = True
    Me.Cls

    Ext$ = "jpg"

    Call C.GetExtAssocia tions(Ext$, _
    Handler$, _
    FileType$, _
    ContentType$, _
    ExeName$)
    Me.Print Ext$
    Me.Print Handler$
    Me.Print FileType$
    Me.Print ContentType$
    Me.Print ExeName$

    ' ' --- Register
    ' Ext$ = "FRED"
    ' Handler$ = "fred:" + Handler$
    ' Call C.RegisterExt(E xt$, _
    ' Handler$, _
    ' FileType$, _
    ' ContentType$, _
    ' ExeName$)
    '
    ' ' --- Check
    ' Call C.GetExtAssocia tions(Ext$, _
    ' Handler$, _
    ' FileType$, _
    ' ContentType$, _
    ' ExeName$)
    ' Me.Print ""
    ' Me.Print Ext$
    ' Me.Print Handler$
    ' Me.Print FileType$
    ' Me.Print ContentType$
    ' Me.Print ExeName$
    '
    ' ' Unregister
    ' Call C.UnRegisterExt (Ext$)

    End Sub

    ======== Start of cFileAssn.cls ========

    Option Explicit: DefObj A-Z

    '
    ' HANDLER .Zip : WinZip
    ' CONTENT TYPE "Content Type" : Application-Zip-Compressed
    ' FILE TYPE WinZip : WinZip File
    ' OPEN COMMAND WinZip + "\shell\open\co mmand" : "C:\xxx\Win Zip" "%1"
    '
    ' V.Handler = "BinMgr"
    ' V.FileType = "BIN Download File"
    ' V.OpenCmd = " + AppPath(App.EXE Name) + ".exe" + " %1"
    ' V.ContentType = "applicatio n/download document"

    Private Const CONTENT_TYPE$ = "Content Type"
    Private Const SHELL_OPEN_COMM AND$ = "\shell\open\co mmand"


    Const REG_SZ = 1 ' Unicode nul terminated string
    Const REG_BINARY = 3 ' Free form binary
    Const HKEY_CURRENT_US ER = &H80000001
    Const HKEY_CLASSES_RO OT = &H80000000

    Private Declare Function RegOpenKey _
    Lib "advapi32.d ll" _
    Alias "RegOpenKey A" _
    (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    phkResult As Long) As Long
    Private Declare Function RegCloseKey _
    Lib "advapi32.d ll" _
    (ByVal hKey As Long) As Long

    Private Declare Function RegCreateKey _
    Lib "advapi32.d ll" _
    Alias "RegCreateK eyA" _
    (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    phkResult As Long) As Long

    Private Declare Function RegDeleteValue _
    Lib "advapi32.d ll" _
    Alias "RegDeleteValue A" _
    (ByVal hKey As Long, _
    ByVal lpValueName As String) As Long

    Private Declare Function RegDeleteKey _
    Lib "advapi32.d ll" _
    Alias "RegDeleteK eyA" _
    (ByVal hKey As Long, _
    ByVal lpSubKey As String) As Long

    Private Declare Function RegQueryValueEx _
    Lib "advapi32.d ll" _
    Alias "RegQueryValueE xA" _
    (ByVal hKey As Long, _
    ByVal lpValueName As String, _
    ByVal lpReserved As Long, _
    lpType As Long, _
    lpData As Any, _
    lpcbData As Long) As Long
    Private Declare Function RegSetValueEx _
    Lib "advapi32.d ll" _
    Alias "RegSetValueExA " _
    (ByVal hKey As Long, _
    ByVal lpValueName As String, _
    ByVal Reserved As Long, _
    ByVal dwType As Long, _
    lpData As Any, _
    ByVal cbData As Long) As Long

    Private Type TPACKET
    Ext As String
    Handler As String
    FileType As String
    ContentType As String
    OpenCommand As String
    End Type




    ' ############### ############### ############### ############### #
    '
    ' AllAPI - utils
    '
    Private Function RegQueryStringV alue(ByVal hKey As Long, _
    ByVal strValueName As String) As String

    Dim lResult As Long, lValueType As Long, strBuf As String,
    lDataBufSize As Long

    'retrieve information about the key
    lResult = RegQueryValueEx (hKey, strValueName, 0, lValueType, ByVal
    0, lDataBufSize)
    If lResult = 0 Then
    If lValueType = REG_SZ Then
    'Create a buffer
    strBuf = String(lDataBuf Size, Chr$(0))
    'retrieve the key's content
    lResult = RegQueryValueEx (hKey, strValueName, 0, 0, ByVal
    strBuf, lDataBufSize)
    If lResult = 0 Then
    'Remove the unnecessary chr$(0)'s
    RegQueryStringV alue = Left$(strBuf, InStr(1, strBuf,
    Chr$(0)) - 1)
    End If
    ElseIf lValueType = REG_BINARY Then
    Dim strData As Integer
    'retrieve the key's value
    lResult = RegQueryValueEx (hKey, strValueName, 0, 0, strData,
    lDataBufSize)
    If lResult = 0 Then
    RegQueryStringV alue = strData
    End If
    End If
    End If
    End Function

    Private Function LF_GetString$(h Key As Long, strPath As String, _
    strValue As String)
    Dim Ret&
    'Open the key
    RegOpenKey hKey, strPath, Ret
    'Get the key's content
    LF_GetString = RegQueryStringV alue(Ret, strValue)
    'Close the key
    RegCloseKey Ret
    End Function

    Private Sub LS_SaveString(h Key As Long, _
    strPath As String, _
    strValue As String, _
    strData As String)
    Dim Ret&
    'Create a new key
    RegCreateKey hKey, strPath, Ret
    'Save a string to the key
    RegSetValueEx Ret, strValue, 0, REG_SZ, _
    ByVal strData, Len(strData)
    'close the key
    RegCloseKey Ret
    End Sub


    Private Sub LS_DelSetting(h Key As Long, strPath As String, strValue As
    String)
    Dim Ret&
    'Create a new key
    RegCreateKey hKey, strPath, Ret
    'Delete the key's value
    RegDeleteValue Ret, strValue
    'close the key
    RegCloseKey Ret
    End Sub

    ' ############### ############### ############### ############### #
    '
    '
    '
    '
    '
    ' ############### ############### ############### ############### #

    ' ############### ############### ############### ############### #
    '
    ' Lower Case - starting with '.'
    '
    Private Function LF_FormatExt$(E xt$)
    Dim S$
    S$ = Trim$(LCase$(Ex t$))
    If Left$(S$, 1) <> "." Then
    S$ = "." + S$
    End If

    LF_FormatExt$ = S$
    End Function

    ' ############### ############### ############### ############### #
    '
    '
    '
    Private Sub LS_GetAssociati ons(A As TPACKET)
    Dim NIX As TPACKET

    If A.Ext$ = "." Then
    A = NIX
    Exit Sub
    End If

    A.Handler = LF_GetString(HK EY_CLASSES_ROOT , A.Ext$, "")
    A.ContentType = LF_GetString(HK EY_CLASSES_ROOT , A.Ext$,
    CONTENT_TYPE)

    A.FileType = LF_GetString(HK EY_CLASSES_ROOT , A.Handler$, "")
    A.OpenCommand = LF_GetString(HK EY_CLASSES_ROOT , A.Handler$ +
    SHELL_OPEN_COMM AND, "")

    End Sub

    ' ############### ############### ############### ############### #
    '
    '
    '
    Private Sub LS_PutAssociati ons(A As TPACKET)

    LS_SaveString HKEY_CLASSES_RO OT, A.Ext$, "", A.Handler$
    LS_SaveString HKEY_CLASSES_RO OT, A.Ext$, CONTENT_TYPE,
    A.ContentType

    LS_SaveString HKEY_CLASSES_RO OT, A.Handler$, "", A.FileType
    LS_SaveString HKEY_CLASSES_RO OT, A.Handler$ + SHELL_OPEN_COMM AND,
    "", A.OpenCommand

    End Sub

    ' ############### ############### ############### ############### #
    '
    '
    '
    Public Sub LS_DelAssociati ons(Ext$)
    Dim Handler$

    ' --- Get Handler
    Handler$ = LF_GetString(HK EY_CLASSES_ROOT , Ext$, "")
    ' --- Delete .EXE
    LS_DelSetting HKEY_CLASSES_RO OT, Ext$, CONTENT_TYPE
    LS_DelSetting HKEY_CLASSES_RO OT, Ext$, ""
    ' --- Delete "ExtMgr" sub keys
    LS_DelSetting HKEY_CLASSES_RO OT, Handler$ + SHELL_OPEN_COMM AND, ""
    LS_DelSetting HKEY_CLASSES_RO OT, Handler$, ""
    ' --- Zap the root References
    RegDeleteKey HKEY_CLASSES_RO OT, Ext$
    RegDeleteKey HKEY_CLASSES_RO OT, Handler$

    End Sub

    ' ############### ############### ############### ############### #
    '
    '
    '
    Public Sub RegisterExt(Ext $, _
    Handler$, _
    FileType$, _
    ContentType$, _
    ExeName$)
    Dim A As TPACKET

    A.Ext$ = LF_FormatExt$(E xt$)
    A.Handler = Handler$
    A.FileType = FileType$
    A.ContentType = ContentType$
    A.OpenCommand = ExeName$
    ' Chr$(34) _
    ' + ExeName$ + ".exe" _
    ' + Chr$(34) _
    ' + " " + Chr$(34) + "%1" + Chr$(34)

    Call LS_PutAssociati ons(A)
    ' ---

    End Sub

    Public Sub UnRegisterExt(E xt$)
    Dim S$
    S$ = LF_FormatExt$(E xt$)
    Call LS_DelAssociati ons(S$)
    End Sub

    ' ############### ############### ############### ############### #
    '
    '
    '
    Public Sub GetExtAssociati ons(Ext$, _
    Handler$, _
    FileType$, _
    ContentType$, _
    ExeName$)
    Dim A As TPACKET

    A.Ext = LF_FormatExt$(E xt$)
    Call LS_GetAssociati ons(A)
    Handler$ = A.Handler
    FileType$ = A.FileType$
    ContentType$ = A.ContentType$
    ExeName$ = A.OpenCommand$

    End Sub







    Comment

    • Rick Rothstein [MVP - Visual Basic]

      #3
      Re: Set RTF file handling

      > Is there a way to instruct Windows file-handling[color=blue]
      > - 'Opens with:' - from within VB6? I want to
      > ensure that files with the .rtf (rich text) extension
      > always go to Word Pad, no matter which future
      > computer uses my program.[/color]

      You have already received an answer to your question, but I'm curious... Is
      this program only going to be used on your own computers exclusively? I ask
      because if your program is to be used by others, I would not want to be one
      of those users. If I have already set up an association for Rich Text Files
      to be read in my favorite Rich Text File editor/viewer, why do you think I
      would want your program to automatically change that to WordPad for me? I
      know I wouldn't want it to.

      Rick


      Comment

      • Steve Gerrard

        #4
        Re: Set RTF file handling


        "D & J G" <donandjane@opt usnet.com.au> wrote in message
        news:44003805$0 $30820$afc38c87 @news.optusnet. com.au...
        [color=blue]
        > Is there a way to instruct Windows file-handling - 'Opens with:' - from
        > within VB6? I want to ensure that files with the .rtf (rich text) extension
        > always go to Word Pad, no matter which future computer uses my program.
        >[/color]

        Considering that some future computer may not even have Word Pad, this seems
        like a bad idea.

        Surely you mean that you want *your* program to launch certain .rtf files in
        WordPad? If so, then explicitly launching WordPad and passing it the desired
        file would make more sense:


        Comment

        • D & J G

          #5
          Re: Set RTF file handling


          "Steve Gerrard" <mynamehere@com cast.net> wrote in message
          news:Ob2dnTX5QM pUDJ3ZRVn-vg@comcast.com. ..[color=blue]
          >
          > "D & J G" <donandjane@opt usnet.com.au> wrote in message
          > news:44003805$0 $30820$afc38c87 @news.optusnet. com.au...
          >[color=green]
          >> Is there a way to instruct Windows file-handling - 'Opens with:' - from
          >> within VB6? I want to ensure that files with the .rtf (rich text)
          >> extension always go to Word Pad, no matter which future computer uses my
          >> program.
          >>[/color]
          >
          > Considering that some future computer may not even have Word Pad, this
          > seems like a bad idea.
          >
          > Surely you mean that you want *your* program to launch certain .rtf files
          > in WordPad? If so, then explicitly launching WordPad and passing it the
          > desired file would make more sense:
          >[/color]

          I'm cheating!!
          To simplify printing, the print instruction goes like this:
          Call Shell("write.ex e /p " + App.Path + "\filename.rtf" )
          If the rtf association isn't set, it doesn't work.
          Too simple??


          Comment

          • D & J G

            #6
            Re: Set RTF file handling


            "D & J G" <donandjane@opt usnet.com.au> wrote in message
            news:4400be46$0 $20415$afc38c87 @news.optusnet. com.au...[color=blue]
            >
            > "Steve Gerrard" <mynamehere@com cast.net> wrote in message
            > news:Ob2dnTX5QM pUDJ3ZRVn-vg@comcast.com. ..[color=green]
            >>
            >> "D & J G" <donandjane@opt usnet.com.au> wrote in message
            >> news:44003805$0 $30820$afc38c87 @news.optusnet. com.au...
            >>[color=darkred]
            >>> Is there a way to instruct Windows file-handling - 'Opens with:' - from
            >>> within VB6? I want to ensure that files with the .rtf (rich text)
            >>> extension always go to Word Pad, no matter which future computer uses my
            >>> program.
            >>>[/color]
            >>
            >> Considering that some future computer may not even have Word Pad, this
            >> seems like a bad idea.
            >>
            >> Surely you mean that you want *your* program to launch certain .rtf files
            >> in WordPad? If so, then explicitly launching WordPad and passing it the
            >> desired file would make more sense:
            >>[/color]
            >
            > I'm cheating!!
            > To simplify printing, the print instruction goes like this:
            > Call Shell("write.ex e /p " + App.Path + "\filename.rtf" )
            > If the rtf association isn't set, it doesn't work.
            > Too simple??[/color]
            PS. Call Shell("wordpad. exe /p " + App.Path + "\filename.rtf" ) doesn't work,
            either!


            Comment

            • J French

              #7
              Re: Set RTF file handling

              On Sun, 26 Feb 2006 16:32:12 +1000, "D & J G"
              <donandjane@opt usnet.com.au> wrote:

              <snip>
              [color=blue][color=green]
              >> I'm cheating!!
              >> To simplify printing, the print instruction goes like this:
              >> Call Shell("write.ex e /p " + App.Path + "\filename.rtf" )
              >> If the rtf association isn't set, it doesn't work.
              >> Too simple??[/color]
              >PS. Call Shell("wordpad. exe /p " + App.Path + "\filename.rtf" ) doesn't work,
              >either![/color]

              Why is that ?

              Have you tried giving it the full path for WordPad ?

              You might be able to get that using FindExecutable on: Dummy.WRI

              If you don't get 'wordpad.exe' from FindExecutable then you can look
              for it once and store the location.

              It looks like Write.exe is a little loader for Wordpad.
              While Wordpad.exe is normally in 'Program Files\Accessori es'
              - Write.exe is in the Windows directory, which is in the system's
              'path'

              Comment

              • Steve Gerrard

                #8
                Re: Set RTF file handling


                "D & J G" <donandjane@opt usnet.com.au> wrote in message
                news:44014b65$0 $27464$afc38c87 @news.optusnet. com.au...[color=blue]
                >[color=green]
                >>
                >> I'm cheating!!
                >> To simplify printing, the print instruction goes like this:
                >> Call Shell("write.ex e /p " + App.Path + "\filename.rtf" )
                >> If the rtf association isn't set, it doesn't work.
                >> Too simple??[/color]
                > PS. Call Shell("wordpad. exe /p " + App.Path + "\filename.rtf" ) doesn't work,
                > either!
                >[/color]

                As Jerry said, write.exe is in Windows, and therefore in the environment path,
                while WordPad is somewhere in Program Files (mine is in ...Files\Window s NT\).

                On my machine, .RTF is clearly associated with MS Word, not WordPad. However,
                this prints the specified file, apparently using WordPad:

                Private Sub Command1_Click( )
                Call Shell("write.ex e /p C:\test.rtf")
                End Sub


                Comment

                Working...