Sub SaveStructure()
    Dim Answer As String
    Dim FileName As String
    Dim i As Integer
    Dim iTmp As Integer
    Dim iIndex As Integer
    Dim RecData As String
    
    ' The only way to get the canceltrap for the commondialogbox.
    ' Remember to also set cancelerror to true.
    On Error GoTo CancelError
           
    CommonDialog1.Filter = "TreeView Database(*.mst) |*.mst"
    If Len(CommonDialog1.FileName) < 1 Then
        CommonDialog1.CancelError = True
        CommonDialog1.ShowSave
    End If
    
    If Len(CommonDialog1.FileName) < 1 Then
        MsgBox "Du måste ange ett filnamn att spara arbetsboken med!", 64
        Exit Sub
    End If
    
    '// Öppnar databasen
    Open CommonDialog1.FileName For Output As #1
    
    'Writes the Node information from the TreeView into a table
    '??. Find a root node in the treeview
    ' GetFirstParent
        
    'get the index of the root node that is at the top of the treeview
    iIndex = TreeView1.Nodes(mnIndex).FirstSibling.index
    iTmp = iIndex
            
    '// Adderar huvudboken:
    RecData = "0_" & ";" & TreeView1.Nodes(iIndex).key & ";" & _
      TreeView1.Nodes(iIndex).Text & ";" & TreeView1.Nodes(iIndex).image & _
      ";" & TreeView1.Nodes(iIndex).selectedimage & "; "
    Print #1, RecData
    
    While iIndex <> TreeView1.Nodes(iTmp).LastSibling.index
        'loop through all the root nodes
        RecData = "0_" & ";" & TreeView1.Nodes(iIndex).key & ";" & _
          TreeView1.Nodes(iIndex).Text & ";" & TreeView1.Nodes(iIndex).image & _
          ";" & TreeView1.Nodes(iIndex).selectedimage & ";" & _
          TreeView1.Nodes(iIndex).Tag
        Print #1, RecData
        iIndex = TreeView1.Nodes(iIndex).Next.index
    Wend
    
    For i = 1 To TreeView1.Nodes.Count
        If TreeView1.Nodes(i).Children > 0 Then
        'save all the child nodes
            iIndex = TreeView1.Nodes(i).Child.FirstSibling.index
            iTmp = iIndex
            RecData = TreeView1.Nodes(iIndex).Parent.key & ";" & _
              TreeView1.Nodes(iIndex).key & ";" & TreeView1.Nodes(iIndex).Text & _
                ";" & TreeView1.Nodes(iIndex).image & ";" & _
                TreeView1.Nodes(iIndex).selectedimage & ";" & _
                TreeView1.Nodes(iIndex).Tag
            Print #1, RecData
            
            While iIndex <> TreeView1.Nodes(iTmp).LastSibling.index
                'loop through all the child nodes
                RecData = TreeView1.Nodes(iIndex).Parent.key & ";" & _
                  TreeView1.Nodes(iIndex).Next.key & ";" & T_
                  TreeView1.Nodes(iIndex).Next.Text & ";" & _
                  TreeView1.Nodes(iIndex).Next.image & ";" & _
                  TreeView1.Nodes(iIndex).Next.selectedimage & ";" & _
                  TreeView1.Nodes(iIndex).Next.Tag
                Print #1, RecData
                iIndex = TreeView1.Nodes(iIndex).Next.index
            Wend
        End If
    Next
    
    Close #1
    Exit Sub
    
CancelError:
    Exit Sub
End Sub
Sub LoadStructure()
'asks the user to find the database and then restores the Nodes
'in the treeview control from the table
    Dim oNodex As Node
    Dim nImage As Integer
    Dim nSelectedImage As Integer
    Dim sTableNames As String
    Dim sNodeTable As String
    Dim RecordData As String
    
    Dim Parent As String
    Dim key As String
    Dim Text As String
    Dim image As String
    Dim selectedimage As String
    Dim RecordInfo(6) As String
    Dim a, b, i, infokey As Integer
    Dim iIndex As Integer
               
    ' *** Det verkar som om fönstret placeras utifrån vad som är
    ' övre vänstra hörnet på aktivt fönster, hur styr man detta?
                       
    ' Activate the open commandbox
    nImage = 0: nSelectedImage = 0
    CommonDialog1.Filter = "TreeView Database(*.mst) |*.mst"
    CommonDialog1.InitDir = App.Path
    CommonDialog1.ShowOpen
    
    If Len(CommonDialog1.FileName) < 1 Then
        Exit Sub    'No file selected
    End If
    
    TreeView1.Nodes.Clear 'Clear the TreeView of any nodes
    Open CommonDialog1.FileName For Input As 1
        
    '// Finns några poster i denna databas,
    '    om inte- så varna användaren om detta.
        
    Do While Not EOF(1)
        'Layouten är i följande form, och ; separerad.
        ' -- > Parent;Key;Text;Image;SelectedImage
        
        '6.Tag (För att ange "attached message")
                    
        Line Input #1, RecordData
        'Omvandlar detta till 5 parametrar
        b = 1: infokey = 0
        For a = 1 To Len(RecordData)
            If Mid$(RecordData, a, 1) = ";" Then
                infokey = infokey + 1
                RecordInfo(infokey) = Mid$(RecordData, b, a - b)
                a = a + 1: b = a
            End If
        Next
        infokey = infokey + 1
        RecordInfo(infokey) = Mid$(RecordData, b, a - b)
       
        nImage = Val(RecordInfo(4))
        nSelectedImage = Val(RecordInfo(5))
           
        If Trim(RecordInfo(1)) = "0_" Then   'All root nodes have 0_ in the parent field
            Set oNodex = TreeView1.Nodes.Add(, 1, Trim(RecordInfo(2)), _
              Trim(RecordInfo(3)), nImage, nSelectedImage)
        Else
            Set oNodex = TreeView1.Nodes.Add(Trim(RecordInfo(1)), _
              tvwChild, Trim(RecordInfo(2)), Trim(RecordInfo(3)), nImage, _
                nSelectedImage)
            oNodex.EnsureVisible 'expend the TreeView so all nodes are visible
            
            'Adderar Tag(Filnamn) till alla artiklar som läggs upp
            oNodex.Tag = RecordInfo(6)
            
        End If
        
    Loop
    Close #1
End Sub