封装存取文本文件的一个例子
Private FileName_ As String '文件名
Private FileNumber_ As Variant '文件编号
Public Enum IOType
ForInput = 1
ForOutPut = 2
ForBinary = 3
End Enum
Private OpenType_ As IOType
Private OutPutText_ As Variant
Property Get OpenType() As IOType '读取打开类型属性
OpenType = OpenType_
End Property
Property Let OpenType(NewValue As IOType) '设置打开类型属性
OpenType_ = NewValue
End Property
Property Get FileName() As String '读取文件路径属性
FileName = FileName_
End Property
Property Let FileName(NewFileName As String) '读取文件路径属性
FileName_ = NewFileName
End Property
Property Get FileNumber() As Variant '读取文件编号属性
FileNumber = FileNumber_
End Property
Property Let FileNumber(NewFileNumber As Variant) '设置文件编号属性
FileNumber_ = NewFileNumber
End Property
Property Get OutPutText() As Variant '设置写入文件内容
OutPutText = OutPutText_
End Property
Property Let OutPutText(NewText As Variant) '读取写入文件内容
OutPutText_ = NewText
End Property
Public Function Enter() As Variant '方法
If OpenType = ForInput Then
Enter = For_Input
ElseIf OpenType = ForOutPut Then
For_Oput
' Else
End If
End Function
Private Function For_Input() As Variant '读取文件
Dim Str As Variant
Open FileName_ For Input As #FileNumber
While Not EOF(1)
Line Input #1, Str
For_Input = For_Input + Str + vbCrLf
Wend
Close #FileNumber
End Function
Private Function For_Oput() '写入文件
Open FileName_ For Output As #FileNumber
Print #FileNumber, OutPutText_
Close #FileNumber
End Function