File Manipulation from VBA
Following are some of the functions that you can frequently use to copy, move and rename your file from VBA. You can either copy the following code directly in a module or alternatively download the attached file. For renaming a file there are two methods given for this...Try this out!
Option Explicit Function blnFileExists(strFile As String) As Boolean Dim objFileSystem As Object Set objFileSystem = CreateObject("Scripting.FileSystemObject") If objFileSystem.FileExists(strFile) Then blnFileExists = True Else blnFileExists = False End If Set objFileSystem = Nothing End Function Sub CopyFile(strFileToCopy As String, strTargetFolder As String) Dim objFileSystem As Object Dim strFileName As String strFileName = Mid(strFileToCopy, InStrRev(strFileToCopy, "\") + 1, 9999) ' "test.xls" ' change to match the strFileName name strTargetFolder = strTargetFolder & "\" Set objFileSystem = CreateObject("Scripting.FileSystemObject") If Not blnFileExists(strFileToCopy) Then MsgBox strFileName & " does not exist!", vbExclamation, "Source File Missing" ElseIf Not objFileSystem.FileExists(strTargetFolder & strFileName) Then objFileSystem.CopyFile (strFileToCopy), strTargetFolder, True Else MsgBox strTargetFolder & "\" & strFileName & " already exists!", vbExclamation, "Destination File Exists" End If Set objFileSystem = Nothing End Sub Sub MoveFile(strFileToMove As String, strTargetFolder As String) Dim objFileSystem As Object Dim strFileName As String strFileName = Mid(strFileToMove, InStrRev(strFileToMove, "\") + 1, 9999) strTargetFolder = strTargetFolder & "\" Set objFileSystem = CreateObject("Scripting.FileSystemObject") If Not blnFileExists(strFileToMove) Then MsgBox strFileName & " does not exist!", vbExclamation, "Source File Missing" ElseIf Not objFileSystem.FileExists(strTargetFolder & strFileName) Then objFileSystem.CopyFile (strFileToMove), strTargetFolder Else MsgBox strTargetFolder & "\" & strFileName & " already exists!", vbExclamation, "Destination File Exists" End If Set objFileSystem = Nothing End Sub Sub RenameFile1(strCompleteFilePath As String, strNewFileName As String) Dim strContainingFolder As String strContainingFolder = Left(strCompleteFilePath, InStrRev(strCompleteFilePath, "\")) Name strCompleteFilePath As strContainingFolder & strNewFileName End Sub Sub RenameFile2(strCompleteFilePath As String, strNewFileName As String) Dim strContainingFolder As String Dim objFileSystem As Object strContainingFolder = Left(strCompleteFilePath, InStrRev(strCompleteFilePath, "\")) Set objFileSystem = CreateObject("Scripting.FileSystemObject") objFileSystem.MoveFile strCompleteFilePath, strContainingFolder & strNewFileName Set objFileSystem = Nothing End Sub
Attachment | Size |
---|---|
FileManipulation.xls | 37 KB |
»
- Vishesh's blog
- Login or register to post comments
- 21986 reads
One function to check both
One function to check both file and folder if they exist...
Public Function FileFolderExists(strFullPath As String) As Boolean
On Error GoTo EarlyExit
If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
EarlyExit:
On Error GoTo 0
End Function