Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
Go Back  Xtreme Visual Basic Talk > > > Open Dialog using GetOpenFileName API


Reply
 
Thread Tools Display Modes
  #1  
Old 08-31-2016, 08:55 AM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,842
Default Open Dialog using GetOpenFileName API


I have a VB6 project in a folder called DialogTest which has one subfolder called "Subfolder".
In this folder I have placed some random text files.

The test project simply opens a dialog so user can select a text file (the initial directory is set to Subfolder directory),
then creates and saves a txt file called Test1 in the project folder (ie the app.path folder).

If the user doesn't select any file in the dialog, file is saved correctly.

If the user selects a txt file in the Subfolder directory, the program incorrectly saves Test1.txt in the Subfolder directory, not the app.path directory.

How can I correct this?


This is my complete code for the test project:
Code:
Option Explicit

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type


Private Sub Command1_Click()
    
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    Dim sFilter As String
    
    OpenFile.lStructSize = Len(OpenFile)
    OpenFile.hwndOwner = Form1.hWnd
    OpenFile.hInstance = App.hInstance
    sFilter = "Text Files (*.txt)" & Chr(0) & "*.TXT" & Chr(0)
    OpenFile.lpstrFilter = sFilter
    OpenFile.nFilterIndex = 1
    OpenFile.lpstrFile = String(257, 0)
    OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
    OpenFile.lpstrFileTitle = OpenFile.lpstrFile
    OpenFile.nMaxFileTitle = OpenFile.nMaxFile
    OpenFile.lpstrInitialDir = App.Path & "\Subfolder"
    OpenFile.lpstrTitle = "Open"
    OpenFile.flags = 0
    lReturn = GetOpenFileName(OpenFile)
    
    If lReturn = 0 Then
        MsgBox "User pressed Cancel Button"
    Else
        MsgBox "User opened " & Trim(OpenFile.lpstrFile)
    End If
    
    Open "Test1.txt" For Output As #1
    Print #1, "abcd"
    Close #1
    
End Sub
Reply With Quote
  #2  
Old 08-31-2016, 11:58 AM
dilettante's Avatar
dilettanteOpen Dialog using GetOpenFileName API dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,517
Default

The current directory can be changed by the dialog. You either need to cache it before the dialog and change it back after, or use an absolute path when creating the file instead of creating it in the current directory.
Reply With Quote
  #3  
Old 08-31-2016, 01:17 PM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,842
Default

OK thanks,

How about a call to ChDir (change directory) to App.Path, which is what it was before the Dialog was called.

This appears to work.
Any drawbacks by doing it this way?

Code:
Private Sub Command1_Click()
    
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    Dim sFilter As String
    
    OpenFile.lStructSize = Len(OpenFile)
    OpenFile.hwndOwner = Form1.hWnd
    OpenFile.hInstance = App.hInstance
    sFilter = "Text Files (*.txt)" & Chr(0) & "*.TXT" & Chr(0)
    OpenFile.lpstrFilter = sFilter
    OpenFile.nFilterIndex = 1
    OpenFile.lpstrFile = String(257, 0)
    OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
    OpenFile.lpstrFileTitle = OpenFile.lpstrFile
    OpenFile.nMaxFileTitle = OpenFile.nMaxFile
    OpenFile.lpstrInitialDir = App.Path & "\Subfolder"
    OpenFile.lpstrTitle = "Open"
    OpenFile.flags = 0
    lReturn = GetOpenFileName(OpenFile)
    
    If lReturn = 0 Then
        MsgBox "User pressed Cancel Button"
    Else
        MsgBox "User opened " & Trim(OpenFile.lpstrFile)
    End If
    
    'change current directory back like this?
    Call ChDir(App.Path)
    MsgBox App.Path
    
    Open "Test1.txt" For Output As #1
    Print #1, "abcd"
    Close #1
    
End Sub
Reply With Quote
  #4  
Old 08-31-2016, 03:19 PM
Cerian Knight's Avatar
Cerian KnightOpen Dialog using GetOpenFileName API Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,169
Default

That would work, but is no better than:
Code:
Open App.Path & "\Test1.txt" For Output As #1 'will work even if added '\' is redundant (e.g., 'C:\' for root case exception)
... since App.Path (and CurDir) will reflect whatever the last ChDir set it to.

Therefore, if you manipulate the path(s) elsewhere with ChDir, you might want to store App.Path in a variable at application launch, or avoid ChDir altogether.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').

Last edited by Cerian Knight; 09-01-2016 at 02:45 PM. Reason: corrected concatenation
Reply With Quote
  #5  
Old 09-01-2016, 05:44 AM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,842
Default

Thanks,

I understand now, and all works fine.
Reply With Quote
  #6  
Old 09-01-2016, 03:26 PM
Cerian Knight's Avatar
Cerian KnightOpen Dialog using GetOpenFileName API Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,169
Default

I corrected my above code, just for posterity.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #7  
Old 09-01-2016, 06:30 PM
dilettante's Avatar
dilettanteOpen Dialog using GetOpenFileName API dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,517
Default

Something else to keep in mind is that App.Path and the initial CD can be quite different. That usually happens because the program was started from a shortcut or by a file association. So you might want to cache CD instead of App.Path.
Reply With Quote
  #8  
Old 09-02-2016, 05:57 AM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,842
Default

Good to know, thanks...

So you mean say in FormLoad event do something like

gAppDir = CurrDir

and use gAppFir from that point forward?
Reply With Quote
  #9  
Old 09-02-2016, 02:40 PM
dilettante's Avatar
dilettanteOpen Dialog using GetOpenFileName API dilettante is offline
Underclocked lifestyle

Forum Leader
* Guru *
 
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,517
Default

It depends on what you are trying to do.

Trying to give a more specific answer raises lots of questions, and would probably veer off into discussions of UAC and per-user vs. per-machine data locations.

But for throwaway programs that are never formally installed it hardly matters. Those should not be assigned file associations and are not really safe to run via shortcuts anyway.

There are no "one size fits all" answers.
Reply With Quote
Reply

Tags
string, subfolder, directory, file, user, folder, project, openfilename, dialog, app.path, private, lreturn, dim, sfilter, text, called, openfile.nmaxfile, msgbox, openfile.lpstrfile, integer, chr0, test1.txt, saves, txt, select


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
 
Open Dialog using GetOpenFileName API
Open Dialog using GetOpenFileName API
 
-->