Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: Browse a Folder and Populate Listbox with Folder Items

  1. #1

    Browse a Folder and Populate Listbox with Folder Items

    Hi guys,

    Im having a hard time creating a simple txtfile editor. What I want to do is browse a folder then populate the listbox with the txtfile in the folder, and if i click the txtfile on the list it will appear on my rich textbox with the ability to edit and save the txtfile.

    See attached file to see what I mean.

    Thanks as always VBAX
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    You appear to be wanting to use Application.GetOpenFilename to browse for a folder. What version of Excel are you running, or better, what is the earliest version (is used by others) the program is likely to be run in?

    Mark

  3. #3
    Hi GTO,

    Im using 2007. Also it will be use in ms 2007.

    Thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    What exactly is your question here, there seem to be 6 or 7 there?

    In you code, you haven't qualified GetOpenFilename with the Application object, then you try to do a Dir on the selected file PLUS .txt, i.e. two extension types.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Oh Im sorry to make you confused. Forget about that code inside the workbook I was just experimenting a while a when I search it on the net how to browse a folder and view the content of the folder in listbox. Actually folder only contains textfiles which I need to edit as well inside rtb and save it.

    Btw thanks for helping me lastime xld. It really helps me a lot..
    Forgive if I always ask for help

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    This will load the listbox

    Private Sub CommandButton1_Click()
        Dim MyFolder As String
        Dim MyFile As String
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show = -1 Then
                ListBox1.Clear
                TextBox1.Text = .SelectedItems(1)
                MyFile = Dir(.SelectedItems(1) & "\*.txt")
                Do While MyFile <> ""
                    ListBox1.AddItem MyFile
                    MyFile = Dir
                Loop
            End If
        End With
    End Sub
    Last edited by Aussiebear; 03-14-2025 at 11:20 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    It works well xld.. Now I have to figure out how to make that txtfile editable in the rtb when I click it.. thanks so much

  8. #8
    Hi guys,

    I manage to load and edit the text in rtb however i have problem saving it. I use this code below.

    [vba]
    Me.rtbTxt.SaveFile (Me.dirTxt.Text & Application.PathSeparator & Me.txtList.Value)
    [/vba]
    It did overwrite the txtfile selected, but there are some text generated. Here are the sample output if I save the file

    {\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
    {\*\generator Riched20 12.0.4518.1014;}\viewkind4\uc1
    \pard\f0\fs17\lang1033 This is a test..
    \par }
    It should on be.
    This is a test..
    How do i eliminate those generated words in the textfile? and save only the changes I made?

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Why are you using a rich textbox, not a standard textbox?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    I read that it is better to use rtb rather than textbox when making textfile editor as you can also format. Is textbox recommended what im working for? Anyway Im just editing txtfiles and overwrite it.

    I tried changing it to textbox but unfortunately it doesnt support .loadFile

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    But what formatting do you need? As far as I can see all you are doing is browsing for a folder, dropping that name into a textbox and populating a listbox with all files there. Why do you need loadfile, and savefile?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    Hi xld,

    I dont need formatting actually as Iam only edting txtfile in rtb.
    Im using loadfile to load the txtfile i selected in the listbox in rtb.
    I used savefile to save(overwrite) the changes I made in rtb with the selected txtfile. Please see the updated attachment below.

    Thanks
    Attached Files Attached Files

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    I never use rtb, I would just open the textfile selected and then save it as a textfile.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Howdy ya'll,

    I admit that I was hoping that this would become obvious to me after checking at work (excel 2010), but it has not.

    What is a 'rich textbox'? Thank you in advance for that bit.

    As I understand the remainder (and I may be well off the mark), we want a browser to pick the text file to edit. If that is the case, rather than dump the text file's text into a control, edit the text, then save and close it, why would we not simply open the file in a text editor?

    Again, I may well be missing something?

    Mark

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    A rich textbox is an enhanced userform textbox control that allows rich text, formtting, colours, etc. The OP doesn't need the foormatting gizmos, but he is using loadfile and savefile properties of the control, which is why I suggested using a standard textbox, and open and save the textfile as normal VBA operations.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    could you show me xld how its done the one you are suggesting using textbox open/save which has the same result i want to achieve? does it use FSO?

    Thanks

  17. #17
    @bigJD
    Create a textbox "Textbox1"
    Replace Private Sub ListBox1_Click() with this code ;
    Dim txt As String
        With Me.Listbox1
        If .ListIndex > -1 Then
            If FileLen(.Value) Then
                Me.Textbox1.Value = _
                CreateObject("Scripting.FileSystemObject") _
                .OpenTextFile(.Value).ReadAll
            Else
                Me.Textbox1.Value = ""
            End If
        End If
        End With
    On your SAVE button which is CommandButton3 put this code

    With Me
        If .Listbox1.ListIndex > -1 Then
            Open Listbox1.Value For Output As #1
            Print #1, .Textbox1.Value
            Close #1
        End If
        End With
    That will work IMO.
    Last edited by Aussiebear; 03-14-2025 at 11:23 AM.

  18. #18
    DefCon thanks it work yesterday but for unknown reason I used it again now it return to Runtime Error "53" File not Found if I clicked debug it points me to
    If FileLen(.Value) Then. Why could that be?

  19. #19
    snb
    Guest
    To populate the listbox:

    Sub snb()
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = -1 Then ListBox1.List = Split(CreateObject("wscript.shell").exec("cmd /c dir " & .SelectedItems(1) & "\*.txt /b").StdOut.readall, vbCrLf)
    End With
    End Sub
    Last edited by Aussiebear; 03-14-2025 at 11:23 AM.

  20. #20
    snb
    Guest
    Since your listbox isn't populated with the file's fullname the code you use doesn't work.

    If you apply my previous suggestion you can use afterwards:


    sub snb_001()
    
    If Listbox1.ListIndex > -1 Then Textbox1.Value =CreateObject("Scripting.FileSystemObject").OpenTextFile(Listbox1.Value).ReadAll
    end sub



    Last edited by Aussiebear; 03-14-2025 at 11:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •