VBA代码逐个读取文本文件中的行

VBA代码逐个读取文本文件中的行

本文介绍了如何使用Power Point VBA代码逐个读取文本文件中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  set file = CreateObject(Scripting.FileSystemObject)。 OpenTextFile(c:\\\
umber.txt,1)
text = file.ReadLine
MsgBox文本

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



重要的补充:我需要代码逐一操作每一行 - 不是一次一个!


pre> text = file.ReadAll

:)



有一个循环:

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

设置fso = CreateObject(Scripting.FileSystemObject)

FileName =c:\testfile.txt

设置MyFile = fso.OpenTextFile(FileName,ForReading)

''读取文件
Do While MyFile.AtEndOfStream<> True
TextLine = MyFile.ReadLine

''向TextLine执行操作

循环
MyFile.Close


This code will read a line from a text file:

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

How can I make it read repeatedly one line after another from the same file? I guess, I should use a loop here, right? I need it to read the first line from the file at the first iteration, the second line at the second iteration, the third one at the third and so on till all the lines have been read. How can I do it?

Important addition: I need the code to operate on each line one by one - not all at once!

解决方案

Use the ReadAll() method:

text = file.ReadAll

(Might be of interest: FileSystemObject Sample Code)

With a loop:

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

这篇关于如何使用Power Point VBA代码逐个读取文本文件中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:11