Results 1 to 31 of 31

Thread: VB6 Common Dialog Error in Windows 7

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    VB6 Common Dialog Error in Windows 7

    When using the CommonDialog control, to open files the program EXE is being locked and the file cannot be moved, deleted, or changed once the program has been closed. Trying to relaunch the program also results in an error. The only way to relaunch the program is the restart the machine. This only happens in Windows 7 but I cannot find anyone else that has experienced a similar problem.

    The code is fairly old, still using the Action Property of the CommonDialog but even switching to use the newer ShowOpen method does not fix this. It does not mater if I actually open a file or not. Even selecting Cancel on the screen will still cause the EXE to be locked.

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: VB6 Common Dialog Error in Windows 7

    Welcome to the forums.....

    Have you tried Run as Administrator option ...???

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    I have but the same error occurs. It's rather strange. There has been no problem in XP or Vista with this program. The code used for the CommonDialog follows every example I've ever seen for it:
    dlgFile.Flags = OFN_HIDEREADONLY
    dlgFile.Filter = "Inventory Files (*.inv)|*.inv|Access Database (*.mdb)|*.mdb|All Files (*.*)|*.*"
    dlgFile.DefaultExt = "inv"
    dlgFile.FileName = ""
    dlgFile.Initdir = gvsInventory$
    dlgFile.Dialogtitle = "Open an Inventory File"
    dlgFile.Action = DLG_FILE_OPEN
    If dlgFile.FileName = "" Then gvnTerminate = True: Exit Sub
    ChDrive Left$(gvsProgram$, 2)
    ChDir Mid$(gvsProgram$, 3)
    If Err <> 0 Then gvnTerminate = True: Exit Sub
    On Error GoTo 0
    Screen.MousePointer = vbHourglass
    OpenInventory dlgFile.FileName
    gvnTerminate% = True
    Screen.MousePointer = vbDefault
    Me.SetFocus


    It definately happens within the CommonDialog, I know, because I have opened the files in other ways without using the CommonDialog, such as hardcoding file names and I have also launched the CommonDialog without opening a file and still have the error.

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: VB6 Common Dialog Error in Windows 7

    Could you try commenting out these two lines:
    Code:
    'ChDrive Left$(gvsProgram$, 2)
    'ChDir Mid$(gvsProgram$, 3)

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    One of many options I've already tried. I also tried updating code to use the "ShowOpen" property as opposed to the "Action" property. Setting the initial directory to another directory after returning from the CommonDialog. Using ChDir to point to another directory after returning from the CommonDialog. And using the API commonDialog as opposed to the VB6 component.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6 Common Dialog Error in Windows 7

    It looks like you are using On Error Resume Next in a way that is not apt, even if you weren't having this kind of issue.

    As you are having an issue, having it in there is the worst thing you can do - because it is explicitly hiding any errors from you, and there is a very strong chance that you are getting errors.


    In addition to that, you should check what gvsInventory$ contains, and whether or not it points to a valid folder.

  7. #7
    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

  8. #8

    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.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6 Common Dialog Error in Windows 7

    I'm on Windows 7, and I've just tested your code (up as far as the If dlgFile.FileName line) with an apt .InitDir for my machine, and values for OFN_HIDEREADONLY and DLG_FILE_OPEN (which I hope you have valid constants for).

    There was no kind of issue at all, and it all worked as it should have.

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    Still getting the error. I'm not debugging on the Windows 7 machine, VisualStudio is not installed there, so I can't determine the error in that way. I am debugging and designing on an XP machine. The Windows 7 machine is only getting the finalized EXE to test.

    Researching a little bit more of my problem I have determined this: When first opened the program runs fine. Whenever the common dialog is launched for Open or Save As something happens. Then when I go to close the program it seems to close normally but the process remains in the system, this does not happen with the same EXE on XP. Then whenever I go to relaunch the program a check is performed to see if the program is already running to avoid opening to separate instances of the program. When doing this the existing process is seen and is made active. That's where the error occurs.

  11. #11
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: VB6 Common Dialog Error in Windows 7

    Back to the common dialog control;
    Have you copied comdlg32.ocx to the program folder or system32 folder?
    Delete it. They just clutter threads anyway.

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    It's in the system32 folder.

  13. #13
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 Common Dialog Error in Windows 7

    Quote Originally Posted by BWisinger View Post
    ...Researching a little bit more of my problem I have determined this: When first opened the program runs fine. Whenever the common dialog is launched for Open or Save As something happens. Then when I go to close the program it seems to close normally but the process remains in the system, this does not happen with the same EXE on XP. Then whenever I go to relaunch the program a check is performed to see if the program is already running to avoid opening to separate instances of the program. When doing this the existing process is seen and is made active. That's where the error occurs.
    This, I would say, is a strong indication as the reason of your initial problem reported in post #1. If it is still running, obviously you can't rename, delete or move it.

    What does your form's Unload and/or QueryUnload code look like?
    Can you display the entire routine that has the code in post #3?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    That actually is the full routine, minus the sub XXXX ... end sub.

    As for the Unload and QueryUnload, the Unload is empty and QueryUnload has:
    Code:
    If Not ExitRoutine%() Then Cancel = True
    '
    Dim FormX As Form
    For Each FormX In Forms
      If FormX.hwnd <> Me.hwnd Then
        MsgBox FormX.Name & " is still loaded"
        Unload FormX
      End If
    Next
    
    If (tmrClock.Enabled = True) Then
        MsgBox "tmrClock.enabled is TRUE"
    End If
    If (tmrSketchBlock.Enabled = True) Then
        MsgBox "tmrSketchBlock.enabled is TRUE"
    End If
    Some of this recently added for testing and trying new ways to correct the error.

    ExitRoutine is a function that will close the database files used by the program:
    Code:
    If Not gvnTerminate% Then
        MsgBox DynamicText("BAS-MN10-006")
        ExitRoutine% = False
        Exit Function
    End If
    '
    If MsgBox(DynamicText("BAS-MN10-007"), vbYesNo + vbQuestion) = vbNo Then
        ExitRoutine% = False
        Exit Function
    End If
    '
    gvbExitFlag% = True
    '
    If PanelText("Structure") <> "" Then
        CloseFile True, False
    End If
    '
    UnloadForms
    '
    SaveFileList
    
    '
    'For lvnCount = 0 To frmPrecisionPlus.Crpt1.Count - 1
    '  Unload frmPrecisionPlus.Crpt1(lvnCount).object
    'Next
    '
    If Len(PanelText("Inventory")) > 0 Then
        CloseInventory
    End If
    '
    On Error Resume Next
    '
    tbRange(0).Close
    tbRange(1).Close
    dbRange.Close
    Set dbRange = Nothing
    '
    dbsBOM.Close
    Set dbsBOM = Nothing
    dbFrameDesn.Close
    Set dbFrameDesn = Nothing
    ReportDB.Close
    Set ReportDB = Nothing
    gdbLookupDB.Close
    Set gdbLookupDB = Nothing
    
    Set dbInventory = Nothing
    
    '
    frmPrecisionPlus.tmrClock.Enabled = False
    '
    ExitRoutine% = True

  15. #15
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 Common Dialog Error in Windows 7

    What is that gvnTerminate flag you are setting? Do you have some Do:Loop or For:Next loop going on that is using that variable to exit? If you do, can you post the code from the end of that loop to the end of the routine containing that loop? If not, how is gvnTerminate being used.

    What may be happening and I don't have all the details to make a better guesstimate, is that your form may actually be re-loaded due to a reference to one of its properties or a control on the form, after Query_Unload enters, but before it exits. Do Loops & For Loops that terminate due to a global variable are primary places to look. When the loop terminates, the form is unloaded, but before the routine containing the loop terminates, it checks on one of its control's properties, thus reloading the form.

    Oh and a question on your logic in your query_unload routine....
    Even when you set Cancel = True, you are still unloading forms? That doesn't sound logical to me?
    Also, you are calling the ExitRoutine which has a call to UnloadForms and SaveFileList. If you are unloading a form that SaveFileList references, then the form will be reloaded & that's your problem. Same applies to CloseInventory.

    This is yet another potential problem. I assume UnloadForms unloads all forms. But at the end of the routine, you are calling this line: "frmPrecisionPlus.tmrClock.Enabled = False" which will re-load frmPrecisionPlus just to set the tmrClock.Enabled property. Never call a form's property or control after it has been unloaded or it will reload & cause a scenario like you are experiencing.

    Recommend unloading forms in one place, either in main form's QueryUnload or Unload as you seem to be doing, or in a cleanup routine like ExitRoutine, but not in both places.
    Last edited by LaVolpe; Mar 2nd, 2010 at 08:15 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6 Common Dialog Error in Windows 7

    In addition to those points, I recommend that the first line of the actual unloading process (after any "cancel"/"exit") for each form is to disable the timer(s), as they have a tendency to keep the form open.

    You can find much more explanation (and example code) of potential issues in the article How should I close my form/program/class? from our Classic VB FAQs (in the FAQ forum)

  17. #17

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    I've tried many different things and still getting the error. I've now also created an entirely new project with a single form with two buttons, a label and a commonDialog object with this code:

    Code:
    Option Explicit
    
    
    
    Private Sub cmdOpen_Click()
    
    comdlg.Action = 1
    
    Label1.Caption = "comdlg.FileName = " & comdlg.FileName
    
    End Sub
    
    
    
    Private Sub cmdExit_Click()
    
    Unload Me
    
    
    End Sub
    And still when closing the program it remains in the processes.

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6 Common Dialog Error in Windows 7

    I just tried that, and it does not have the problem... it seems that somehow you have got a corrupted common dialog control.

    Is it any better if you use the code version (such as in post #7) instead?

  19. #19

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    No, I've tried that too with no success. And the commdlg32.ocx is the same as one off of an XP machine where it runs fine. I copied it over as part of trying to determine if the .ocx had some error or was out-of-date.

  20. #20

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    Quote Originally Posted by si_the_geek View Post
    I just tried that, and it does not have the problem... it seems that somehow you have got a corrupted common dialog control.

    Is it any better if you use the code version (such as in post #7) instead?
    You said you were running Windows 7 as well. Which version? The machine getting the error is running Windows 7 Home Premium 64-bit with an AMD Athlon Processor TF-20.

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB6 Common Dialog Error in Windows 7

    I'm using Win7 Ultimate 64-bit, which should be exactly the same as far as this is concerned.

    I suspect that copying the commdlg32.ocx was a bad idea, as it has various dependencies - some of which are likely to be specific to the version of Windows (and therefore you might have broken something). I haven't distributed it for many years, so I am not aware of the correct way to do it.

  22. #22

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: VB6 Common Dialog Error in Windows 7

    I did run RegSvr32 on the files after copying but they are now returned to the files that were there originally, but as the error still occurred even then the change back doesn't really matter.

  23. #23
    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.

  24. #24
    Junior Member
    Join Date
    Mar 2010
    Location
    Netherlands
    Posts
    20

    Re: VB6 Common Dialog Error in Windows 7

    Hi BWisinger, I experience the same problem with the navigating. I too program on a pc with XP and the Exe on Windows 7. Could you solve it yet? Or maybe Windows 7 needs an update.

    Another thing I thought of in connection with this problem: When installing VB the installer asked years ago if I remember it right, if you want to install network tools to. Because I have no network I did not do that. Could this maybe have influence on the working with commondialog1.save in Windows 7?

  25. #25
    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

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

    Re: VB6 Common Dialog Error in Windows 7

    You mean like:

    Dim strFile As String

    strFile = ShowOpenDialog(App.Path, "All Files|*.*", "*.*")
    MsgBox strFile

    ?

  27. #27
    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
    You mean like:

    Dim strFile As String

    strFile = ShowOpenDialog(App.Path, "All Files|*.*", "*.*")
    MsgBox strFile

    ?
    Yeah,

    I put:

    Code:
    txtFile.Text = ShowOpenDialog(App.Path, "All Files|*.*", "*.*")
    and that worked! I overlooked the Msgbox code you had in the code.

    Edit:

    How do I test if cancel was clicked?

    Never mind! I solved the problem using:

    Code:
    Private Sub cmdAttach_Click()
            Dim sFile As String
        sFile = ShowOpenDialog(App.Path, "All Files|*.*", "*.*")
        If Not sFile <> "" Then
        Exit Sub
        Else
        txtFile.Text = sFile
       LstFiles.AddItem txtFile.Text
       lblCount.Caption = lblCount.Caption + 1
        End If
    End Sub
    Last edited by Nightwalker83; Jul 10th, 2010 at 12:35 AM. Reason: Adding more!
    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

  28. #28
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: VB6 Common Dialog Error in Windows 7

    Code:
    Private Sub cmdAttach_Click()
        Dim sFile As String
        sFile = ShowOpenDialog(App.Path, "All Files|*.*", "*.*")
        
        If sFile <> "" Then
            txtFile.Text = sFile
            LstFiles.AddItem txtFile.Text
            lblCount.Caption = val(lblCount.Caption) + 1
        End If
    End Sub
    .....

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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

    Re: VB6 Common Dialog Error in Windows 7

    That is a bit smaller!
    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

  30. #30
    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

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

    Re: VB6 Common Dialog Error in Windows 7

    Try:

    vb Code:
    1. Dim sFile As String
    2.     sFile = ShowOpenDialog(App.Path, "All Files|*.*", "*.*")
    3.    
    4.     If sFile <> "" Then
    5.  'Code here
    6.     End If
    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

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