VBA - 如何将集合添加到集合的集合中
我正在尝试在 Word 2007 中使用 VBA 创建代码来表示表单文档。我已经创建了类来表示部分、问题集和问题。
所以我有 15 个部分。我创建了一个函数来创建每个“部分”对象,将其添加到“部分”集合中,然后销毁该对象,结果是对象在集合(或其他内容)中保持持久状态。
是否可以使用相同的方法将集合添加到集合中,或者我是否必须显式定义每个集合?
模块中的代码:
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
I am trying to create code to represent a form document using VBA in Word 2007. I have created classes to represent Section, QuestionSet and Question.
So I have 15 Sections. I have created a function to create each 'Section' Object add it to the 'Sections' Collection then destroy the object, the result being that the objects remain persistent in the collection (or something).
Is it possible to use the same method to add collections to collections or would I have to define each collection explictly?
Code in Module:
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
Then this is called via:
Sub Initilise()
Set Sections = New Collection
DefineSection "PersonalDetails"
DefineQuestionSet "PersonalDetails", "PersonalDetails", 29, False
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是。您绝对可以无限地添加集合到集合到集合。您发布的代码看起来只需浏览一下就应该可以工作。您有具体问题吗?
更新:VBA 仅传递对对象的引用。
如果在将对象分配给集合后显式销毁该对象(例如,Set myObj = Nothing
),那么您也将销毁集合内的对象。< /strike>[编辑]:显然这不是真的。来自此网站(首先由 Stevo 在评论中链接):
更新:没有理由不能将集合添加到对象中。您只需要实例化对象的类即可支持此类方法。例如,在
clsSection
类模块中,您需要一个Add
方法,该方法将传递给它的对象添加到存储在clsSection
中的集合中:Yes. You can absolutely add collections to collections to collections ad infinitum. The code you have posted looks like it should work just from glancing through it. Are you having specific problems?
UPDATE: VBA only passes around references to objects.
If you explicitly destroy an object after assigning it to a collection (eg,Set myObj = Nothing
) then you will also be destroying the object inside the collection.[EDIT]: Apparently this is not true. From this website (first linked by Stevo in the comments):
UPDATE: There's no reason why you can't add a collection to an object. You just need the class your object is instantiated from to support such a method. For example, in your
clsSection
class module you need anAdd
method which adds objects passed to it to a collection stored in theclsSection
:试试这个简单的例子:
follow 函数返回一个初始化的集合
Try this simple example:
The follow function return a initializated collection
我想我现在有了答案,但如果有人能澄清的话将会有所帮助。
我认为代码正在做的是尝试将集合添加到对象中 - 这显然是行不通的。所以我需要创建一个集合,我可以将对象和集合添加到其中。 ETC。
I think I have the answer now, but if someone could clarify that would be helpful.
I think what the code is doing is trying to add a collection to an object - which obviously won't work. So I need to create a collection which I can add the object AND the collection to. etc.