Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 132
Default MkDir question

The following macro works and will eventurally be used in a Workbook Close
event. My question is why do I need to use MkDir twice to create a
subdirectory as C:\SubDir1\SubDir2. Is there an easier way to do this?

Private Sub TestSave()
'-----create a Backup

Dim BUpath1 As String
Dim BUpath2 As String
Dim BUname As String

BUpath1 = "C:\Invoices"
BUpath2 = "C:\Invoices\BackUp"
BUname = "Sub-Contract Invoice BACKUP TEST " & Format(Now, "dd-mmm-yy
h-mm-ss")

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Worksheets("Start").Activate
ThisWorkbook.Save

On Error Resume Next
If Dir(BUpath2) = "" Then
MkDir (BUpath1)
MkDir (BUpath2)
ActiveWorkbook.SaveCopyAs BUpath2 & "\" & BUname

MsgBox "A copy of the file has been saved as: " & BUpath & "\" & BUname
End If

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub
--
Jim
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default MkDir question

You have to create the directories in layers, you cannot create the child
until the parent exists.

I do it like this

Private Sub TestSave()
'-----create a Backup

Dim BUpath As String
Dim BUname As String

BUpath = "C:\Invoices\BackUp"
BUname = "Sub-Contract Invoice BACKUP TEST " & Format(Now, "dd-mmm-yy
h-mm-ss")

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Worksheets("Start").Activate
ThisWorkbook.Save

On Error Resume Next
If Dir(BUpath) = "" Then
MakeDir BUpath
ActiveWorkbook.SaveCopyAs BUpath & "\" & BUname

MsgBox "A copy of the file has been saved as: " & BUpath & "\" & BUname
End If

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub

Private Function MakeDir(Path As String)
Dim aryDirs As Variant
Dim partPath As String
Dim i As Long

aryDirs = Split(Path, Application.PathSeparator)
partPath = aryDirs(LBound(aryDirs))
On Error Resume Next
For i = LBound(aryDirs) + 1 To UBound(aryDirs)

partPath = partPath & Application.PathSeparator & aryDirs(i)
MkDir partPath
Next i
End Function



--
__________________________________
HTH

Bob

"Jim G" wrote in message
...
The following macro works and will eventurally be used in a Workbook Close
event. My question is why do I need to use MkDir twice to create a
subdirectory as C:\SubDir1\SubDir2. Is there an easier way to do this?

Private Sub TestSave()
'-----create a Backup

Dim BUpath1 As String
Dim BUpath2 As String
Dim BUname As String

BUpath1 = "C:\Invoices"
BUpath2 = "C:\Invoices\BackUp"
BUname = "Sub-Contract Invoice BACKUP TEST " & Format(Now, "dd-mmm-yy
h-mm-ss")

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Worksheets("Start").Activate
ThisWorkbook.Save

On Error Resume Next
If Dir(BUpath2) = "" Then
MkDir (BUpath1)
MkDir (BUpath2)
ActiveWorkbook.SaveCopyAs BUpath2 & "\" & BUname

MsgBox "A copy of the file has been saved as: " & BUpath & "\" & BUname
End If

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub
--
Jim



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 132
Default MkDir question

Sheer genius! I can almost understand it as well.

I'll make good use of this.

Thanks Bob
--
Jim


"Bob Phillips" wrote:

You have to create the directories in layers, you cannot create the child
until the parent exists.

I do it like this

Private Sub TestSave()
'-----create a Backup

Dim BUpath As String
Dim BUname As String

BUpath = "C:\Invoices\BackUp"
BUname = "Sub-Contract Invoice BACKUP TEST " & Format(Now, "dd-mmm-yy
h-mm-ss")

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Worksheets("Start").Activate
ThisWorkbook.Save

On Error Resume Next
If Dir(BUpath) = "" Then
MakeDir BUpath
ActiveWorkbook.SaveCopyAs BUpath & "\" & BUname

MsgBox "A copy of the file has been saved as: " & BUpath & "\" & BUname
End If

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub

Private Function MakeDir(Path As String)
Dim aryDirs As Variant
Dim partPath As String
Dim i As Long

aryDirs = Split(Path, Application.PathSeparator)
partPath = aryDirs(LBound(aryDirs))
On Error Resume Next
For i = LBound(aryDirs) + 1 To UBound(aryDirs)

partPath = partPath & Application.PathSeparator & aryDirs(i)
MkDir partPath
Next i
End Function



--
__________________________________
HTH

Bob

"Jim G" wrote in message
...
The following macro works and will eventurally be used in a Workbook Close
event. My question is why do I need to use MkDir twice to create a
subdirectory as C:\SubDir1\SubDir2. Is there an easier way to do this?

Private Sub TestSave()
'-----create a Backup

Dim BUpath1 As String
Dim BUpath2 As String
Dim BUname As String

BUpath1 = "C:\Invoices"
BUpath2 = "C:\Invoices\BackUp"
BUname = "Sub-Contract Invoice BACKUP TEST " & Format(Now, "dd-mmm-yy
h-mm-ss")

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Worksheets("Start").Activate
ThisWorkbook.Save

On Error Resume Next
If Dir(BUpath2) = "" Then
MkDir (BUpath1)
MkDir (BUpath2)
ActiveWorkbook.SaveCopyAs BUpath2 & "\" & BUname

MsgBox "A copy of the file has been saved as: " & BUpath & "\" & BUname
End If

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub
--
Jim




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default MkDir question

Try this code (use standard module) Declare Function must be at top of module.

Declare Function MakePath Lib "imagehlp.dll" Alias _
"MakeSureDirectoryPathExists" (ByVal lpPath As
String) As Long

Sub CreatePath()
MakeDir "C:\Invoices\Invoices\BackUp"
End Sub

Sub MakeDir(DirPath As String)

If Right(DirPath, 1) < "\" Then DirPath = DirPath & "\"

MakePath DirPath

End Sub
--
jb


"Jim G" wrote:

The following macro works and will eventurally be used in a Workbook Close
event. My question is why do I need to use MkDir twice to create a
subdirectory as C:\SubDir1\SubDir2. Is there an easier way to do this?

Private Sub TestSave()
'-----create a Backup

Dim BUpath1 As String
Dim BUpath2 As String
Dim BUname As String

BUpath1 = "C:\Invoices"
BUpath2 = "C:\Invoices\BackUp"
BUname = "Sub-Contract Invoice BACKUP TEST " & Format(Now, "dd-mmm-yy
h-mm-ss")

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Worksheets("Start").Activate
ThisWorkbook.Save

On Error Resume Next
If Dir(BUpath2) = "" Then
MkDir (BUpath1)
MkDir (BUpath2)
ActiveWorkbook.SaveCopyAs BUpath2 & "\" & BUname

MsgBox "A copy of the file has been saved as: " & BUpath & "\" & BUname
End If

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub
--
Jim

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default MkDir question

Try this :

http://www.EXCELGAARD.dk/Lib/UDFs/MAKEPATH/


CE



Jim G wrote:
The following macro works and will eventurally be used in a Workbook
Close event. My question is why do I need to use MkDir twice to
create a subdirectory as C:\SubDir1\SubDir2. Is there an easier way
to do this?

Private Sub TestSave()
'-----create a Backup

Dim BUpath1 As String
Dim BUpath2 As String
Dim BUname As String

BUpath1 = "C:\Invoices"
BUpath2 = "C:\Invoices\BackUp"
BUname = "Sub-Contract Invoice BACKUP TEST " & Format(Now, "dd-mmm-yy
h-mm-ss")

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Worksheets("Start").Activate
ThisWorkbook.Save

On Error Resume Next
If Dir(BUpath2) = "" Then
MkDir (BUpath1)
MkDir (BUpath2)
ActiveWorkbook.SaveCopyAs BUpath2 & "\" & BUname

MsgBox "A copy of the file has been saved as: " & BUpath & "\" &
BUname End If

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 132
Default MkDir question

Perfectly simple, thanks John
--
Jim


"john" wrote:

Try this code (use standard module) Declare Function must be at top of module.

Declare Function MakePath Lib "imagehlp.dll" Alias _
"MakeSureDirectoryPathExists" (ByVal lpPath As
String) As Long

Sub CreatePath()
MakeDir "C:\Invoices\Invoices\BackUp"
End Sub

Sub MakeDir(DirPath As String)

If Right(DirPath, 1) < "\" Then DirPath = DirPath & "\"

MakePath DirPath

End Sub
--
jb


"Jim G" wrote:

The following macro works and will eventurally be used in a Workbook Close
event. My question is why do I need to use MkDir twice to create a
subdirectory as C:\SubDir1\SubDir2. Is there an easier way to do this?

Private Sub TestSave()
'-----create a Backup

Dim BUpath1 As String
Dim BUpath2 As String
Dim BUname As String

BUpath1 = "C:\Invoices"
BUpath2 = "C:\Invoices\BackUp"
BUname = "Sub-Contract Invoice BACKUP TEST " & Format(Now, "dd-mmm-yy
h-mm-ss")

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Worksheets("Start").Activate
ThisWorkbook.Save

On Error Resume Next
If Dir(BUpath2) = "" Then
MkDir (BUpath1)
MkDir (BUpath2)
ActiveWorkbook.SaveCopyAs BUpath2 & "\" & BUname

MsgBox "A copy of the file has been saved as: " & BUpath & "\" & BUname
End If

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub
--
Jim

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 132
Default MkDir question

Thanks Charlotte
--
Jim


"Charlotte E." wrote:

Try this :

http://www.EXCELGAARD.dk/Lib/UDFs/MAKEPATH/


CE



Jim G wrote:
The following macro works and will eventurally be used in a Workbook
Close event. My question is why do I need to use MkDir twice to
create a subdirectory as C:\SubDir1\SubDir2. Is there an easier way
to do this?

Private Sub TestSave()
'-----create a Backup

Dim BUpath1 As String
Dim BUpath2 As String
Dim BUname As String

BUpath1 = "C:\Invoices"
BUpath2 = "C:\Invoices\BackUp"
BUname = "Sub-Contract Invoice BACKUP TEST " & Format(Now, "dd-mmm-yy
h-mm-ss")

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Worksheets("Start").Activate
ThisWorkbook.Save

On Error Resume Next
If Dir(BUpath2) = "" Then
MkDir (BUpath1)
MkDir (BUpath2)
ActiveWorkbook.SaveCopyAs BUpath2 & "\" & BUname

MsgBox "A copy of the file has been saved as: " & BUpath & "\" &
BUname End If

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with MkDir steve_doc Excel Programming 3 July 19th 07 02:50 PM
mkdir Peter[_61_] Excel Programming 2 May 12th 07 12:48 PM
MKDir not working Robert_L_Ross Excel Programming 3 June 30th 06 07:17 AM
mkdir problem Tom Ogilvy Excel Programming 2 August 31st 04 03:22 PM
mkdir problem Norman Jones Excel Programming 0 August 30th 04 04:37 PM


All times are GMT +1. The time now is 04:39 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"