vbscript 在Excel VBA中读取和写入文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript 在Excel VBA中读取和写入文件相关的知识,希望对你有一定的参考价值。

' Define global variables
Public userName As String

' Change the user as needed in this function
Function CurrentUser() As String
    Dim fileName As String: fileName = ActiveWorkbook.path & "\config.txt"
    Dim fcontent As String
    Dim fileStream As Integer: fileStream = FreeFile
    
    ' File I/O stream
    Open fileName For Input As #fileStream
    fcontent = Input(LOF(fileStream), fileStream)
    Close #fileStream
    
    ' userName is equal to contents of txt file
    userName = fcontent
    CurrentUser = userName
End Function

' Sets the username in config file
Sub SetCurrentUser()
    Dim myInput As Variant: myInput = InputBox("Enter the current username")
    Dim myFile As String: myFile = ActiveWorkbook.path & "\config.txt"
    Dim fileStream As Integer: fileStream = FreeFile
    
    ' File I/O stream
    Open myFile For Output As #fileStream
    Print #fileStream, myInput
    Close #fileStream
End Sub

以上是关于vbscript 在Excel VBA中读取和写入文件的主要内容,如果未能解决你的问题,请参考以下文章