Results 1 to 2 of 2

Thread: Joacim Andersson's CCommonDialogs' Extension Question (Resolved)

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Resolved Joacim Andersson's CCommonDialogs' Extension Question (Resolved)

    Alright, I am using the wonderful class provided by Joacim to open and save (I mean, to show the dialogs for opening and saving) the files of an application I developed.

    What I noticed is that sometimes the files are saved without any extension... You could say "Just add the extension if it is not there and problem solved" but my real problem is that I have like 3 different extensions the user can select, and thus the way I save the file has to change.

    Could someone help me? Is there a way to know which was the extension the user selected in the dialog?

    Thanks in advance.
    Last edited by Tec-Nico; Oct 3rd, 2004 at 01:35 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  2. #2

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Nevermind...

    If you have this problem here is how I solved it:

    1.- You have to add a member variable in Joacim's Class under the comment "'local variable(s) to hold property value(s)":

    VB Code:
    1. Private m_FilterIndex As Long
    2.- You have to add a Let Property and a Get Property named "FilterIndex", it would be better if it was under the "Filter" Let and Get Properties:
    VB Code:
    1. Public Property Get FilterIndex() As Long
    2.  FilterIndex = m_FilterIndex
    3. End Property
    4.  
    5. Public Property Let FilterIndex(ByVal lFilterIndex As Long)
    6.  m_FilterIndex = lFilterIndex
    7. End Property
    3.- You have to edit the ShowSave function so it would be like this:
    VB Code:
    1. Public Function ShowSave() As Boolean
    2.  Dim ofn As OPENFILENAME
    3.  Dim nRetVal As Long
    4.  
    5.  If m_FilterIndex = 0 Then m_FilterIndex = 1 'Added for FilterIndex
    6.  
    7.  With ofn
    8.   .lStructSize = Len(ofn)
    9.   .hwndOwner = m_Owner.hWnd
    10.   .hInstance = App.hInstance
    11.   .lpstrFilter = VBA.Replace$(m_Filter, "|", Chr$(0)) & Chr$(0)
    12.   .nFilterIndex = m_FilterIndex 'Added for FilterIndex
    13.   .nMaxFile = MAX_PATH + 1
    14.   .nMaxFileTitle = MAX_PATH + 1
    15.   .lpstrFileTitle = Space$(MAX_PATH)
    16.   .lpstrInitialDir = CurDir$
    17.   .lpstrFile = Me.FileName & Space$(MAX_PATH - Len(Me.FileName))
    18.   .lpstrTitle = m_Title
    19.   .flags = &H800 + &H4 + &H200000 + &H2 'PathMustExist + HideReadOnly + LongNames + OverwritePrompt
    20.   .lpstrDefExt = m_DefExt
    21.  End With
    22.  nRetVal = GetSaveFileName(ofn)
    23.  If nRetVal Then
    24.   m_FileTitle = ofn.lpstrFileTitle
    25.   m_FileName = ofn.lpstrFile
    26.   m_Path = Left$(m_FileName, InStrRev(m_FileName, "\") - 1)
    27.   m_FilterIndex = ofn.nFilterIndex 'Added for FilterIndex
    28.  End If
    29.  ShowSave = (nRetVal > 0)
    30. End Function
    4.- You have to edit the ShowOpen as well:
    VB Code:
    1. Public Function ShowOpen() As Boolean
    2.  Dim ofn As OPENFILENAME
    3.  Dim nRetVal As Long
    4.  
    5.  If m_FilterIndex = 0 Then m_FilterIndex = 1 'Added For FilterIndex
    6.  
    7.  With ofn
    8.   .lStructSize = Len(ofn)
    9.   .hwndOwner = m_Owner.hWnd
    10.   .hInstance = App.hInstance
    11.   .lpstrFilter = VBA.Replace$(m_Filter, "|", Chr$(0)) & Chr$(0)
    12.   .nMaxFile = MAX_PATH + 1
    13.   .nMaxFileTitle = MAX_PATH + 1
    14.   .nFilterIndex = m_FilterIndex 'Added For FilterIndex
    15.   .lpstrFileTitle = Space$(MAX_PATH)
    16.   .lpstrInitialDir = CurDir$ 'Manavo's
    17.   .lpstrFile = Me.FileName & Space$(MAX_PATH - Len(Me.FileName))
    18.   .lpstrTitle = m_Title
    19.   .flags = &H1000 + &H4 + &H200000 'FileMustExist + HideReadOnly + LongNames
    20.   .lpstrDefExt = m_DefExt
    21.  End With
    22.  
    23.  nRetVal = GetOpenFileName(ofn)
    24.  If nRetVal Then
    25.   m_FileTitle = ofn.lpstrFileTitle
    26.   m_FileName = Left$(ofn.lpstrFile, InStr(ofn.lpstrFile, Chr$(0)) - 1)
    27.   m_Path = Left$(m_FileName, InStrRev(m_FileName, "\") - 1)
    28.   m_FilterIndex = ofn.nFilterIndex 'Added For FilterIndex
    29.  End If
    30.  
    31.  ShowOpen = (nRetVal > 0)
    32. End Function

    And that's it. Now you can use the FilterIndex property to know which was the filter they chose.

    Now, regarding the problem of saving the file without the extension, you have to do this trick when you call the ShowSave function:

    VB Code:
    1. Dim cmnDlg As CCommonDialogs
    2.  Set cmnDlg = New CCommonDialogs
    3.  
    4.  With cmnDlg
    5.   .Init Me
    6.  
    7.   .Path = App.Path
    8.   .Filter = "HTML Document (*.htm) | *.htm| Rich Text Format (*.rtf)| *.rtf"
    9.   .FileName = ""
    10.  
    11.   .DefExt = ".force" 'Force the dialog to use the extensions.
    12.  
    13.  If .ShowSave Then
    14.   'Your Code here
    15.  End If
    16.  End With
    17.  
    18.  Set cmnDlg = Nothing
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

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