''ごみ箱に送るためのAPI
Private Declare Function SHFileOperation Lib "shell32.dll" _
(lpFileOp As SHFILEOPSTRUCT) As Long
''SHFileOperation関数に渡すユーザー定義型
Private 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 String ''ダイアログのタイトル
End Type
Private Const FO_DELETE = &H3 ''削除する
Private Const FOF_ALLOWUNDO = &H40 ''ごみ箱に送る
Sub DeleteFile()
Dim SH As SHFILEOPSTRUCT, re As Long, Target As String
Target = Application.GetOpenFilename(Title:="削除するファイルを選択してください")
If Target = "False" Then Exit Sub
With SH
.hwnd = Application.hwnd
.wFunc = FO_DELETE
.pFrom = Target
.fFlags = FOF_ALLOWUNDO
End With
re = SHFileOperation(SH)
If re <> 0 Then MsgBox "削除に失敗しました", vbExclamation
End Sub