码迷,mamicode.com
首页 > 编程语言 > 详细

VBA文件处理

时间:2014-11-11 10:35:25      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   for   文件   

Option Explicit

 ▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽ Excel对象 △△△△△△△△△△△△△△△△△△

 Open
Public Function FileOpen_ByExcel(ByVal FileName As String, ByRef Target As Workbook) As Boolean

    On Error GoTo OpenFileError
    
    Set Target = Workbooks.Open(FileName:=FileName, ReadOnly:=False)
    FileOpen_ByExcel = True
    Exit Function
    
OpenFileError:
    FileOpen_ByExcel = False
    
End Function

 Save
Public Function FileSave_ByExcel(ByVal FileName As String, ByVal Target As Workbook) As Boolean

    On Error GoTo SaveFileError
    
    If FileName = "" Then
        Target.Save
    Else
        Target.SaveAs FileName:=FileName
    End If
    FileSave_ByExcel = True
    Exit Function
    
SaveFileError:
    FileSave_ByExcel = False
    
End Function


 Close
Public Function FileClose_ByExcel(ByVal Target As Workbook) As Boolean

    On Error GoTo FileCloseError
    
    Target.Close savechanges:=False
    FileClose_ByExcel = True
    Exit Function
    
FileCloseError:
    FileClose_ByExcel = False
    
End Function



 ▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽ FileSystemObject △△△△△△△△△△△△△△△△△△

 Folder CreateFolder
Public Function FolderCreate_ByFSO(ByVal FolderName As String, ByVal DeleteFlg As Boolean) As Boolean

    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    On Error GoTo FolderCreateError
    
    If FSO.FolderExists(FolderName) Then
        If DeleteFlg Then
            FSO.DeleteFolder (FolderName)
        Else
            Set FSO = Nothing
            FolderCreate_ByFSO = True
            Exit Function
        End If
    End If
    
    Dim ParentFolderName As String
    ParentFolderName = FSO.GetParentFolderName(FolderName)
    If FSO.FolderExists(ParentFolderName) = False Then
        If FolderCreate_ByFSO(ParentFolderName, False) = False Then
            GoTo FolderCreateError
        End If
    End If
    
    FSO.CreateFolder (FolderName)
    Set FSO = Nothing
    FolderCreate_ByFSO = True
    Exit Function
    
FolderCreateError:
    Set FSO = Nothing
    FolderCreate_ByFSO = False
    
End Function

 CreateFile
Public Function FileCreate_ByFSO(ByVal FileName As String, ByVal DeleteFlg As Boolean) As Boolean

    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    On Error GoTo FileCreateError
    
    If FSO.FileExists(FileName) Then
        If DeleteFlg Then
            FSO.DeleteFile (FileName)
        Else
            Set FSO = Nothing
            FileCreate_ByFSO = True
            Exit Function
        End If
    End If
    
    Dim ParentFolderName As String
    ParentFolderName = FSO.GetParentFolderName(FileName)
    If FSO.FolderExists(ParentFolderName) = False Then
        If FolderCreate_ByFSO(ParentFolderName, False) = False Then
            GoTo FileCreateError
        End If
    End If
    
    FSO.CreateTextFile (FileName)
    Set FSO = Nothing
    FileCreate_ByFSO = True
    Exit Function
    
FileCreateError:
    Set FSO = Nothing
    FileCreate_ByFSO = False
    
End Function
‘
‘‘ OpenTextFilePublic Function OpenTextFile_ByFSO(ByVal FileName As String) As String
‘
‘    Const ForReading As Integer = 1    Const CreateFlgFalse As Boolean = False
‘
‘    Dim FSO As Object, TextFile As Object, TextStr As String    Set FSO = CreateObject("Scripting.FileSystemObject")
‘
‘    On Error GoTo OpenTextFileError
‘
‘    Set TextFile = FSO.OpenTextFile(FileName, ForReading, CreateFlgFalse)    TextStr = TextFile.Readall
‘
‘    TextFile.Close    Set FSO = Nothing
‘
‘    OpenTextFile_ByFSO = TextStr    Exit Function
‘
‘OpenTextFileError:
‘
‘    TextFile.Close    Set FSO = Nothing    OpenTextFile_ByFSO = ""
‘
‘End Function
‘
‘‘ OpenTextFilePublic Function WriteTextFile_ByFSO(ByVal FileName As String, ByVal Buffer As String) As Boolean
‘
‘    If FileCreate_ByFSO(FileName, True) = False Then        WriteTextFile_ByFSO = False        Exit Function    End If
‘
‘    Const ForWriting As Integer = 2    Const CreateFlgTrue As Boolean = True
‘
‘    Dim FSO As Object, TextFile As Object    Set FSO = CreateObject("Scripting.FileSystemObject")
‘
‘    On Error GoTo OpenTextFileError
‘
‘    Set TextFile = FSO.OpenTextFile(FileName, ForWriting, CreateFlgTrue)    TextFile.Write (Buffer)
‘
‘    TextFile.Close    Set FSO = Nothing
‘
‘    WriteTextFile_ByFSO = True    Exit Function
‘
‘OpenTextFileError:
‘
‘    TextFile.Close    Set FSO = Nothing    WriteTextFile_ByFSO = False
‘
‘End Function

Public Function OpenTextFile_ByADODBStream(FileName As String) As String
    Dim FileBody As String
 
    Dim ADODBStream As Object
    Set ADODBStream = CreateObject("ADODB.Stream")
            
    With ADODBStream
        .Type = 1
        .Mode = 3
        .Open
        .LoadFromFile FileName
        .Position = 0
        .Type = 2
        .Charset = "utf-8"
        FileBody = .ReadText
        .Close
    End With
    
    Set ADODBStream = Nothing
    
    OpenTextFile_ByADODBStream = FileBody
     
End Function

 WriteTextFile_ByADODBStream
Public Function WriteTextFile_ByADODBStream(ByVal OutFileName As String, ByVal Buffer As String) As Boolean

    If FileCreate_ByFSO(OutFileName, True) = True Then
    
        Dim ADODBStream As Object
        Set ADODBStream = CreateObject("ADODB.Stream")
            
        
        With ADODBStream
            .Type = 2
            .Charset = "utf-8"
            .Open
            .WriteText Buffer, 1
            .SaveToFile OutFileName, 2
            .Close
        End With
        
        Set ADODBStream = Nothing
        WriteTextFile_ByADODBStream = True
    Else
        WriteTextFile_ByADODBStream = False
    End If
    
End Function




‘ log

Public Function WriteLog(ByVal LogFilePath As String, ByVal msg As String)
    Dim FSO As Object, LOG As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
   
    ‘
    If FSO.FileExists(LogFilePath) = False Then
        FSO.CreateTextFile (LogFilePath)
    End If
   
    ‘
    Set LOG = FSO.OpenTextFile(LogFilePath, 8)
    ‘
    LOG.WriteLine Now & vbTab & msg
   
    Set LOG = Nothing
    Set FSO = Nothing
End Function

 

VBA文件处理

标签:style   blog   io   color   ar   os   sp   for   文件   

原文地址:http://www.cnblogs.com/WillYang/p/4088791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!