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. iOS - Xcode升级到5.1& iOS升级到iOS7.1问题:Undefined symbols for architecture x86_64

    Xcode升级到5.1 新特性之一就是默认让所有App都通过64位编译器编译.原来在Xcode5.0.x的时候默认的Standard architectures只有(arm7,armv7s),到5.1 ...

  2. 如何使用Keil仿真环境查看CPU类型字长?【worldsing笔记】

    笔者上次写了如何用IAR查CPU的字长和数据类长度的方法:点击这里查看 今天试着在Keil MDK 5.0 里查看CPU的字长和数据类长度,打开一个已有的工程,编译并进入Debug,如图1.1所示: ...

  3. 在CentOS 6.2上安装 MemcacheQ 最新版

    1.      安装 yum install gcc cc make libevent libevent-devel 2.      安装Berkeley DB 下载:http://www.oracl ...

  4. USACO Feb. 2012

    Moo 找规律 吧 第一个是很久以前自己写的递归 #include<stdio.h> __int64 n; __int64 dfs(__int64 l,__int64 r,__int64 ...

  5. iOS动画详解(一)

    Core Graphics Framework是一套基于C的API框架,使用了Quartz作为绘图引擎.它提供了低级别.轻量级.高保真度的2D渲染.该框架可以用于基于路径的绘图.变换.颜色管理.脱屏渲 ...

  6. iOS开发-相关文档

    关于调试,查看Xcode Overview文档相关部分:http://developer.apple.com/library/ios/documentation/ToolsLanguages/Conc ...

  7. PJax在jQuery 3.0无法运行问题修复

    PJax在jQuery 3.0无法运行 [现象] 页面报错:Uncaught TypeError: Cannot read property 'push' of undefined [原因] jQue ...

  8. JavaScript学习笔记之下拉选择框的操作

    对于下拉框的操作十分繁多,这几天项目须要就总结一下 一.动态构建option 有时候我们须要动态构建下拉选择框里面的值,这里我们就要用到 var varItem = new Option(" ...

  9. SVN安装笔记

    1.先去以下网址去下载服务器端与客户端的SVN 2.服务端SVN的安装 点击next 点击next 再点击next 点击next 点击install 点击Finish,这样服务器端的SVN就安装好了, ...

  10. [React Native] Create a component using ScrollView

    To show a list of unchanging data in React Native you can use the scroll view component. In this les ...