Vb6: file selection for later use with the browse button

Here is 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 I edit the text file selected so later, I only call it using the text file in my function. However, I get a path that is 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 and then output it and use it.

+4
source share
5 answers

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, the GetFileName function looks right, so I assume the problem is related to this

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

Use this to check.

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

, Left $ lstrlen

0

shahkalpesh, , COM-.

VB6 :

  • >
  • "" "Microsoft Common Dialog Control 6.0" (SP6)

Common Dialog

:

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
+9

"Common Dialog Controls" VB6?

VB6 , .
→ → Microsoft Common Dialog Controls v....

, GetFileNameFromBrowseW -

+1

Maybe sSave contains the path name, but the text file contains 255 spaces.

0
source

Replace sSavewith textfile. When you need to reference the selected file, use Text1(presumably a VB text box control, so only Text1 implicitly calls Text1.Text, .Textlike the default item for VB.Textbox).

0
source