Creating Subfolders

M

Matt Lawrance

Hi.

I am looking for a VBA macro that can find out if a folder exists and if not create it. The problem with most of the macros that I find on the net, is that you have to tell it where to create the folder.

We have individual folders for each of our clients and we are needing to add a new folder to each of them. So I am looking for a macro that can integate each of these subfolder to see if the required folder exists. If it does not then create it. Then to move onto the next one.

I have been able to get VBA to create a list of all the subfolders, but not able to check or create the new required folder.

Many thanks for your assistance


Submitted via EggHeadCafe - Software Developer Portal of Choice
Dr. Dotnetsky's Cool .NET Tips and Tricks # 19
http://www.eggheadcafe.com/tutorial...8f57-bb41f7f8a8be/dr-dotnetskys-cool-net.aspx
 
C

Chip Pearson

Try some code like the following:


Sub AAA()
Dim FSO As Scripting.FileSystemObject
Dim SubF As Scripting.Folder
Dim RootF As Scripting.Folder
Dim RootFName As String
Dim SubFName As String

Set FSO = New Scripting.FileSystemObject
RootFName = "C:\Test\" '<<<< CHANGE
Set RootF = FSO.GetFolder(RootFName)
SubFName = "NewFolderName" '<<<< CHANGE
On Error Resume Next
For Each SubF In RootF.SubFolders
SubF.SubFolders.Add SubFName
Next SubF
End Sub


In this code, change RootFName to the folder that contains all your
client folders. Change SubFName to the name of the subfolder that
should be added to each client's folder. For example, if RootFName is
"C:\Clients" and SubFName is "NewFolder", then a folder named
"NewFolder" will be added to each folder within C:\Clients. E.g.,
"C:\Clients\Client1\NewFolder"

You'll need a reference to the scripting runtime. In VBA, go to the
Tools menu, choose References, and scroll down to "Microsoft Scripting
Runtime" and check that item.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
 
M

Matt Lawrance

Hi Chip.
Many thanks for the code, this worked a treat.

Thanks
Matt Lawrance



Chip Pearson wrote:

Try some code like the following:Sub AAA()Dim FSO As Scripting.
19-Jan-10

Try some code like the following:


Sub AAA()
Dim FSO As Scripting.FileSystemObject
Dim SubF As Scripting.Folder
Dim RootF As Scripting.Folder
Dim RootFName As String
Dim SubFName As String

Set FSO = New Scripting.FileSystemObject
RootFName = "C:\Test\" '<<<< CHANGE
Set RootF = FSO.GetFolder(RootFName)
SubFName = "NewFolderName" '<<<< CHANGE
On Error Resume Next
For Each SubF In RootF.SubFolders
SubF.SubFolders.Add SubFName
Next SubF
End Sub


In this code, change RootFName to the folder that contains all your
client folders. Change SubFName to the name of the subfolder that
should be added to each client's folder. For example, if RootFName is
"C:\Clients" and SubFName is "NewFolder", then a folder named
"NewFolder" will be added to each folder within C:\Clients. E.g.,
"C:\Clients\Client1\NewFolder"

You'll need a reference to the scripting runtime. In VBA, go to the
Tools menu, choose References, and scroll down to "Microsoft Scripting
Runtime" and check that item.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]

Previous Posts In This Thread:


Submitted via EggHeadCafe - Software Developer Portal of Choice
Bing Search RSS with Silverlight 3 RIA Domain Service
http://www.eggheadcafe.com/tutorial...7-17e5f15f4c9a/bing-search-rss-with-silv.aspx
 

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