VBA to load FreeMind file into PowerPoint

  • Nobody/Anonymous

    I offer the following VBA for anyone who wants to import a mindmap into PowerPoint - this gives a basic structured text only presentation. Since I now tend to use Freemind when working on a presentation, I find this a good starting point. Note the hard-coded filename: my Office library seems to be broken.

    peter dot ward at uk dot fujitsu dot com

    Public Sub LoadMap()
        Dim doc As MSXML2.DOMDocument
        Dim FileName As String
        Dim success As Boolean

        On Error GoTo HandleErr

        Set doc = New MSXML2.DOMDocument
        ' Load the  XML from disk, without validating it. Wait
        ' for the load to finish before proceeding.
        doc.async = False
        doc.validateOnParse = False
        ' Need to use std file open dialog here
        FileName = WhichFile()

        success = doc.Load("c:\\governance.mm")
        ' If anything went wrong, quit now.
        If success Then
            Call ProcessDocument(doc)
        End If

        Exit Sub
        MsgBox "Error " & Err.Number & ": " & Err.Description
        Resume ExitHere
    End Sub

    Private Function WhichFile() As String
        ' Don't know why - can't get File Picker to work
        WhichFile = "c:\\governance.mm"
    End Function

    Private Sub ProcessDocument(doc As MSXML2.IXMLDOMNode)
        Dim root As MSXML2.IXMLDOMNode
        Dim children As MSXML2.IXMLDOMNodeList
        Dim child As MSXML2.IXMLDOMNode

         ' Get the root of the XML tree.
        Set root = doc.firstChild
        Set child = root.firstChild
        Call ProcessNode(child)
    End Sub

    Private Sub ProcessNode(node As MSXML2.IXMLDOMNode)
        Dim children As MSXML2.IXMLDOMNodeList
        Dim child As MSXML2.IXMLDOMNode

        If node.nodeName = "node" Then 'ignore noise
            If node.haschildnodes Then 'ignore tip nodes
                Call MakeSlide(node)
                For Each child In node.childNodes
                    Call ProcessNode(child)
                Next child
            End If
        End If
    End Sub

    Private Sub MakeSlide(node As MSXML2.IXMLDOMNode)
        Dim newSlide As PowerPoint.slide
        Dim label As String
        label = GetMindMapEntry(node)
        With ActivePresentation
            Set newSlide = .Slides.Add(.Slides.Count + 1, ppLayoutBlank)
            With newSlide
                .Layout = 2
                .Shapes.Title.TextFrame.TextRange.Text = label
                Call AddBullets(node, newSlide)
            End With
        End With

    End Sub

    Private Function GetMindMapEntry(node As MSXML2.IXMLDOMNode) As String
        Dim attributes As MSXML2.IXMLDOMNamedNodeMap
        Dim labelNode As MSXML2.IXMLDOMNode
        Set attributes = node.attributes
        Set labelNode = attributes.getNamedItem("TEXT")
        GetMindMapEntry = labelNode.nodeValue
    End Function

    Private Sub AddBullets(node As MSXML2.IXMLDOMNode, newSlide As PowerPoint.slide)
        Dim children As MSXML2.IXMLDOMNodeList
        Dim child As MSXML2.IXMLDOMNode
        Dim bullet As String
        Dim firstLine As Boolean
        firstLine = True
        With newSlide
            For Each child In node.childNodes
                If child.nodeName = "node" Then 'ignore noise
                    bullet = GetMindMapEntry(child)
                    With .Shapes.Item(2).TextFrame.TextRange
                        If Not firstLine Then
                            .InsertAfter (vbCr)
                        End If
                        .InsertAfter (bullet)
                        firstLine = False
                    End With
                End If
            Next child
        End With
    End Sub

    Public Sub test()
        Dim slide As PowerPoint.slide
        Set slide = ActivePresentation.Slides.Item(1)
        MsgBox ("Hi")
    End Sub

    • Terry Doner

      Terry Doner - 2005-01-20

      Interesting. Could you breifly describe the functional advantage this has versus a copy/paste from freemind to ppt?

      The one possibility is that your code creates new pages from every first level node, whereas copy/paste creates only one  slide.


Log in to post a comment.