Moving files via code

C

Conan Kelly

Hello all,

I found this code in a previous post to move files with code:

Sub test()
MoveAllFiles "P:\new-journals", "P:\processed-journals"
End Sub

Sub MoveAllFiles(Source As String, Dest As String)

Dim objFS As New FileSystemObject
Dim objSource As Folder
Dim objDest As Folder
Dim objFile As File

On Error GoTo MoveAllFiles_Err:

If objFS.FolderExists(Source) Then

If objFS.FolderExists(Dest) Then

Set objSource = objFS.GetFolder(Source)
Set objDest = objFS.GetFolder(Dest)

For Each objFile In objSource.Files
objFile.Move (objDest.Path & _
Application.PathSeparator & objFile.Name)

Next
Else
Err.Raise vbObjectError + 1001, "", _
"Folder " & Source & " not found."
End If

Else

Err.Raise vbObjectError + 1001, "", _
"Folder " & Source & " not found."

End If

Exit Sub
MoveAllFiles_Err:
With Err
.Raise .Number, "[MoveAllFiles]" & .Source, _
.Description, .HelpFile, .HelpContext
End With
End Sub


With this example, I can modify this code to get it to do what I want.

The problem that I'm having is that in the line of code:

Dim objFS As New FileSystemObject

it appears that my system doesn't recognize "FileSystemObject" as a
valid data type.
Do I need to reference another library for this to work?

Any help anyone can provide will be greatly appreciated,

Conan Kelly
 
B

Bob Phillips

You have to set a reference to the Microsoft Scripting Runtime library in
VBA. In the VBIDE, go to Tools>References and set it.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top