无法将Word文档正确读取到“集合”中

时间:2019-03-05 16:00:02

标签: arrays vba ms-word

我有一个自定义对象(Item),我试图遍历文档,将文档中的每个句子作为Item的变量添加,并将其添加到数组中。我的问题是,我读的最后一个项目似乎占用了数组中的每个空间。下面的代码:

Sub Main()
'Count Variables
Dim i As Integer, j As Integer

'Variables
Dim My_Question As New Question
'Dim myexcel As Object
'Dim myWB As Object
'Dim ExcelMaster As String
Dim Found As Boolean

'Array declaration
Dim Q_Coll As New Collection
'Dim MasterCodes As New Collection

i = 1
'Reads in Items and store as item objects
'Storing in Array for later sorting
For i = 1 To 3
    My_Question.Stem = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "Stem: " & My_Question.Stem

    My_Question.A = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "A: " & My_Question.A

    My_Question.B = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "B: " & My_Question.B

    My_Question.C = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "C: " & My_Question.C

    My_Question.D = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "D: " & My_Question.D

    My_Question.Master = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "Master: "; My_Question.Master

    My_Question.ICS = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "ICS: " & My_Question.ICS

    My_Question.State = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "State: " & My_Question.State

    My_Question.Key = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "Key: " & My_Question.Key

    Q_Coll.Add Item:=My_Question
Next i

For i = 1 To 3
    Debug.Print Q_Coll(i).Stem
Next i
End Sub

输入
这是一个测试问题?
(A)答A
(B)答案B
(C)答案C
(D)答案D

主代码:ABC12345
ICS:1.A.1.A
州:新
密钥:B

这是另一个要测试的问题吗?
(A)答案1
(B)答案2
(C)答案3
(D)答案4

主代码:ABC54321
ICS:1.B.1.C
州:新
密钥:A

这是最后一个测试问题?
(A)答A1
(B)答案B2
(C)答案C3
(D)回答D4

主代码:XYZ12345
ICS:2.F.1.C
状态:可用
密钥:D

调试输出 茎:这是一个测试问题?

A:(A)答案A

B:(B)回答B

C:(C)回答C

D:(D)答案D

母版:母版代码:ABC12345

ICS:ICS:1.A.1.A

状态:状态:新

键:键:B

Stem:这是另一个要测试的问题吗?

A:(A)答案1

B:(B)答案2

C:(C)答案3

D:(D)回答4

母版:母版代码:ABC54321

ICS:ICS:1.B.1.C

状态:状态:新

键:键:A

Stem:这是最后一个测试问题?

A:(A)回答A1

B:(B)回答B2

C:(C)回答C3

D:(D)回答D4

母版:母版代码:XYZ12345

ICS:ICS:2.F.1.C

状态:状态:可用

键:键:D

这是最后一个测试问题?

这是最后一个测试问题?

这是最后一个测试问题?

我正在阅读时打印出每个字符串,但最终输出只是一遍又一遍地打印最后一个项目。

1 个答案:

答案 0 :(得分:0)

发现了问题。用“ Dim My_Question As Question”替换代码顶部的“ Question”定义,然后在for循环开始时将其设置为“ Set My_Question = New Question”似乎可以修复输出。下面的代码:

Sub Main()
'Count Variables
Dim i As Integer, j As Integer

'Variables
Dim My_Question As Question
Dim Found As Boolean

'Collection declaration
Dim Q_Coll As New Collection

i = 1
'Reads in Items and store as item objects
'Storing in Collection for later sorting
For i = 1 To 15
    Set My_Question = New Question
    My_Question.Stem = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "Stem: " & My_Question.Stem

    My_Question.A = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "A: " & My_Question.A

    My_Question.B = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "B: " & My_Question.B

    My_Question.C = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "C: " & My_Question.C

    My_Question.D = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "D: " & My_Question.D

    My_Question.Master = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "Master: "; My_Question.Master

    My_Question.ICS = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "ICS: " & My_Question.ICS

    My_Question.State = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "State: " & My_Question.State

    My_Question.Key = Selection.Sentences(1).Text
    Selection.Sentences(1).Delete
    Debug.Print "Key: " & My_Question.Key

    Q_Coll.Add Item:=My_Question
Next i

For i = 1 To 15
    Debug.Print Q_Coll(i).Stem
Next i
End Sub