如何从保存的文件中保存文本

时间:2017-10-17 06:11:38

标签: vb6

我正在为自己的目的制作一个数字生成器。 我已经使用它来使用这些功能:   - 保存生成的数字

我想要什么功能:   - 当我关闭它时,从记事本加载最后生成的数字。

这是代码:

Private Const FilePath As String = "C:\Users\sto0007404\Documents\Numbers.txt"
Private CurrentNumber As Long

Private Sub Command1_Click()
    CurrentNumber = CurrentNumber + 1
    txtRefNo.Text = "EM" & Format(CurrentNumber, String(4, "0"))
End Sub

Private Sub Form_Load()
    Dim TextFileData As String, MyArray() As String, i As Long

    ' Open file as binary
    Open "FilePath" For Binary As #1

    ' Read entire file's data in one go
    TextFileData = Space$(LOF(1))
    Get #1, , TextFileData

    ' Close File
    Close #1

    ' Split the data in separate lines
    MyArray() = Split(TextFileData, vbCrLf)

    For i = 0 To UBound(MyArray())
        ' Set CurrentNumber equal to the current max
        CurrentNumber = Val(Mid$(MyArray(i), 2))
    Next
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Dim i As Long

    ' delete the old file
    If Not LenB(Dir(FilePath)) = 0 Then Kill FilePath

    'open the file for writing
    Open FilePath For Output As #1

    For i = 1 To CurrentNumber
        Write #1, "EM" & Format(i, String(4, "0"))
    Next

    'close the file (if you dont do this, you wont be able to open it again!)
    Close #1
End Sub

1 个答案:

答案 0 :(得分:0)

独立使用二进制模式...

打开文件输出为#1 打印#1,“一些文字” 关闭#1

打开文件输入为#1 行输入#1,myvariable 关闭#1

msgbox myvariable

流程与您显示的相同,那么问题是什么?

相关问题