Results 1 to 31 of 31

Thread: VB6 Common Dialog Error in Windows 7

Hybrid View

  1. #1
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: VB6 Common Dialog Error in Windows 7

    Try without the control. Delete the common dialog control and remove it from the components list. Then add a module named something like basCommonDlg.bas and copy this code to it:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Enum FlagsEnum
    4.     feOpen
    5.     feSaveAs
    6. End Enum
    7.  
    8. Private Type OPENFILENAME
    9.     lStructSize As Long
    10.     hwndOwner As Long
    11.     hInstance As Long
    12.     lpstrFilter As String
    13.     lpstrCustomFilter As String
    14.     nMaxCustFilter As Long
    15.     nFilterIndex As Long
    16.     lpstrFile As String
    17.     nMaxFile As Long
    18.     lpstrFileTitle As String
    19.     nMaxFileTitle As Long
    20.     lpstrInitialDir As String
    21.     lpstrTitle As String
    22.     Flags As Long
    23.     nFileOffset As Integer
    24.     nFileExtension As Integer
    25.     lpstrDefExt As String
    26.     lCustData As Long
    27.     lpfnHook As Long
    28.     lpTemplateName As String
    29. End Type
    30.  
    31. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    32. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
    33.  
    34. ' Common Dialog - Open
    35. Public Function ShowOpenDialog(pstrInitialPath As String, pstrFilter As String, pstrDefaultExt As String) As String
    36.     Dim typFileName As OPENFILENAME
    37.            
    38.     typFileName = GetStructure(pstrInitialPath, "", pstrFilter, pstrDefaultExt, feOpen)
    39.     If GetOpenFileName(typFileName) Then ShowOpenDialog = Left$(typFileName.lpstrFile, InStr(typFileName.lpstrFile, Chr$(0)) - 1)
    40. End Function
    41.  
    42. ' Common Dialog - SaveAs
    43. Public Function ShowSaveAsDialog(pstrInitialPath As String, pstrFile As String, pstrFilter As String, pstrDefaultExt As String) As String
    44.     Dim typFileName As OPENFILENAME
    45.    
    46.     typFileName = GetStructure(pstrInitialPath, pstrFile, pstrFilter, pstrDefaultExt, feSaveAs)
    47.     If GetSaveFileName(typFileName) Then ShowSaveAsDialog = Left$(typFileName.lpstrFile, InStr(typFileName.lpstrFile, Chr$(0)) - 1)
    48. End Function
    49.  
    50. Private Function GetStructure(pstrPath As String, pstrFile As String, pstrFilter As String, pstrDefaultExt As String, penFlags As FlagsEnum) As OPENFILENAME
    51.     Const OFN_FILEMUSTEXIST = &H1000
    52.     Const OFN_PATHMUSTEXIST = &H800
    53.     Const OFN_HIDEREADONLY = &H4
    54.     Const OFN_LONGNAMES = &H200000
    55.     Const OFN_OVERWRITEPROMPT = &H2
    56.     Const OF_WRITE = &H1
    57.     Const MAX_PATH = 260
    58.     Dim frm As Form
    59.  
    60.     With GetStructure
    61.         .lStructSize = Len(GetStructure)
    62.         ' Get any form's window handle
    63.         For Each frm In Forms
    64.             Exit For
    65.         Next
    66.         .hwndOwner = frm.hWnd
    67.         Set frm = Nothing
    68.         .hInstance = App.hInstance
    69.         .lpstrFilter = Replace(pstrFilter, "|", Chr(0)) & Chr(0)
    70.         .nMaxFile = MAX_PATH + 1
    71.         .nMaxFileTitle = MAX_PATH + 1
    72.         .lpstrFileTitle = Space(MAX_PATH)
    73.         .lpstrInitialDir = pstrPath
    74.         .lpstrDefExt = pstrDefaultExt
    75.         Select Case penFlags
    76.             Case feOpen
    77.                 .lpstrTitle = "Open"
    78.                 .lpstrFile = Space(MAX_PATH)
    79.                 .Flags = OFN_FILEMUSTEXIST + OFN_HIDEREADONLY + OFN_LONGNAMES
    80.             Case feSaveAs
    81.                 .lpstrTitle = "Save As"
    82.                 .lpstrFile = pstrFile & Space$(MAX_PATH - Len(pstrFile))
    83.                 .Flags = OFN_PATHMUSTEXIST + OFN_HIDEREADONLY + OFN_LONGNAMES + OF_WRITE ' + OFN_OVERWRITEPROMPT
    84.         End Select
    85.     End With
    86. End Function
    Sample usage: (Note that the code requires any one form to be open, meaning this doesn't work from the debug window.)
    Code:
    Private Sub Command1_Click()
        MsgBox ShowOpenDialog(App.Path, "All Files|*.*", "*.*")
        MsgBox ShowSaveAsDialog(App.Path, "untitled.txt", "All Files|*.*", "*.*")
    End Sub

  2. #2

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    I changed the code to report errors and use the API code but the error still exists. I am thinking it might have something to do with Windows 7 settings in general however and may not, in fact, be a problem with the code in particular. But I am uncertain.

  3. #3
    New Member
    Join Date
    Mar 2010
    Posts
    1

    Re: VB6 Common Dialog Error in Windows 7

    Ellis, Your replacement common dialog code helped me greatly. I am using VB6 on Win7x64. (Because I have to) In particular the common dialog control was giving me problems, but this works fine. Awesome.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: VB6 Common Dialog Error in Windows 7

    Quote Originally Posted by Ellis Dee View Post
    Code:
    Private Sub Command1_Click()
        MsgBox ShowOpenDialog(App.Path, "All Files|*.*", "*.*")
        MsgBox ShowSaveAsDialog(App.Path, "untitled.txt", "All Files|*.*", "*.*")
    End Sub
    How do I modify the code so I can refer to the file name?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    New Member
    Join Date
    Mar 2011
    Posts
    1

    Re: VB6 Common Dialog Error in Windows 7

    Hi ,

    I have applied this solution of using API code given in my application for OPEN Dialog box and it worked , However , when Open dialog box poped up , if User clicked on Cancel Button instead of Open button , it will display error , So could you please let me know how Cancel button/code can be handled on Form so its gracefully return back to Form , instead of error when pressed cancel button on OPEN dialog box.

    Thanks
    A_Nirwal



    Quote Originally Posted by Ellis Dee View Post
    Try without the control. Delete the common dialog control and remove it from the components list. Then add a module named something like basCommonDlg.bas and copy this code to it:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Enum FlagsEnum
    4.     feOpen
    5.     feSaveAs
    6. End Enum
    7.  
    8. Private Type OPENFILENAME
    9.     lStructSize As Long
    10.     hwndOwner As Long
    11.     hInstance As Long
    12.     lpstrFilter As String
    13.     lpstrCustomFilter As String
    14.     nMaxCustFilter As Long
    15.     nFilterIndex As Long
    16.     lpstrFile As String
    17.     nMaxFile As Long
    18.     lpstrFileTitle As String
    19.     nMaxFileTitle As Long
    20.     lpstrInitialDir As String
    21.     lpstrTitle As String
    22.     Flags As Long
    23.     nFileOffset As Integer
    24.     nFileExtension As Integer
    25.     lpstrDefExt As String
    26.     lCustData As Long
    27.     lpfnHook As Long
    28.     lpTemplateName As String
    29. End Type
    30.  
    31. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    32. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
    33.  
    34. ' Common Dialog - Open
    35. Public Function ShowOpenDialog(pstrInitialPath As String, pstrFilter As String, pstrDefaultExt As String) As String
    36.     Dim typFileName As OPENFILENAME
    37.            
    38.     typFileName = GetStructure(pstrInitialPath, "", pstrFilter, pstrDefaultExt, feOpen)
    39.     If GetOpenFileName(typFileName) Then ShowOpenDialog = Left$(typFileName.lpstrFile, InStr(typFileName.lpstrFile, Chr$(0)) - 1)
    40. End Function
    41.  
    42. ' Common Dialog - SaveAs
    43. Public Function ShowSaveAsDialog(pstrInitialPath As String, pstrFile As String, pstrFilter As String, pstrDefaultExt As String) As String
    44.     Dim typFileName As OPENFILENAME
    45.    
    46.     typFileName = GetStructure(pstrInitialPath, pstrFile, pstrFilter, pstrDefaultExt, feSaveAs)
    47.     If GetSaveFileName(typFileName) Then ShowSaveAsDialog = Left$(typFileName.lpstrFile, InStr(typFileName.lpstrFile, Chr$(0)) - 1)
    48. End Function
    49.  
    50. Private Function GetStructure(pstrPath As String, pstrFile As String, pstrFilter As String, pstrDefaultExt As String, penFlags As FlagsEnum) As OPENFILENAME
    51.     Const OFN_FILEMUSTEXIST = &H1000
    52.     Const OFN_PATHMUSTEXIST = &H800
    53.     Const OFN_HIDEREADONLY = &H4
    54.     Const OFN_LONGNAMES = &H200000
    55.     Const OFN_OVERWRITEPROMPT = &H2
    56.     Const OF_WRITE = &H1
    57.     Const MAX_PATH = 260
    58.     Dim frm As Form
    59.  
    60.     With GetStructure
    61.         .lStructSize = Len(GetStructure)
    62.         ' Get any form's window handle
    63.         For Each frm In Forms
    64.             Exit For
    65.         Next
    66.         .hwndOwner = frm.hWnd
    67.         Set frm = Nothing
    68.         .hInstance = App.hInstance
    69.         .lpstrFilter = Replace(pstrFilter, "|", Chr(0)) & Chr(0)
    70.         .nMaxFile = MAX_PATH + 1
    71.         .nMaxFileTitle = MAX_PATH + 1
    72.         .lpstrFileTitle = Space(MAX_PATH)
    73.         .lpstrInitialDir = pstrPath
    74.         .lpstrDefExt = pstrDefaultExt
    75.         Select Case penFlags
    76.             Case feOpen
    77.                 .lpstrTitle = "Open"
    78.                 .lpstrFile = Space(MAX_PATH)
    79.                 .Flags = OFN_FILEMUSTEXIST + OFN_HIDEREADONLY + OFN_LONGNAMES
    80.             Case feSaveAs
    81.                 .lpstrTitle = "Save As"
    82.                 .lpstrFile = pstrFile & Space$(MAX_PATH - Len(pstrFile))
    83.                 .Flags = OFN_PATHMUSTEXIST + OFN_HIDEREADONLY + OFN_LONGNAMES + OF_WRITE ' + OFN_OVERWRITEPROMPT
    84.         End Select
    85.     End With
    86. End Function
    Sample usage: (Note that the code requires any one form to be open, meaning this doesn't work from the debug window.)
    Code:
    Private Sub Command1_Click()
        MsgBox ShowOpenDialog(App.Path, "All Files|*.*", "*.*")
        MsgBox ShowSaveAsDialog(App.Path, "untitled.txt", "All Files|*.*", "*.*")
    End Sub

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