PDA

View Full Version : File selection box



duck__boy
02-15-2010, 05:01 AM
Hey all,

Is there a way to bring up a box to allow me to select a folder in VBA for word?

Thanks.

dansyoz
02-15-2010, 05:27 AM
You can try

Function BrowseFolder() As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show Then
BrowseFolder = .SelectedItems(1) & "\"
End If
End With
End Function

duck__boy
02-15-2010, 05:32 AM
Ace, works a treat.

Thanks for that.

Next thing I am trying to do is make an array of all the file names in that folder that are either *.doc or *.docx, don't spose you have any pointers for that at all?

dansyoz
02-15-2010, 06:14 AM
There might be better ways but try

Dim lngFileCounter As Long ' holds array subscript
Dim strCurrFile As String ' holds current file name
Dim strSearchDir As String ' holds the results of the browse function
Dim strFileMask As String ' holds the file mask you want to search for
Dim strSearchDirArray() As String ' The array we are going to put our results in

ReDim strSearchDirArray(1) ' Give the array some small room to start
strSearchDir = Browsefolder ' use the function I posted to return a directory

strFilemask = "*.doc" ' set this to whatever you want
lngFileCounter = 0
strCurrFile = Dir$(strSearchDir + strFileMask)
Do While strCurrFile <> ""
lngFileCounter = lngFileCounter + 1
strSearchDirArray(lngFileCounter) = strCurrFile
strCurrFile = Dir$
' Redimension the file array
ReDim Preserve strSearchDirArray(lngFileCounter )
Loop
' then add your code to deal with the contents of the array


Note I use option base 1 in my code so subscripts start from 1 not 0.
Hope this helps

duck__boy
02-15-2010, 06:20 AM
Thanks for the help - even if it's not quite where I need to be I'm sure it will enable me to get there.

Thanks.