![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
'
--------------------
' 类模块:FSO
' SielnceNet@qq.Com
' 时间:2008-7-4 15:57:53
' 说明:打开/保存任意编码格式的文本文件
' --------------------
Option Explicit
Private FileBody As String ' 文件内容
Private ErrInfo As String
Private Coding As String
' Dim ADO_Stream As Stream '使用此项需引用ADO对象
Dim ADO_Stream As Object
' 文件打开成功后使用此方法获取文件内容
Public Function GetFileText() As String
GetFileText = FileBody
End Function
' 操作失败时。。使用此方法获取错误信息
Public Function GetErrorInfo() As String
GetErrorInfo = ErrInfo
End Function
' 打开文件
Public Function OpenFile(ByVal FileName As String ) As Boolean
' FileName:需要打开的文件名
If ADO_Stream Is Nothing Then Exit Function
Err.Number = 0
On Error GoTo ferr
With ADO_Stream
.Type = 1
.Mode = 3
.Open
.LoadFromFile FileName
.Position = 0
.Type = 2
.Charset = IIf(Coding = "" , " gb2312 " , Coding)
FileBody = .ReadText
.Close
End With
OpenFile = True
Exit Function
ferr:
ErrInfo = FileName & " 打开失败!错误信息: " & Err.Description
Debug.Print ErrInfo
Err.Number = 0
End Function
' 保存文件
Public Function SaveFile(ByVal FileName As String , ByVal strFileBody As String ) As Boolean
' FileName:文件保存路径名字
' strFileBody:需要保存的文件内容
If ADO_Stream Is Nothing Then Exit Function
Err.Number = 0
On Error GoTo ferr
With ADO_Stream
.Type = 2
.Mode = 3
.Charset = IIf(Coding = "" , " gb2312 " , Coding)
.Open
.WriteText strFileBody
.SaveToFile FileName, 2
End With
SaveFile = True
Exit Function
ferr:
ErrInfo = FileName & " 保存失败!错误信息: " & Err.Description
Debug.Print ErrInfo
Err.Number = 0
End Function
Private Sub Class_Initialize()
Err.Number = 0
On Error GoTo ferr
' Set ADO_Stream = New Stream '使用此项需引用ADO对象
Set ADO_Stream = CreateObject ( " ADODB.Stream " )
Exit Sub
ferr:
Set ADO_Stream = Nothing
ErrInfo = " ADODB.Stream对象创建失败! "
Debug.Print ErrInfo
Err.Number = 0
End Sub
' 设置文件编码
' 缺省编码为gb2312
Public Property Let SetCoding(ByVal vNewValue As String )
Coding = vNewValue
End Property
' 类模块:FSO
' SielnceNet@qq.Com
' 时间:2008-7-4 15:57:53
' 说明:打开/保存任意编码格式的文本文件
' --------------------
Option Explicit
Private FileBody As String ' 文件内容
Private ErrInfo As String
Private Coding As String
' Dim ADO_Stream As Stream '使用此项需引用ADO对象
Dim ADO_Stream As Object
' 文件打开成功后使用此方法获取文件内容
Public Function GetFileText() As String
GetFileText = FileBody
End Function
' 操作失败时。。使用此方法获取错误信息
Public Function GetErrorInfo() As String
GetErrorInfo = ErrInfo
End Function
' 打开文件
Public Function OpenFile(ByVal FileName As String ) As Boolean
' FileName:需要打开的文件名
If ADO_Stream Is Nothing Then Exit Function
Err.Number = 0
On Error GoTo ferr
With ADO_Stream
.Type = 1
.Mode = 3
.Open
.LoadFromFile FileName
.Position = 0
.Type = 2
.Charset = IIf(Coding = "" , " gb2312 " , Coding)
FileBody = .ReadText
.Close
End With
OpenFile = True
Exit Function
ferr:
ErrInfo = FileName & " 打开失败!错误信息: " & Err.Description
Debug.Print ErrInfo
Err.Number = 0
End Function
' 保存文件
Public Function SaveFile(ByVal FileName As String , ByVal strFileBody As String ) As Boolean
' FileName:文件保存路径名字
' strFileBody:需要保存的文件内容
If ADO_Stream Is Nothing Then Exit Function
Err.Number = 0
On Error GoTo ferr
With ADO_Stream
.Type = 2
.Mode = 3
.Charset = IIf(Coding = "" , " gb2312 " , Coding)
.Open
.WriteText strFileBody
.SaveToFile FileName, 2
End With
SaveFile = True
Exit Function
ferr:
ErrInfo = FileName & " 保存失败!错误信息: " & Err.Description
Debug.Print ErrInfo
Err.Number = 0
End Function
Private Sub Class_Initialize()
Err.Number = 0
On Error GoTo ferr
' Set ADO_Stream = New Stream '使用此项需引用ADO对象
Set ADO_Stream = CreateObject ( " ADODB.Stream " )
Exit Sub
ferr:
Set ADO_Stream = Nothing
ErrInfo = " ADODB.Stream对象创建失败! "
Debug.Print ErrInfo
Err.Number = 0
End Sub
' 设置文件编码
' 缺省编码为gb2312
Public Property Let SetCoding(ByVal vNewValue As String )
Coding = vNewValue
End Property