我想定义一个数组,如:
sample_array = Array( _
"foo", _
"bar", _
...
"dog", _
"cat" _
)
...在用 VB 编写的应用程序宏(在本例中为 PowerPoint 2010)中,但我需要从文本文件中定义数组,该文件的格式如下:
foo
bar
...
dog
cat
定义文本文件路径并将值(假设它们始终是常规 ascii 字符串)直接读取到数组中的最简单方法是什么?
谢谢!
我想定义一个数组,如:
sample_array = Array( _
"foo", _
"bar", _
...
"dog", _
"cat" _
)
...在用 VB 编写的应用程序宏(在本例中为 PowerPoint 2010)中,但我需要从文本文件中定义数组,该文件的格式如下:
foo
bar
...
dog
cat
定义文本文件路径并将值(假设它们始终是常规 ascii 字符串)直接读取到数组中的最简单方法是什么?
谢谢!
您可以一次加载整个文件并用换行符将其拆分,如下所示
Sub read_whole_file()
Dim sFile As String, sWhole As String
Dim v As Variant
sFile = "C:\mytxtfile.txt"
Open sFile For Input As #1
sWhole = Input$(LOF(1), 1)
Close #1
v = Split(sWhole, vbNewLine)
End Sub
Dim arr() as String
dim i as Integer
i=0
Open "c:\test.txt" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, arr(i) ' read next line from file and add text to the array
i=i+1
redim preserve arr(i) ' Redim the array for the new element
Loop
Close #1 ' Close file.