Private Sub Command1_Click()CommonDialog1.Filter = "文本文件(*.txt)|*.txt"
CommonDialog1.ShowOpen
file = CommonDialog1.FileName
If file <> "" Then
Form1.Caption = file
Open file For Input As #1
Text1.Text = Input(LOF(1), 1)
Close #1
End IfEnd Sub问:为什么我运行它,打开一个较小的文本文件(.txt)的,总是出错!!!!错误如下: 实时错误 "62"
输入超出文件尾
CommonDialog1.ShowOpen
file = CommonDialog1.FileName
If file <> "" Then
Form1.Caption = file
Open file For Input As #1
Text1.Text = Input(LOF(1), 1)
Close #1
End IfEnd Sub问:为什么我运行它,打开一个较小的文本文件(.txt)的,总是出错!!!!错误如下: 实时错误 "62"
输入超出文件尾
CommonDialog1.Filter = "文本文件(*.txt)|*.txt"
Dim str As String
CommonDialog1.ShowOpen
file = CommonDialog1.FileName
If file <> "" Then
Form1.Caption = file
Open file For Input As #1
Do Until EOF(1)
Line Input #1, str
Text1.Text = Text1.Text & str
Loop
Close #1
End If
End Sub
这段代码可以打开,不过如果文件很大会很慢甚至没有响应。
你那个代码如果打开不含有汉字得txt 文件没有文体,那是因为汉字得编码文体(一个汉字是一个字节还是两个)
String, Optional Append As Boolean = False) As Boolean
`PURPOSE: Loads file specified by FilePath into textcontrol
`(e.g., Text Box, Rich Text Box) specified by TxtBox`If Append = true, then loaded text is appended to existing
` contents else existing contents are overwritten`Returns: True if Successful, false otherwiseDim iFile As Integer
Dim s As StringIf Dir(FilePath) = "" Then Exit FunctionOn Error GoTo ErrorHandler:
s = TxtBox.TextiFile = FreeFile
Open FilePath For Input As #iFile
s = Input(LOF(iFile), #iFile)
If Append Then
TxtBox.Text = TxtBox.Text & s
Else
TxtBox.Text = s
End IfLoadFileToTB = TrueErrorHandler:
If iFile > 0 Then Close #iFileEnd FunctionPublic Function SaveFileFromTB(TxtBox As Object, _
FilePath As String, Optional Append As Boolean = False) _
As Boolean
`PURPOSE: Saves contents of text control (e.g., Text Box,
`Rich Text Box) specified by TxtBox to file specified by FilePath`If Append = true, then TxtBox`s contents are
`appended to existing file contents
`else existing file is overwritten`Returns: True if Successful, false otherwiseDim iFile As IntegeriFile = FreeFile
If Append Then
Open FilePath For Append As #iFile
Else
Open FilePath For Output As #iFile
End IfPrint #iFile, TxtBox.Text
SaveFileFromTB = TrueErrorHandler:Close #iFile
End Function
String, Optional Append As Boolean = False) As Boolean
`PURPOSE: Loads file specified by FilePath into textcontrol
`(e.g., Text Box, Rich Text Box) specified by TxtBox`If Append = true, then loaded text is appended to existing
` contents else existing contents are overwritten`Returns: True if Successful, false otherwiseDim iFile As Integer
Dim s As StringIf Dir(FilePath) = "" Then Exit FunctionOn Error GoTo ErrorHandler:
s = TxtBox.TextiFile = FreeFile
Open FilePath For Input As #iFile
s = Input(LOF(iFile), #iFile)
If Append Then
TxtBox.Text = TxtBox.Text & s
Else
TxtBox.Text = s
End IfLoadFileToTB = TrueErrorHandler:
If iFile > 0 Then Close #iFileEnd FunctionPublic Function SaveFileFromTB(TxtBox As Object, _
FilePath As String, Optional Append As Boolean = False) _
As Boolean
`PURPOSE: Saves contents of text control (e.g., Text Box,
`Rich Text Box) specified by TxtBox to file specified by FilePath`If Append = true, then TxtBox`s contents are
`appended to existing file contents
`else existing file is overwritten`Returns: True if Successful, false otherwiseDim iFile As IntegeriFile = FreeFile
If Append Then
Open FilePath For Append As #iFile
Else
Open FilePath For Output As #iFile
End IfPrint #iFile, TxtBox.Text
SaveFileFromTB = TrueErrorHandler:Close #iFile
End Function
用它的loadfile