Working with folders

P

Pete

I have a couple of problems I hope someone will be able to help with

Firstly I would like a folder to be created (name inputted in to text
box by end user) then a commanbutton which will then create said folder
at the given path. However at moment all I can achieve is a folder
called textbox1 so nearly there but something amiss.



Secondly when folder is created I would like a list box to be able to
view all folders within above folder, Im working with the code below
and trying to utilise it without much luck, Im aware that this deals
with files rather than folders.


Private Sub UserForm_Initialize()
Dim objFSO As Object, TargetFolder As Object, FC As Object

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set TargetFolder = objFSO.GetFolder("C:\Program Files\Azzmet\Data\RISK
ASSESSMENTS")
Set FC = TargetFolder.Files
HowMany = FC.Count


For Each File In FC
ListBox1.AddItem (File.Name)
Debug.Print File.Name
Next File

Any help would be appreciated

Regards


Pete
 
D

Doug Robbins - Word MVP

It would help if you pasted the code that you have. However, the code that
you would need to create a folder with the name that is entered into
Textbox1 would be

MkDir "c:\" & Textbox1.text

As for getting the names of the folders, See the article "How to get the
names of all the folders in the folder tree, starting from a specified
folder" at:

http://www.word.mvps.org/FAQs/MacrosVBA/ReadFoldersIntoArray.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
P

Pete

Hi Doug

Manyhtanks for the info, the code Im using for creating the folder, how
would your code fit in with this?

Private Sub CommandButton1_Click()
Dim fs, f, N
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.CreateFolder("c:\TextBox1.Value\")

Set f = Nothing
Set fs = Nothing

End Sub


Regards

Pete
 
D

Doug Robbins - Word MVP

Assuming that

Set f = fs.CreateFolder("c:\TextBox1.Value\")

does create a folder, to get it to create one under the root directory with
the name of the entry in TextBox1, you would use:

Set f = fs.CreateFolder("c:\" & TextBox1.Value)


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
P

Pete

Hi Doug

I have manged to get the code for running through folders to run
however I cant get the folders to be displayed in a list box ?


Regards

Pete
 
D

Doug Robbins - Word MVP

Replace the following code in the article to which I referred you:

For i = LBound(FoldersArray) To UBound(FoldersArray)
ActiveDocument.Range.InsertAfter FoldersArray(i) & vbCr
Next i

with

For i = LBound(FoldersArray) To UBound(FoldersArray)
[ListBox].AddItem FoldersArray(i)
Next i

replace [ListBox] with a proper reference to the listbox to which you want
to add the folders.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
P

Pete

Hi Doug

Sorry Im missing something here, I now have the following code

Dim FoldersArray As Variant
Dim i As Integer

'Read all subfolders of the specified folder into an array
'by calling the funcGetSubfolders function
FoldersArray = funcGetSubfolders("C:\Windows")

'Put the results (the array values) into the current document if it is
blank,
'or else into a new document
If Len(ActiveDocument.Range.Text) > 1 Then
Documents.Add
End If
For i = LBound(FoldersArray) To UBound(FoldersArray)
ListBox1.AddItem FoldersArray(i)
Next i

ActiveDocument.Saved = True

I have tried all varients of placing text within listbox1 code, form
intilize etc so must be doing something wrong?


Regards

Pete
 
D

Doug Robbins - Word MVP

The code would normally be in the Initialize() event of the form. This part
of the code is not required

'Put the results (the array values) into the current document if it is
blank,
'or else into a new document
If Len(ActiveDocument.Range.Text) > 1 Then
Documents.Add
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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