Option Explicit
Sub MoveMyFile()
Dim sSource As String
Dim sDestination As String
sSource = ThisDocument.Path & "\Test.doc"
sDestination = ThisDocument.Path & "\Test\Test Moved.doc"
Call FSOMoveFile(sSource, sDestination, False)
End Sub
Public Sub FSOMoveFile(sSource As String, _
sDestination As String, _
bOverwrite As Boolean)
Dim FSO As Object
On Error GoTo Oops
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not fFileExists(FSO, sSource) Then
MsgBox "Source file: " & sSource & " doesn't exist", vbCritical
GoTo ExitOops
End If
If fFileExists(FSO, sDestination) And bOverwrite Then
With FSO
.DeleteFile (sDestination)
.MoveFile sSource, sDestination
End With
ElseIf fFileExists(FSO, sDestination) And Not bOverwrite Then
GoTo ExitOops
Else
FSO.MoveFile sSource, sDestination
End If
ExitOops:
Set FSO = Nothing
Exit Sub
Oops:
MsgBox "Error # " & Str(Err.Number) & " " & Err.Description
Resume ExitOops
End Sub
Public Function fFileExists(FSO As Object, sPath As String)
If FSO.FileExists(sPath) Then fFileExists = True
End Function
|