Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何在Visual Basic for PowerPoint 2010中从文本文件填充数组_Vba_Powerpoint_Powerpoint 2010 - Fatal编程技术网

Vba 如何在Visual Basic for PowerPoint 2010中从文本文件填充数组

Vba 如何在Visual Basic for PowerPoint 2010中从文本文件填充数组,vba,powerpoint,powerpoint-2010,Vba,Powerpoint,Powerpoint 2010,我想定义一个数组,如: sample_array = Array( _ "foo", _ "bar", _ ... "dog", _ "cat" _ ) …在用VB为应用程序编写的宏中(本例中为PowerPoint 2010),但我需要从一个文本文件中定义数组,该文件的格式如下: foo bar ... dog cat 定义文本文件路径并将值(假定它们始终是常规ascii字符串)直接读取到数组中的最简单方法是什么 谢谢 您可以一次加载整个文件,并按以下换行将其拆分 Dim arr() as

我想定义一个数组,如:

sample_array = Array( _
"foo", _
"bar", _
...
"dog", _
"cat" _
)
…在用VB为应用程序编写的宏中(本例中为PowerPoint 2010),但我需要从一个文本文件中定义数组,该文件的格式如下:

foo
bar
...
dog
cat
定义文本文件路径并将值(假定它们始终是常规ascii字符串)直接读取到数组中的最简单方法是什么


谢谢

您可以一次加载整个文件,并按以下换行将其拆分

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.
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

您可以一次装入整个文件,并按如下所示的换行将其拆分

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

Redim只能与最初声明的没有固定大小的数组一起使用。因此,您必须调暗arr(),然后稍后再调暗arr(0)(以开始),然后才能在循环中使用Redim。此代码的结构不足以回答问题。问题是如何创建一个长度与文件中项目数匹配的数组。即使此代码已修复,它也将始终在数组末尾创建一个空白项。此脚本应将redim行移到“line input#1,arr(i)”行上方。redim只能与最初声明的没有固定大小的数组一起使用。因此,您必须调暗arr(),然后稍后再调暗arr(0)(以开始),然后才能在循环中使用Redim。此代码的结构不足以回答问题。问题是如何创建一个长度与文件中项目数匹配的数组。即使此代码已修复,它也将始终在数组末尾创建一个空白项。此脚本应将redim行移到“line input#1,arr(i)”行上方。