Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
and - 07 Jan 2005 12:36 GMT
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
Helmut Weber - 07 Jan 2005 13:07 GMT
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/
and - 07 Jan 2005 13:23 GMT
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
--
Helmut Weber - 07 Jan 2005 14:10 GMT
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/
Jay Freedman - 07 Jan 2005 14:33 GMT
For help with getting the file names into the listbox, see
http://word.mvps.org/FAQs/MacrosVBA/ReadFilesIntoArray.htm.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> Hi Andy,
>
[quoted text clipped - 20 lines]
> Word XP, Win 98
> http://word.mvps.org/
and - 07 Jan 2005 15:13 GMT
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

-----------------

ANDy
Helmut Weber - 07 Jan 2005 21:02 GMT
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/
and - 09 Jan 2005 10:03 GMT
> 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
--
Helmut Weber - 09 Jan 2005 11:35 GMT
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/
and - 09 Jan 2005 12:13 GMT
Helmut, vielen Dank!

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

Best regards,

ANDy
--
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.