【问题标题】:Can't read Word doc into Collection properly无法正确将 Word 文档读入 Collection
【发布时间】:2019-07-27 03:49:49
【问题描述】:

我有一个自定义对象 (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

调试输出 Stem:这是一道测试题?

A:(A)答案 A

B:(B) 答案 B

C: (C) 答案 C

D: (D) 答案 D

Master:Master Code:ABC12345

ICS:ICS:1.A.1.A

状态:状态:新

键:键:B

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

答:(A) 答案 1

B:(B) 答案 2

C: (C) 答案 3

D: (D) 答案 4

Master:Master Code:ABC54321

ICS:ICS:1.B.1.C

状态:状态:新

键:键:A

Stem:这是最后一道测试题?

A:(A) 答案 A1

B:(B) 答案 B2

C: (C) 答案 C3

D:(D)答案 D4

Master:Master Code:XYZ12345

ICS:ICS:2.F.1.C

状态:状态:可用

键:键:D

这是最后一道测试题吗?

这是最后一道测试题吗?

这是最后一道测试题吗?

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

【问题讨论】:

  • 什么是Item 对象?您是否定义了一个名为Item 的类? Item 被 VBA 用于许多事情 - 创建具有该名称的对象/类不是一个好主意。无论如何,这不是minimal reproducible example——为了测试它,我们还需要定义对象(minimal reproducible example)。如果它很大,请创建一个较小的“测试用例”。您可以使用edit 链接向原始问题添加信息。
  • 文档中是否有足够的句子用于此(9x15)?
  • 考虑使用集合而不是数组,它会让你的生活更轻松。 'Set ItemArr(i)=Item' 行将变为 'ItemColl.add Item'。集合可以像数组一样访问,例如设置 my_item = ItemColl.Item(i)。 Item 是 Collection 类的一个方法,因此您可以了解为什么有人建议您使用 Item 作为变量名不是一个好主意。您可以使用“For Each my_item in ItemColl”来遍历集合
  • @LordAstrotrain 您还需要注意 VBA 不知道什么是语法句子。例如,考虑以下情况:Mr.史密斯在约翰博士的杂货店花了 1,234.56 美元购买: 10.25 公斤土豆; 10公斤鳄梨;和15.1公斤格林夫人的Mt. Pleasant澳洲坚果。对你和我来说,这将算作一句话;对于 VBA,它算作 5 个句子。基本上,VBA 的“句子”停在第一个标点符号处。

标签: arrays vba ms-word


【解决方案1】:

发现问题。用“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

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    相关资源
    最近更新 更多