PDA

View Full Version : multiple dir listbox's



devine78
03-15-2011, 03:47 AM
I want to create a userform with multiple listbox in Word.

If there are document on a network on a specified path i want to show this in a listbox and open thru commandbutton.

example:
if listbox1 would only show the documents on "P:\"
if listbox2 would only show the documents on "P:\apps\"
If listbox3 .... etc.

The most of the documents are doc of dot files.

I've found some code to show only listbox1, but i don't know to fill up listbox2

Private Sub UserForm_Initialize()
Dim fileList() As String
Dim fName As String
Dim fPath As String
Dim I As Integer
'define the directory to be searched for files
fPath = "P:\"

'build a list of the files
fName = Dir(fPath & "*.doc")
While fName <> ""
'add fName to the list
I = I + 1
ReDim Preserve fileList(1 To I)
fileList(I) = fName
'get next filename
fName = Dir()
Wend
'see if any files were found
If I = 0 Then
MsgBox "No files found"
Exit Sub
End If
'cycle through the list and add to listbox
For I = 1 To UBound(fileList)
Me.ListBox1.AddItem fileList(I)
Next
End Sub
Private Sub Files_Change()
Documents.Open "P:\" & Files.Text
Unload DocOpen
End Sub

I hope someone can help me to fill the listbox2, listbox3 .... and if you selected one of the files you can click on commandbutton to open the file.

Thanks.

Tinbendr
03-15-2011, 12:24 PM
Welcome to VBA Express!

Private Sub UserForm_Initialize()
Call GetFiles("C:\Documents and Settings\Owner\My Documents\", Me.ListBox1)
Call GetFiles("P:\apps\", Me.ListBox2)
Call GetFiles("Different Path\", Me.ListBox3)
End Sub


Add module and insert.
Sub GetFiles(fPath As String, MyCtrl As Control)
Dim fileList() As String
Dim fName As String
Dim I As Integer

'build a list of the files
fName = Dir(fPath & "*.doc")
While fName <> ""
'add fName to the list
I = I + 1
ReDim Preserve fileList(1 To I)
fileList(I) = fName
'get next filename
fName = Dir()
Wend
'see if any files were found
If I = 0 Then
MsgBox "No files found"
Exit Sub
Else
'Assign Array to Listbox control
MyCtrl.List = fileList()
End If
End Sub

fumei
03-15-2011, 01:22 PM
As for opening a given file, what do you want to happen if there is something selected in ListBox1 AND Listbox2 AND Listbox3?

devine78
03-15-2011, 03:36 PM
Thanks David, your solution works, for showing the files. :bow:

For Gerry

If someone selected a file in Listbox1 or Listbox2 it's must open as a new document (template) so that they won't overwrite the original document.

I hope you can help me.

Thanks.

Tinbendr
03-15-2011, 07:11 PM
The most of the documents are doc of dot files.
Ok, a little clarification here.

Are they in fact dot files, or fresh Doc from Dot?

While you can use a doc file as a 'template', the chance of overwriting still exists, unlike creating Doc from dot. (Code gets stopped with an error or something.)

devine78
03-16-2011, 12:11 AM
Hello David,

if there a problem with .doc files then we will only using .dot files.

I hope this helps.

Thanks

Tinbendr
03-16-2011, 08:20 AM
if there a problem with .doc files then we will only using .dot files.
Not a problem, per se, but I think you'll be happier down the line if you use dot's instead.

So, one more question, (actually Gerry already asked this), what will happen when more than one template selected in multiple listboxes? Do you want to create a new doc from all listboxes that have a selection?

fumei
03-16-2011, 09:57 AM
If at all possible, do NOT use documents as repeating sources. That is what templates are for.

devine78
03-16-2011, 11:23 AM
Hello David and Gerry,

I want to use it for different departments. If someone asking for an account they can open the document and fill up the document, then if you click on save it wil asking the name you want to use, just like a new document.

The most of them will only selected one document. If someone accidentaly selected one or more documents just open as a new document.

If it possible that they only can selected 1 document at a time. If this is a problem with vba then it isn't a problem to leave this way.


I hope you understand my question.

Thanks

fumei
03-16-2011, 12:00 PM
"they can open the document and fill up the document, then if you click on save it wil asking the name you want to use, just like a new document. "

What makes you think that? If you open a document - not a template - and you click on Save, it simply saves it. It shoul dnot be asking for a name.

"If someone accidentaly selected one or more documents just open as a new document. "

Ummmm, which one? All of them?

devine78
03-16-2011, 01:11 PM
Hello Gerry,

Just 1 document what standing in the list (see attachment as example)

When I selected Mobile Phone.dot and press on commandbutton then the document open as a new document.

At the other time you Select Hardware.dot and then press on commandbutton then the document open as a new document.

I hope you understand me, sorry for my English.

fumei
03-17-2011, 10:23 AM
That is obvious, but as I have asked... what if Meno.dot AND Hardware.dot are selected?

devine78
03-17-2011, 11:05 AM
Hello Gerry,

If they are both selected then both can be opened.

If there are later complains for selecting more documents, then I would try creating a deselection commandbutton.

I don't know if it's a problem to create a code to open documents from different multiple listbox?

So for your question if Meno.dot AND Hardware.dot are selected then both can be opened.

I hope you can help me.

Thanks.

devine78
03-23-2011, 03:06 PM
Hello,

I Found a way to open the files and open as a new template.
I created a Module put the following code in it.

Public Sub OpenDoc(Document As String)
Documents.Add Template:=Document
ActiveDocument.Fields.Update
End Sub
Then at the Userform i used the following code

Private Sub CommandButton1_Click()
OpenDoc ThisDocument.Path & "\" & ListBox1.Value
Userform1.Hide
Unload Me
End Sub
Private Sub UserForm_Initialize()
Call GetFiles("\\Server\My Files\", Me.ListBox1)
Call GetFiles("\\Server\My Files\VBA\", Me.ListBox2)
Call GetFiles("\\Server\My Files\VBA\ICT\", Me.ListBox3)

End Sub
This works fine but if i want to open a file at listbox2 then i've to create a new commandbutton
"OpenDoc ThisDocument.Path & "\VBA\" & ListBox2.Value"

Is it possible to using only 1 commandbutton something like if listbox1 selected then open file or if listbox2 selected then open the file from listbox2.

Then i've another question is it possible not showing the file extension. Just the name of the document and not ".dot"

I hope someone still can help me.

Thanks.