【发布时间】: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实例,以便找出问题所在。