PDA

View Full Version : Browse a Folder and Populate Listbox with Folder Items



bigJD
05-09-2012, 08:50 PM
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 :)

GTO
05-09-2012, 09:00 PM
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

bigJD
05-10-2012, 12:10 AM
Hi GTO,

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

Thanks :)

Bob Phillips
05-10-2012, 12:15 AM
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.

bigJD
05-10-2012, 01:14 AM
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 :)

Bob Phillips
05-10-2012, 01:36 AM
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

bigJD
05-10-2012, 06:00 PM
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 :)

bigJD
05-10-2012, 08:32 PM
Hi guys,

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


Me.rtbTxt.SaveFile (Me.dirTxt.Text & Application.PathSeparator & Me.txtList.Value)

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?

Bob Phillips
05-11-2012, 12:50 AM
Why are you using a rich textbox, not a standard textbox?

bigJD
05-11-2012, 01:23 AM
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

Bob Phillips
05-11-2012, 01:29 AM
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?

bigJD
05-11-2012, 01:43 AM
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

Bob Phillips
05-11-2012, 02:57 AM
I never use rtb, I would just open the textfile selected and then save it as a textfile.

GTO
05-11-2012, 03:42 AM
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

Bob Phillips
05-11-2012, 04:05 AM
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.

bigJD
05-11-2012, 08:07 AM
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

defcon_3
05-26-2012, 01:32 AM
@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.

bigJD
05-27-2012, 11:34 PM
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?

snb
05-28-2012, 09:08 AM
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

snb
05-28-2012, 12:36 PM
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

bigJD
05-28-2012, 07:46 PM
@ snb it doesnt work either. :(

snb
05-29-2012, 12:27 AM
What is 'it' ?


Private Sub Listbox1_Change()


If Listbox1.ListIndex > -1 Then Textbox1.Value =CreateObject("Scripting.FileSystemObject").OpenTextFile(Listbox1.Value).ReadAll
End Sub