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中读取和写入文件的主要内容,如果未能解决你的问题,请参考以下文章

如何用vba读取多个txt文件名和txt文件内容写入excel中?

excel vba 的读取列,写入列,循环和if 的一些操作

利用VBA向excel表格中写入数据后,再次运行程序的话写入速度会非常慢

VBA SQL写入语句,从一个表格写入令一个表格?

vbscript [Excel VBA中的时间戳]在Excel VBA #Excel VBA中创建时间戳字符串

vbscript VBA - 在Excel中格式化SAP QB数据