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 =
.Mode =
.Open
.LoadFromFile FileName
.Position =
.Type =
.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 =
.Charset = "utf-8"
.Open
.WriteText Buffer,
.SaveToFile OutFileName,
.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文件处理的更多相关文章

  1. vim之vba文件

    [vim之vba文件] Vimball 官方描述: The vimball plugin facilitates creating, extracting , and listing the cont ...

  2. VBA文件对话框的应用(VBA打开文件、VBA选择文件、VBA选择文件夹)

    在VBA中经常要用到文件对话框来进行打开文件.选择文件或选择文件夹的操作.用Microsoft Office提供的文件对话框比较方便.用法如下Application.FileDialog(fileDi ...

  3. VBA文件操作

    做这些东西主要是为了,实现,我们的最终目标. 查到 两个大表里面的变化数据. 所以 这次 ①实现了 文件操作的一部分内容. 包括,excel的打开.分四个步骤. 1.路径 2.打开工作博 3.操作 4 ...

  4. vba文件对比并高亮显示

    每月月底要和人事要离职人员名单,并账号列表里删除已经离职人员的账号,如下代码通过将账号列表与人事发来的离职清单进行对比,高亮找出离职人员的账号,并进行删除. Sub DeleteMain() Dim ...

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

    原文链接:http://yhf8377.blog.163.com/blog/static/1768601772012102111032840/ 在此之前,先强调一下,这个方法只是用来破解Excel内部 ...

  6. 将excel按照某一列拆分成多个文件(方案整理)

    1解决方案:将excel按照某一列拆分成多个文件 https://blog.csdn.net/ntotl/article/details/79141314 2遇到的问题:解决vbe6ext.olb不能 ...

  7. 如何破解Excel VBA密码

    首先,如果文件格式是(.xslm),需要先打开Excel文件,另存为2003版格式(.xls). 然后用普通的文本编辑器(我用的是NotePad++)打开这个文件,注意文件类型选“所有文件”. 然后在 ...

  8. Excel VBA(宏):添加宏

    写在前面: .编写宏,打开VBA,双击ThisWorkbook对当前工作薄进行编写宏:双击Sheet1,对整个sheet编写宏: 或者创建模块,在模块里,编写.调试代码. 打开VBA的方法见第一讲,结 ...

  9. VIM插件攻略

    工欲善其事,必先利其器.一个强大的开发环境可以大大提高工作效率.好吧,我知道这是废话...不过,我想一定有很多跟我一样打算进入Linux平台开发的新手,一开始都为找不到一个像Windows下的VS那样 ...

随机推荐

  1. ListView自定义适配器--10.17

    1. 添加button 2. ViewHolder 优化性能 就是一个持有者的类,他里面一般没有方法,只有属性,作用就是一个临时的储存器,把你getView方法中每次返回的View存起来,可以下次再用 ...

  2. Windows性能监视器之CPU、硬盘、IO等监控方法详解-摘自网络

    一般操作系统性能主要涉及到的问题主要有:处理器使用情况.内存占有量.磁盘I/0操作以及网络流量等. 查看Windows性能情况,大部分情况下是通过 “Windows任务管理器”,可以通过在 ”命令行” ...

  3. CSS层叠样式选择器归纳

     常用选择器 1.1 类型选择器:用来寻找特定类型的元素  标签 { 声明 } p { color:black; } h1 { font-weight:bold; } 1.2 后代选择器: 选择一个元 ...

  4. 剑指OFFER之顺时针打印矩阵(九度OJ1391)

    题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2 ...

  5. eclipse内使用tomcat项目究竟被部署到了哪里

    笔者在使用eclipse+tomcat做本地调试,项目没跑起来,原因就很奇怪啊(某前辈说过:奇怪源于无知),然后就想它究竟是把项目放到哪个目录下呢,我的tomcat/webapps目录下并没有啊. 默 ...

  6. [Java 7][msvcr100.dll] Error when load Eclipse

    [Problem] After I updated to Java 7, I could not load Eclipse. Here comes the erros: Eclipse: eclips ...

  7. 从零开始学android开发-sqlitepro安装

  8. HDU 5477 A Sweet Journey 水题

    A Sweet Journey Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. 如何用JAVA生成注册序列号

    原文:http://blog.csdn.net/eagleking012/article/details/7099900 平常我们都接触过软件注册,输入序列号.激活码.注册码.授权码:对于这些字符码到 ...

  10. android135 360 来电去电归属地显示,自定义toast,

    点击会开启服务. sivAddress.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) ...