我想创建代码来使用Word 2007中的VBA来表示表单文档。我创建了表示Section,QuestionSet和Question的类。VBA - 如何将集合添加到集合
所以我有15个部分。我创建了一个函数来创建每个'Section'对象,将它添加到'Sections'集合中,然后销毁该对象,结果是对象在集合(或某物)中保持持久性。
是否可以使用相同的方法将集合添加到集合中,还是我必须明确定义每个集合?
代码模块中:
Public Sections As Collection
Function DefineSection(ByVal SectionName As String)
Set Section = New clsSection
Section.myName = SectionName
Sections.Add Section, SectionName
End Function
Function DefineQuestionSet(ByVal SectionName As String, ByVal Name As String, ByVal NoOfQuestions As Integer, ByVal IsMutuallyExclusive As Boolean, Optional ByVal DependentOnSection As String)
Dim Qsets As Collection
Set Qsets = New Collection
Set QuestionSet = New clsQuestionSet
QuestionSet.Name = Name
QuestionSet.NoOfQuestions = NoOfQuestions
QuestionSet.MutuallyExclusive = IsMutuallyExclusive
If Not (DependentOnSection) = "" Then
QuestionSet.DependentOnSection = DependentOnSection
End If
Qsets.Add QuestionSet
Sections.Item(SectionName).Add Qsets
End Function
那么这是通过被称为:
Sub Initilise()
Set Sections = New Collection
DefineSection "PersonalDetails"
DefineQuestionSet "PersonalDetails", "PersonalDetails", 29, False
End Sub
不幸的是我已经改变了代码,现在,尝试只是创建独立的收藏 - 然而我在'DefineQuestionSet'函数中收到了一行 'Sections.Item(SectionName).Add Qsets'中的错误。 – Stevo 2011-02-25 16:07:06
为了澄清,代码将对象添加到集合中,然后销毁对象的实例,但它仍然保留在集合中,因此我不必跟踪对象。这些收藏品是否一样?即创建集合B,添加到父集合A,销毁集合B,但因为它被添加到集合A中,所以它仍然是集合A中的一个'项目'。 – Stevo 2011-02-25 16:12:01
错误消息是永远神秘的'对象不支持此属性或方法' – Stevo 2011-02-25 16:20:49