VBA文件处理

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
‘
‘‘ OpenTextFile
‘Public 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
‘
‘‘ OpenTextFile
‘Public 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 = NothingEnd Function
时间: 2024-10-03 14:55:47

VBA文件处理的相关文章

VBA 文件操作

'这里我要说明一下,在Excel2010中FileSearch已经失效,CreateObject("Scripting.FileSystemObject")也有问题'获取一个目录下所有的文件Public Function getFiles(folderPath As String) As Variant Dim tempFileName As String 'Variant表示任意类型 Dim fileNames() As Variant '记录fileNames的总长度 Dim fi

Excel VBA(宏):添加宏

写在前面: 1.编写宏,打开VBA,双击ThisWorkbook对当前工作薄进行编写宏:双击Sheet1,对整个sheet编写宏: 或者创建模块,在模块里,编写.调试代码. 打开VBA的方法见第一讲,结合常用窗口进行编写.调试. 2.部分对象有提示,如Dim a As,敲击空格后有提示. 3.所有宏要运行,必须启动宏.(2007版启动宏,点击表格左上角 "excel选项" "信任中心" "信任中心设置" "启用宏") 4.&q

[转载]如何破解Excel VBA密码

原文链接:http://yhf8377.blog.163.com/blog/static/1768601772012102111032840/ 在此之前,先强调一下,这个方法只是用来破解Excel内部的VBA工程,想要破解Excel文件打开密码的同学可以飘过…… 首先,如果文件格式是Excel 2010版(.xslm),需要先打开Excel文件,另存为2003版格式(.xls). 然后用普通的文本编辑器(我用的是NotePad++)打开这个文件,注意文件类型选“所有文件”. 然后在文件里查找“D

Excel-VBA操作文件四大方法之一

在我们日常使用Excel的时候,不仅会用到当前Excel文件的数据,还经常需要访问其他的数据文件.这些数据文件可能是Excel文件.文本文件或数据库文件等.经常有朋友会问如何在vba代码里操作这些数据文件?本文就系统地介绍一下在Excel中应用VBA操作数据文件的方法. 本文主要介绍四种常用的方法:1.利用Excel对象来处理文件:2.利用VBA文件处理语句来处理文件:3.利用FileSystemObject对象来处理文件:4.利用API函数来处理文件. 当然对于数据库文件,还可以利用ADO+S

将Vim改造为强大的IDE

工欲善其事,必先利其器.一个强大的开发环境可以大大提高工作效率.好吧,我知道这是废话...不过,我想一定有很多跟我一样打算进入Linux平台开发的新手,一开始都为找不到一个像Windows下的VS那样可以一键安装并且功能几乎完美无缺的开发工具而郁闷不已,甚至打算收回刚刚迈出的脚步.所幸的是,通过几天努力,我总算配置出了一个功能完备的基于Vim的开发环境.这个开发环境除了基本的Vim外,还包括Ctags,Taglist,Cscope,SuperTab,OmniCppComplete,Winmana

ubuntu下定制Vim/Gvim及使用技巧

vim是linux下的编辑器之神,是玩linux的必备工具,同样emacs是神的编辑器,两个编辑器是各有千秋,看个人的喜好,青菜萝卜各有所爱.我是比较喜欢vim,用vim编写bash,perl,python脚本,以及修改配置文件等等,本文就主要介绍在Ubuntu下gvim环境的搭建,希望对大家有所帮助. 一. Ubuntu安装gvim方法 安装gvim的方法,最简单方法是在Ubuntu软件中心搜索gvim,然后点击安装即可,如下图: 如果使用命令行可以打开终端,输入sudo apt-get in

Outlook账户迁移帮助

Outlook账户迁移指南 1. 介绍 开贴聊聊如何迁移Outlook用户账户到另外一台电脑. 相信许多Outlook用户都遇到过这样的情况:买来一台新电脑,安装完Outlook后,想把旧电脑里面的Outlook所有邮件,联系人,日历,甚至许多个性化配置都想迁移到新电脑的Outlook上.对于大多数熟悉Outlook的用户而言,将数据文件(如PST)等导入/导出到新电脑轻而易举,但有时他们会发现虽然重要的邮件信息都成功保留了,其他平时习以为常的功能却无法正常使用了.本文旨在通过分布详述迁移步骤,

【转】谁说Vim不是IDE?(三)

谁说Vim不是IDE?(三) 常用插件 之所以说Vim形成了自己的生态环境,就是因为Vim具备开放的插件体系,开发者为了提升开发效率,为Vim编写了数以万计的插件,我们可以根据需要任意选择,也可以基于Vimscript语言开发自己的专属插件. 每个插件都是以Vim为后缀的脚本文件,编写插件的语言一般采用Vimscript.Vimscript是Vim自己的编程语言,我们在后续会介绍到它.尽管Vimscript几乎为可以Vim做任何事,但还是会存在一些场景,用其他语言更好更快.考虑到这一点,Vim的

谁说Vim不是IDE?

话 说操作系统.编程语言和编辑器似乎是程序员永恒的吐槽话题,技术发展了几十年,大家讨论起这几个"之争"还是充满愤怒.津津乐道.有人描述过Emacs和 VI程序员,大家沿着不同的道路和目标前进,但总是会在某个点交叉相遇,互相扔石头,砸到对方鼻青脸肿,然后收拾心情又继续前行. 前一阵子我在微博上写了一条微博回复,是关于Vim和IDE讨论的:Vim和那些IDE的应用场景还是有区别的,在我看来Vim也是IDE,是程序员的必备技能之一.这基本表达了我的观点. 原微博是@玉伯也叫射雕发 布的,这条