【问题标题】:Deleting A Folder - How To?删除文件夹 - 如何?
【发布时间】:2018-09-28 15:44:50
【问题描述】:

这是我用来删除/删除位于桌面上的文件夹的代码:

Option Explicit

Sub deletefiles()
Dim fso As Object
Dim folder 'As String
Dim f
Dim Name As String

On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\Users\My\Desktop\PDFs")  '<-- edit path as required to match your machine

' delete all files in root folder
For Each f In folder.Files
   On Error Resume Next
   Name = f.Name
   f.Delete True

   On Error GoTo 0
Next

' delete all subfolders and files
For Each f In folder.SubFolders
   On Error Resume Next
   Name = f.Name
   f.Delete True

   On Error GoTo 0
Next

folder.Delete

End Sub

宏执行删除文件夹中的文件的工作......除了删除文件夹本身。在我关闭工作簿之前,该文件夹实际上并没有从桌面上消失。

为什么?如何更改代码,这样我就不必先关闭工作簿?

谢谢。

ps:工作宏不必是上面那个。我会接受任何有效的代码。

================================================ =============================

更新

这是创建 PDF、电子邮件和删除的完整代码:

Option Explicit

Sub pdf()
Dim wsA As Worksheet, wbA As Workbook, strTime As String
Dim strName As String, strPath As String
Dim strFile As String
Dim strPathFile As String


'On Error GoTo errHandler

    Set wbA = ActiveWorkbook
    Set wsA = ActiveSheet


'replace spaces and periods in sheet name
    strName = Replace(wsA.Name, " ", "")
    strName = Replace(strName, ".", "_")

'create default name for savng file
    'strPath = "G:\Finance\Corporate Accounting\SHIRLEY\A. Financial Planning Fee Payment Processing\"
    strPath = "C:\Users\My\Desktop\PDFs\"
    strFile = strName    '"_" & strTime & "_" & Sheets("MDM Invoice").Range("B2").Value
    strPathFile = strPath & strFile


Dim myFolder$
myFolder = Environ("UserProfile") & "\Desktop\PDFs"

If Dir(myFolder, vbDirectory) = "" Then
     MkDir myFolder
End If

'export to PDF if a folder was selected
    wsA.ExportAsFixedFormat 0, strPathFile

'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & strPathFile

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub

Sub Send_Email()

Dim FileName As String
Dim strPath As String, strPath2 As String
Dim c As Range
Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim i As Integer
Dim it As String

strPath = Environ("UserProfile") & "\Desktop\PDFs\"
strPath2 = Environ("UserProfile") & "\Desktop\PDFs"

    For Each c In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).Cells
        Set OutLookApp = CreateObject("Outlook.application")
        Set OutLookMailItem = OutLookApp.CreateItem(0)
        With OutLookMailItem
                .To = "logitga@yahoo.com"  'c.Value
                .CC = "Your CC here"
                .Subject = "Your Subject here"
                .HTMLBody = "Your Body content here"
                FileName = Dir(strPath & "*.*")

                .Attachments.Add strPath & FileName
                .Display
                '.Send
        End With
    Next c
On Error Resume Next

Kill "C:\Users\My\Desktop\PDFs\*.*"    ' delete all files in the folder
RmDir "C:\Users\My\Desktop\PDFs"  ' delete folder
End Sub

Sub byby()
'Dim fso
'    Set fso = CreateObject("Scripting.FileSystemObject")
'    fso.DeleteFolder Environ("UserProfile") & "\Desktop\PDFs"

Kill "C:\Users\My\Desktop\PDFs\*.*"    ' delete all files in the folder
RmDir "C:\Users\My\Desktop\PDFs"  ' delete folder
End Sub

【问题讨论】:

  • 忽略此条目。
  • @Jerry,如果您自己找到解决方案,您可以发布自己问题的答案。
  • 我试过这个宏... Option Explicit Sub byby() Dim fso Set fso = CreateObject("Scripting.FileSystemObject") fso.deletefolder Environ("UserProfile") & "\Desktop\PDFs" End Sub 但在我关闭工作簿之前,它也不会完全删除该文件夹。为什么?
  • 工作簿是否位于文件夹中?
  • 调试的第一步是删除/注释所有On Error Resume Next 实例,以便找出问题所在。

标签: vba directory


【解决方案1】:

要删除文件夹,您应该使用RmDir Statement。根据文档:

如果您尝试对包含文件的目录或文件夹使用 RmDir,则会发生错误。在尝试删除目录或文件夹之前,请使用 Kill 语句删除所有文件。

所以你最终会得到这样的例程:

Kill "C:\Users\My\Desktop\PDFs\*.*"    ' delete all files in the folder
RmDir "C:\Users\My\Desktop\PDFs"  ' delete folder

【讨论】:

  • 当我使用 KILL 和 RmDir 命令时,我仍然面临同样的问题,即必须关闭工作簿才能删除父文件夹。
【解决方案2】:

对象与变体

只需将第 5 行和第 6 行更改为:

Dim folder As Object
Dim f As Object

这可能是由于某些复制粘贴或其他原因造成的。

【讨论】:

  • 感谢您的建议。在文件夹从桌面消失之前仍然需要关闭工作簿。
  • 尝试在 'folder.Delete' 之前添加 'MsgBox folder.Attributes' 一行
  • 一切正常。然后我将文件夹的属性设置为只读,无法摆脱它。 folder.Attributes 的结果为 17,文件夹为 16,只读为 1。现在我使用了folder.delete True 并摆脱了文件夹好吧,试试folder.delete True
  • 尝试释放变量:Set f = Nothing:Set folder=Nothing:Set fso = Nothing
  • VBasic2008 :尝试了所有...仍然不行。我的工作簿被占有了!
猜你喜欢
  • 2013-12-15
  • 2010-09-23
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
相关资源
最近更新 更多