ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Moving files via code (https://www.excelbanter.com/excel-programming/346205-moving-files-via-code.html)

Conan Kelly

Moving files via code
 
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



Bob Phillips[_6_]

Moving files via code
 
You have to set a reference to the Microsoft Scripting Runtime library in
VBA. In the VBIDE, go to ToolsReferences and set it.

--

HTH

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


"Conan Kelly" <CTBarbarin at msn dot com wrote in message
...
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






All times are GMT +1. The time now is 04:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com