Thursday, January 01, 2009

How to delete Folder using VBA

VBA RmDir Method


Here is a simple method to delete the entire folder using VBA

Sub Delete_Folders_FS()

Dim sFolder As String

On Error GoTo Err_Msg

sFolder = "c:\temp\2Bdeleted\"

RmDir (sFolder)

Err_Msg:

If Err.Number <> 0 Then

MsgBox Err.Description

Err.Clear

End If

End Sub

3 comments:

  1. Hi I tried with the code given for deleting a folder, but its showing error as Path/File access error. please tell me how to solve and also I need to delete only empty folder in a path, kindly help me providing suitable code.

    Thanks In advance

    ReplyDelete
  2. Anonymous3:28 AM

    Because there are files in the folder you are trying to delete. Use the Kill statement to delete files then remove the folder...either that or use
    objFolder = "Path to folder"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.deletefolder objFolder

    ReplyDelete
  3. Anonymous1:10 PM

    Thanks, below is working fine.

    objFolder = "Path to folder"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.deletefolder objFolder
    Read more at http://vbadud.blogspot.com/2009/01/how-to-delete-folder-using-vba.html#OSEFl6CPmg3ih0dL.99

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...

Visual Basic for Applications (VBA) Forum (recent threads)

CodeKeep VBA Feed

Visual Studio Tools for Office Forum (recent threads)

Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

Office Business Applications (OBA) Team Blog

MSDN Code Gallery Published Resources For Tag VSTO

microsoft.public.vsnet.vstools.office Google Group