Saturday, February 19, 2011

Vb6: Selecting a file to later be used via a browse button

Here's my code (note that this was given by a friend):

Private Sub Browse_Click()
   Dim textfile As String
   textfile = Space(255)
   GetFileNameFromBrowseW Me.hWnd, StrPtr(sSave), 255, StrPtr("c:\"), 
      StrPtr("txt"), StrPtr("Apps (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) +
      "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), StrPtr("Select File")
      Text1 = Left$(textfile, lstrlen(textfile))
End Sub

Basically later on I edit the text file selected so later I call it just by using textfile in my function. However I get a path not found so I feel like I'm doing something wrong. Thanks in advance.

Edit: All I want to do is select a text file, then later be able to call it and use it.

From stackoverflow
  • Maybe sSave contains a pathname, but textfile contains 255 spaces.

  • Isn't this functionality provided by "Common Dialog Controls" in VB6?

    My VB6 is a little rusty but basic dialog to choose a file is provided already.
    Tools -> Controls -> Microsoft Common Dialog Controls v....

    Also, your call to GetFileNameFromBrowseW doesn't include reference the variable - textfile

  • Replace sSave with textfile. When you later need to refer to the file picked, use Text1 (presumably a VB textbox control so Text1 alone is implicitly calling Text1.Text, .Text being the VB.Textbox's default member).

  • As shahkalpesh mentioned, you can access this functionality simply using a standard COM library.

    In VB6, add the component:

    • Project > Components
    • On the Controls tab, choose Microsoft Common Dialog Control 6.0 (SP6)

    Now on your form, add the new Common Dialog control from the toolbox

    In code, you need:

    CommonDialog.Filter = "Apps (*.txt)|*.txt|All files (*.*)|*.*"
    CommonDialog.DefaultExt = "txt"
    CommonDialog.DialogTitle = "Select File"
    CommonDialog.ShowOpen
    
    'The FileName property gives you the variable you need to use
    MsgBox CommonDialog.FileName
    
  • From here

    I found this code and ran it.

    Private Const VER_PLATFORM_WIN32_NT = 2
    Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128
    End Type
    Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
    Private Declare Function GetFileNameFromBrowseW Lib "shell32" Alias "#63" (ByVal hwndOwner As Long, ByVal lpstrFile As Long, ByVal nMaxFile As Long, ByVal lpstrInitialDir As Long, ByVal lpstrDefExt As Long, ByVal lpstrFilter As Long, ByVal lpstrTitle As Long) As Long
    Private Declare Function GetFileNameFromBrowseA Lib "shell32" Alias "#63" (ByVal hwndOwner As Long, ByVal lpstrFile As String, ByVal nMaxFile As Long, ByVal lpstrInitialDir As String, ByVal lpstrDefExt As String, ByVal lpstrFilter As String, ByVal lpstrTitle As String) As Long
    Private Sub Form_Load()
        'KPD-Team 2001
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@Allapi.net
        Dim sSave As String
        sSave = Space(255)
        'If we're on WinNT, call the unicode version of the function
        If IsWinNT Then
            GetFileNameFromBrowseW Me.hWnd, StrPtr(sSave), 255, StrPtr("c:\"), StrPtr("txt"), StrPtr("Text files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), StrPtr("The Title")
        'If we're not on WinNT, call the ANSI version of the function
        Else
            GetFileNameFromBrowseA Me.hWnd, sSave, 255, "c:\", "txt", "Text files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0), "The Title"
        End If
        'Show the result
        MsgBox sSave
    End Sub
    Public Function IsWinNT() As Boolean
        Dim myOS As OSVERSIONINFO
        myOS.dwOSVersionInfoSize = Len(myOS)
        GetVersionEx myOS
        IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
    End Function
    

    From what I can tell you GetFileName function looks right so my guess the problem is with this

    Text1 = Left$(textfile, lstrlen(textfile))
    

    Use this to check

    MsgBox "(" & Text1 & ")-(" & textfile & ")"
    

    to make sure you are getting the expected result from Left$ and lstrlen

    MarkJ : Might be an idea to suggest using the common dialog at the top of the answer? It would avoid you're answer being downvoted anyway:) It's possible Jackie-Brown is using some VBA environment where you can't use the common dialog, in which case your answer is correct of course.
    RS Conley : A suggestion involving Common dialog OCX is not the minimum thing needed to fix the issue as it introduces a dependency and requires changes to the setup of the program. What I missed is that he is passing sSave instead of textfile. But I showed how to diagnose it and give a known good example.

0 comments:

Post a Comment