Get *.doc and *.rtf files in 1 folder listed on a UserForm?

A

and

Is it possible to get all *doc and *.rtf files in a selected folder on a
user form in Word XP?

Any idea how I could tackle this problem? Or are there any examples
around from which i could copy and paste the structure?


*Background info: Some of the files will be selected on the user form by
the user. Also, the user will set only one of the files as the "main
file". Only selected files will be processed by another macro. The main
file will be processed differently. The files should be displayed on the
form with radio buttons.


Ciao,
ANDy
 
H

Helmut Weber

Hi Andy,

not the files, the filenames!

And using radiobuttons is not a good idea. I don't know how many
files one (1) directory can hold, but an awefull lot.
And more, I would recommended a listbox for the main file.
And a listbox for secondary files. Though it may be possible
to create a workaraound to for finding out, what entry in
a multiple select list was clicked first, this seems unnecessarily
complicated to me. By using application.filesearch or just dir(),
which might have fewer bugs, you could read in the filenames into
an array and assign this array to both the lists.

Though it is not a problem to create controls at runtime
and adjust the size of the userform accordingly, but how to
design the algorithm where to place the controls, etc...?

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
A

and

Hi Helmut,
not the files, the filenames!
You're absolutely right. That would require even more runtime userform
adjustments!
And using radiobuttons is not a good idea. I don't know how many
files one (1) directory can hold, but an awefull lot.
In this case up to 10. These are specially prepared directories for file
processing.
And more, I would recommended a listbox for the main file.
And a listbox for secondary files.
That sounds very practical. If I get it right, I could first let the
user select the main file in listbox #1, and then some other files (up
to 9 in this case) in a second list box?

Are there listboxes for multiple selections? How do I feed the user
selection to a list box?

I hope you can help me make the next step.

Thanks in advance,

ANDy
--
 
H

Helmut Weber

Hi Andy,

in case you have got so far, to have a userform
with 2 listboxes, and you can fill the listboxes with
filenames, and the 2nd listbox has the multiselect property
fmMultiSelectMulti (1), then a command button would
return the selected items like this:

Private Sub CommandButton1_Click()
Dim i As Integer
For i = 0 To Me.ListBox2.ListCount - 1
If Me.ListBox2.Selected(i) Then
MsgBox Me.ListBox2.List(i)
End If
Next
End Sub

HTH

Greetings from Bavaria, a region in Europe
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
A

and

Hi Helmut and Jay,

Thanks a lot for your help. It's really great to get some help of
experienced VBA programmers. In the meanwhile, I have tried to
accomplish it in another way (see below), but I don't understand why the
path is not included in the filename variable, strFName. Do you?:

Sub GetAllFilesInFolder()

Dim strFPath As String
Dim strFName As String
Dim lngI As Long 'number of listbox items

'open a Word dialog
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
strFPath = .Directory
End With
'strip quotes
If Len(strFPath) = 0 Then Exit Sub
If Asc(strFPath) = 34 Then
strFPath = Mid$(strFPath, 2, Len(strFPath) - 2)
End If

'get only *.doc and *.rtf files from the selected
'path and insert them into the list box
strFName = Dir$(strFPath & "*.*") 'path is not included! WHY?
Do While strFName <> ""
If Mid(strFName, Len(strFName) - 2, 3) = "doc" Or _
Mid(strFName, Len(strFName) - 2, 3) = "rtf" Then
lstDoelBestanden.AddItem (strFName)
End If
strFName = Dir
Loop

End Sub
 
H

Helmut Weber

Hi Andy,

good question,

because someone thought, returning the name
and only the name, was, what dir() should return. ;-)
strFName = Dir$(strFPath & "*.*")

Filesearch returns the fullname, but then people ask,
how to isolate the name! Whatever way, there is always
something to wish for.

You certainly don't need help on building the fullname, do you?

Greetings from Bavaria,
a region in Europe,
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
A

and

You certainly don't need help on building the fullname, do you?
Err, well, I've waisted my entire saturday trying this, so, if you could
spare a moment, ...

Best regards from the Netherlands,
ANDy
--
 
H

Helmut Weber

Hi Andy,

like this:

Sub Test333()
Dim strFName As String
Dim strFFull As String
Dim strFPath As String
strFPath = "c:\test\"
strFName = Dir$(strFPath & "*.*")
While strFName <> ""
strFFull = strFPath & strFName
' case sensitive !!
If LCase(Right(strFFull, 4)) = ".doc" Or _
LCase(Right(strFFull, 4)) = ".rtf" Then
Selection.TypeText Text:=strFFull & vbCr
' or add it to a listbox or whatever
End If
strFName = Dir()
Wend
End Sub

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
A

and

Helmut, vielen Dank!

When you see it, it looks so simple and easy.

Best regards,

ANDy
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top