ファイル内のデータを一括で移動またはコピーする。
このサンプルではコピー用にしているため、
[ '操作内容は..]部分で移動に関するコードをコメントブロックしている。
移動に変更するなら、
コピー部分をコメントブロックして、
移動部分のコメントをはずすと良い。
なお、コピー元とコピー先のフォルダのアドレスは、
ダイアログボックスなどで指定するように変更すると良いだろう。
<サンプル>
Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'動作方法を指定
Type SHFILEOPSTRUCT
myHnd As Long 'ダイアログボックスの親ウィンドウのハンドル
myFunc As Long '操作内容を指定する
fileFrom As String '操作元のファイル名、ディレクトリ名
fileTo As String '操作先のファイル名、ディレクトリ名
End Type
'操作内容を指定
Public Const FO_MOVE = &H1& '移動
Public Const FO_COPY = &H2& 'コピー
Sub ファイルを一括にコピーか移動する()
Dim myCopy As SHFILEOPSTRUCT
Dim c As Long
Dim myClass As String 'クラス名
'Excelのクラス名
myClass = "XLMAIN"
With myCopy
'ダイアログボックスの親ウィンドウのハンドル
.myHnd = FindWindow(myClass, Application.Caption)
'操作内容は...
.myFunc = FO_COPY '「コピー」
' .myFunc = FO_MOVE '「移動」
'コピー元のファイルアドレスを指定
.fileFrom = "C:\Documents and Settings\***\デスクトップ\test1\読み込み元" ’←アドレスを入力すること
'コピー先のファイアドレスルを指定
.fileTo = "C:\Documents and Settings\***\デスクトップ\test1\test01" ’←アドレスを入力すること
End With
'実行する
c = SHFileOperation(myCopy)
End Sub