gpt4 book ai didi

file-io - 如何使用Power Point VBA代码从文本文件中逐行读取行?

转载 作者:行者123 更新时间:2023-12-02 17:47:50 25 4
gpt4 key购买 nike

此代码将从文本文件中读取一行:

set file = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\number.txt", 1)
text = file.ReadLine
MsgBox text

如何让它从同一文件中一行一行地重复读取?我想,我应该在这里使用循环,对吗?我需要它在第一次迭代时读取文件中的第一行,在第二次迭代时读取第二行,在第三次迭代时读取第三行,依此类推,直到读取所有行。我该怎么做?

重要补充:我需要代码逐行操作 - 不是一次全部操作!

最佳答案

使用ReadAll()方法:

text = file.ReadAll

(可能感兴趣:FileSystemObject Sample Code)

带循环:

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, MyFile, FileName, TextLine

Set fso = CreateObject("Scripting.FileSystemObject")

FileName = "c:\testfile.txt"

Set MyFile = fso.OpenTextFile(FileName, ForReading)

'' Read from the file
Do While MyFile.AtEndOfStream <> True
TextLine = MyFile.ReadLine

'' Do stuff to TextLine

Loop
MyFile.Close

关于file-io - 如何使用Power Point VBA代码从文本文件中逐行读取行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1719342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com