PDA

View Full Version : VBA to delete folder made by macro



megha
11-21-2011, 10:25 AM
The following code saves my document as different file in specific folder with date and time as a file name. It also creates a folder with current month and year before saving the file. It is working wonderful.

I am trying to edit my code so when it reaches to the anniversary the old folder with all files get automatically deleted. For example, let’s say today I click the command button and it create folder “Nov_2011” and save my files in that folder for November 2011. When I reach to Nov 2012 I want to delete “Nov_2011” folder automatically. Is that possible?

Please advice. I am not expert with the VBA. I will appreciate your help. Thanks in advance.


Private Sub CommandButton1_Click()
Dim sFileName As String
Dim sPath As String
CommandButton1.Enabled = False
sFileName = Format(DateValue(Now()), "mmm_dd_yyyy") & "_" & _
Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hh_mm_ss_AM/PM")
If Len(Dir("\\My Document/Completed_Checklist\" & Format(DateValue(Now()), "mmm_yyyy"), vbDirectory)) = 0 Then
MkDir "\\ My Document/Completed_Checklist\" & Format(DateValue(Now()), "mmm_yyyy")
End If
sPath = "\\ My Document/Completed_Checklist\" & Format(DateValue(Now()), "mmm_yyyy")
sFileName = sFileName & ".doc"
ActiveDocument.SaveAs FileName:=sPath & "\" & sFileName, FileFormat:=xlNormal, ReadOnlyRecommended:=False
ThisDocument.Close SaveChanges:=False
End Sub