Operaciones Con Archivos Y Carpetas Den Macros

Páginas: 4 (909 palabras) Publicado: 11 de enero de 2013
The following procedures demonstrate creating, copying, moving and deleting of files and folders using VBA:

I know it's long, but it's worth it!

Let's start with the File ProceduresCheck if a file exists
CODE
Sub FileExists()
Dim fso
Dim file As String
file = "C:\Test.xls" ' change to match the file w/Path
Set fso = CreateObject("Scripting.FileSystemObject")
If Notfso.FileExists(file) Then
MsgBox file & " was not located.", vbInformation, "File Not Found"
Else
MsgBox file & " has been located.", vbInformation, "File Found"
End If
End Sub

Copy a file if itexists
CODE
Sub CopyFile()
Dim fso
Dim file As String, sfol As String, dfol As String
file = "test.xls" ' change to match the file name
sfol = "C:\" ' change to match the source folder pathdfol = "E:\" ' change to match the destination folder path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(sfol & file) Then
MsgBox sfol & file & " does not exist!",vbExclamation, "Source File Missing"
ElseIf Not fso.FileExists(dfol & file) Then
fso.CopyFile (sfol & file), dfol, True
Else
MsgBox dfol & file & " already exists!", vbExclamation,"Destination File Exists"
End If
End Sub

Move a file if it exists
CODE
Sub MoveFile()
Dim fso
Dim file As String, sfol As String, dfol As String
file = "test.xls" ' change to match the file namesfol = "C:\" ' change to match the source folder path
dfol = "E:\" ' change to match the destination folder path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(sfol & file)Then
MsgBox sfol & file & " does not exist!", vbExclamation, "Source File Missing"
ElseIf Not fso.FileExists(dfol & file) Then
fso.MoveFile (sfol & file), dfol
Else
MsgBox dfol &file & " already exists!", vbExclamation, "Destination File Exists"
End If
End Sub

Delete a file if it exists
CODE
Sub DeleteFile()
Dim fso
Dim file As String
file = "C:\test.xls" ' change to...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Archivos y carpetas
  • Carpetas y archivos
  • carpeta de archivos
  • Archivos y carpetas
  • carpetas y archivos
  • Carpeta Operativa
  • Operaciones Con Archivos
  • archivo 2 carpeta

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS