Make a directory on the fly

J

Jonefer

I'm trying to write a vb routine that will save a file in
a certain directory. I want to test for the existence of
the directory and create it if it doesn't exist, before
saving the file.

please help?
Thanks
 
D

Don Guillett

Here is a sub I use to create a directory and save a backup file in that
directory. Modify to suit.

Sub Backup() 'kept in personal.xls & assigned to toolbar button
On Error GoTo BackupFile
MkDir CurDir & "\Backup"
BackupFile:
With ActiveWorkbook
MyWB = .Path & "\BACKUP\" & .Name
.SaveCopyAs MyWB
.Save
End With
End Sub
 
G

gocush

Try something like this: - adjusted for your names


On Error Resume Next
MkDir ThisWorkbook.Path & "\NEW DIRECTORY NAME"
ChDir ThisWorkbook.Path & "\NEW DIRECTORY NAME"
ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\NEW DIRECTOR
NAME\MyFileName.xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False



HT
 
T

Tom Ogilvy

Just trap the error - less work than testing.

On Error Resume Next
mkdir "C:\MyDirectory"
On Error goto 0

If your directory is several levels, then you might need to work down the
structure

On Error Resume Next
mkdir "c:\Level1"
mkdir "c:\Level1\Level2"
mkdir "c:\Level1\Level2\Level3"
On Error goto 0

If there might be a problem with permissions, then you might need to check
for the existence after the above.
 

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