保存一个类对象或集合或常规对象,然后再次加载

时间:2019-05-03 10:55:13

标签: excel vba

在我的VBA项目中,我创建了一个自定义类,该类具有不同的参数。其中一个参数的类型为“ Collection”,一个参数的类型为clsnode(自定义类对象),它们基本上存储树视图的所有Nodes / Root Nodes。现在,设置完所有参数后,我想保存所有这些设置,以便当我重新打开工作簿时,它将读取并加载它们。

我尝试创建Type对象并将其另存为bin文件,但是随后遇到此编译错误:“无法获取或放置对象引用变量或包含对象引用的用户定义类型的变量”

下面是我在模块中编写的代码:

Type TreeData
   ActiveNode As clsNode
   AppName As String
   Changed As Boolean
   CheckBoxes As Boolean
   EnableLabelEdit As Boolean
   FullWidth As Boolean
   Indentation As Single
   LabelEdit As Long
   LineColor As Long
   MoveCopyNode As clsNode
   NodeHeight As Single
   Nodes As New Collection
   RootButton As Boolean
   RootNodes As New Collection
   ShowExpanders As Boolean  
End Type

Function readRoot() As TreeData
    Dim fileName As String, fileNo As Integer, takeRoot As TreeData
    fileName = "C:\Users\xxx\Documents\test.bin"
    fileNo = FreeFile
    Open fileName For Binary Lock Read As #fileNo
    Get #fileNo, , takeRoot
    'Recovered the TreeData to testVar
    readRoot = takeRoot
    Close #fileNo
End Function


Sub SaveRoot(ActiveNode As clsNode, AppName As String, Changed As Boolean, CheckBoxes As Boolean, EnableLabelEdit As Boolean, FullWidth As Boolean, Indentation As Single, LabelEdit As Long, LineColor As Long, MoveCopyNode As clsNode, NodeHeight As Single, RootButton As Boolean, ShowExpanders As Boolean, Nodes As Collection, RootNodes As Collection)

    Dim fileName As String, fileNo As Integer, storeRoot As TreeData

    storeRoot.ActiveNode = ActiveNode
    storeRoot.AppName = AppName
    storeRoot.Changed = Changed
    storeRoot.CheckBoxes = CheckBoxes
    storeRoot.EnableLabelEdit = EnableLabelEdit
    storeRoot.FullWidth = FullWidth
    storeRoot.Indentation = Indentation
    storeRoot.LabelEdit = LabelEdit
    storeRoot.LineColor = LineColor
    storeRoot.MoveCopyNode = MoveCopyNode
    storeRoot.NodeHeight = NodeHeight
    Set storeRoot.Nodes = Nodes
    storeRoot.RootButton = RootButton
    Set storeRoot.RootNodes = RootNodes
    storeRoot.ShowExpanders = ShowExpanders

    'Now save all testVar variables to a binary file
    fileName = "C:\Users\xxx\Documents\test.bin"
    fileNo = FreeFile
    Open fileName For Binary Lock Read Write As #fileNo
    Put #fileNo, , storeRoot
    Close #fileNo
End Sub

0 个答案:

没有答案