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 / Excel / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Make new folder & save to it

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
krabople - 25 Jan 2006 11:21 GMT
Hi, I was wondering if anybody knows a way, through VBA, to create a new
folder in the same directory and then save the active workbook to that
new folder?

Thanks in advance

Signature

krabople

Martin Fishlock - 25 Jan 2006 11:56 GMT
Dear Krabople:

Here are a couple of routines that may help. Modofy as necessary.

Sub MakeFolderAndSaveActiveBook()
   Dim foldername As String
   Dim filename As String
   Dim s As String
   Dim wb As Workbook
   foldername = "ggg" ' modify for folder name
   filename = "filename.xls"
   s = CreateFolder(foldername)
   If Len(s) > 0 Then
       MsgBox s, vbOKOnly, "Created"
       s = s & "\" & filename
       ActiveWorkbook.SaveAs s
       MsgBox s, vbOKOnly, "Created"
   Else
       MsgBox "Cannot Create Folder " & foldername, vbOKOnly, "Cannot
create folder"
   End If
End Sub

Function CreateFolder(folder As String) As String
   Dim p As Variant
   On Error GoTo Err
   CreateFolder = ""
   p = CurDir
   If Len(p) > 0 Then
       If Right(p, 1) <> "\" Then p = p & "\"
       p = p & folder
       MkDir p
       CreateFolder = p
   End If
Err:
End Function

Signature

HTHs Martin

> Hi, I was wondering if anybody knows a way, through VBA, to create a new
> folder in the same directory and then save the active workbook to that
> new folder?
>
> Thanks in advance
krabople - 25 Jan 2006 12:23 GMT
That's perfect, thanks very much for your help!

Signature

krabople

krabople - 25 Jan 2006 12:34 GMT
Hi, thanks again for the code, it works great. The only problem is that
it creates the folder in My Documents for some reason whereas I need
the folder to be in the same directory as the active workbook. How do I
do this?

Signature

krabople

Martin Fishlock - 25 Jan 2006 14:33 GMT
Try this:

Function CreateFolder(folder As String) As String
   Dim p As Variant
   On Error GoTo Err
   CreateFolder = ""
   p = ActiveWorkbook.Path ' CurDir of active workbook<<<<<<<<<<<<
   If Len(p) > 0 Then
       If Right(p, 1) <> "\" Then p = p & "\"
       p = p & folder
       MkDir p
       CreateFolder = p
   End If
Err:
End Function

Signature

HTHs Martin

> Hi, thanks again for the code, it works great. The only problem is that
> it creates the folder in My Documents for some reason whereas I need
> the folder to be in the same directory as the active workbook. How do I
> do this?
krabople - 25 Jan 2006 14:48 GMT
Brilliant that's done the job, thanks again

Signature

krabople

 
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.