Private Sub btnDelete_Enter()
Dim oldname As String
oldname = ""
oldname = Me.txtAttachmentPath & Me.txtAttachmentName
' MsgBox oldname
If MsgBox("您确认要删除这个文件吗? 删除的文件在您电脑的回收站,可以恢复!!", vbOKCancel + vbInformation, "提示!!!!") = vbOK Then
'文件夹路径及文件名称你可自己更改,删除的文件至回收站,需要恢复文件的到回收站还原即可
'如果文件已经删除了,再次删除则不会出现错误提示而是退出
If Dir(oldname) = "" Then
Exit Sub
End If
DeleteFiles oldname
MsgBox "文件已删除了", vbInformation, "提示:"
If oldname = Null Then
End If
End If
End Sub
Private Sub btnReplace_Enter()
Dim oldname As String
oldname = ""
oldname = Me.txtAttachmentPath & Me.txtAttachmentName
' MsgBox oldname
If MsgBox("您确认要替换这个文件吗? 被替换的文件在您电脑的回收站,可以恢复!!", vbOKCancel + vbInformation, "提示!!!!") = vbOK Then
'文件夹路径及文件名称你可自己更改,删除的文件至回收站,需要恢复文件的到回收站还原即可
'如果文件已经删除了,再次删除则不会出现错误提示而是退出
If Dir(oldname) = "" Then
Exit Sub
End If
DeleteFiles oldname
MsgBox "该文件将被删除", vbInformation, "提示:"
If oldname = Null Then
End If
End If
End Sub
'声明
Public Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As Long
End Type
Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Public Const FO_Delete = &H3
Public Const FOF_ALLOWUNDO = &H40
Public Const HWND_DESKTOP = 0
Public Const NOCONFIRMATION = &H10 '不提示
'函数定义
Function DeleteFiles(Path As String) '定义删除文件函数
Dim Shop As SHFILEOPSTRUCT
With Shop
.hwnd = HWND_DESKTOP
.pTo = ""
.wFunc = FO_Delete
.pFrom = Path + Chr(0)
.fFlags = FOF_ALLOWUNDO + NOCONFIRMATION
End With
SHFileOperation Shop
End Function