I have found VBA code for Word that gets the contents of a OneNote notebook using the OneNote API.
This crashes when GetHierarchy is called to retrieve page content.
The following code crashes OneNote when executing
oneNote.GetHierarchy GetAttributeValueFromNode(pageNode, "ID"), hsChildren, pageXml
Option Explicit ' OneNote 2010 ' Demonstrate the GetHierarchy method. ' Use any VBA host including Excel 2010, PowerPoint 2010, ' or Word 2010. ' OneNote 2010 is not a VBA host. ' In your VBA host, add references to the following ' external libraries using the Add References dialog: ' Microsoft OneNote 14.0 Object Library ' Microsoft XML, v6.0 ' OneNote's GetHierarchy method allows you ' to get metadata and data about the OneNote ' Notebooks. ' Paste all this code into a module, ' place the cursor within the ' ListOneNotePageContentFromFirstPageOfFirstSectionOfFirstNotebook ' procedure, and press F5. ' ' The ListOneNotePageContentFromFirstPageOfFirstSectionOfFirstNotebook ' procedure uses the MSXML library to parse the returned XML ' from OneNote and output Notebook metadata ' to the Immediate window of your VBA host. ' The code iterates through the first Notebook, then its first Section, ' and finally gets the first Page and outputs the Page's content ' to the Immediate window. Sub ListOneNotePageContentFromFirstPageOfFirstSectionOfFirstNotebook() ' Connect to OneNote 2010. ' OneNote will be started if it's not running. Dim oneNote As OneNote12.Application Set oneNote = New OneNote12.Application ' Get all of the Notebook nodes. Dim nodes As MSXML2.IXMLDOMNodeList Set nodes = GetFirstOneNoteNotebookNodes(oneNote) If Not nodes Is Nothing Then ' Get the first OneNote Notebook in the XML document. Dim node As MSXML2.IXMLDOMNode Set node = nodes(0) Dim noteBookName As String noteBookName = node.Attributes.getNamedItem("name").Text ' Get the ID for the Notebook so the code can retrieve ' the list of sections. Dim notebookID As String notebookID = node.Attributes.getNamedItem("ID").Text ' Load the XML for the Sections for the Notebook requested. Dim sectionsXml As String oneNote.GetHierarchy notebookID, hsSections, sectionsXml ', xs2010 Dim secDoc As MSXML2.DOMDocument60 Set secDoc = New MSXML2.DOMDocument60 secDoc.setProperty "SelectionNamespaces", "xmlns:one=""http://schemas.microsoft.com/office/onenote/2007/onenote""" If secDoc.LoadXML(sectionsXml) Then Dim secNodes As MSXML2.IXMLDOMNodeList Set secNodes = secDoc.DocumentElement.SelectNodes("//one:Section") If Not secNodes Is Nothing Then Dim secNode As MSXML2.IXMLDOMNode Set secNode = secNodes(0) Dim sectionName As String sectionName = secNode.Attributes.getNamedItem("name").Text Dim sectionID As String sectionID = GetAttributeValueFromNode(secNode, "ID") ' Load the XML for the Pages of the Section requested. Dim pagesXml As String oneNote.GetHierarchy sectionID, hsPages, pagesXml ', xs2010 Dim pagesDoc As MSXML2.DOMDocument60 Set pagesDoc = New MSXML2.DOMDocument60 pagesDoc.setProperty "SelectionNamespaces", "xmlns:one=""http://schemas.microsoft.com/office/onenote/2007/onenote""" If pagesDoc.LoadXML(pagesXml) Then Dim pageNodes As MSXML2.IXMLDOMNodeList Set pageNodes = pagesDoc.DocumentElement.SelectNodes("//one:Page") If Not pageNodes Is Nothing Then Dim pageNode As MSXML2.IXMLDOMNode Set pageNode = pageNodes(0) ' Print out data about the Notebook, Section, and the first Page, ' including its content. Debug.Print "Notebook Name: " & noteBookName Debug.Print "Notebook ID: " & notebookID Debug.Print " Section Name: " & sectionName Debug.Print " Section ID: " & sectionID Debug.Print " Page Name: " & GetAttributeValueFromNode(pageNode, "name") Debug.Print " ID: " & GetAttributeValueFromNode(pageNode, "ID") Dim pageXml As String oneNote.GetHierarchy GetAttributeValueFromNode(pageNode, "ID"), hsChildren, pageXml ', xs2010 Dim pageDoc As MSXML2.DOMDocument60 Set pageDoc = New MSXML2.DOMDocument60 If pageDoc.LoadXML(pageXml) Then Debug.Print " *** Page Content ***" Debug.Print pageXml Else MsgBox "OneNote 2010 Page XML data failed to load." End If Else MsgBox "OneNote 2010 Page nodes not found." End If Else MsgBox "OneNote 2010 Pages XML data failed to load." End If Else MsgBox "OneNote 2010 Section nodes not found." End If Else MsgBox "OneNote 2010 Section XML data failed to load." End If Else MsgBox "OneNote 2010 XML data failed to load." End If End Sub Private Function GetFirstOneNoteNotebookNodes(oneNote As OneNote12.Application) As MSXML2.IXMLDOMNodeList ' Get the XML that represents the OneNote notebooks available. Dim notebookXml As String ' OneNote fills notebookXml with an XML document providing information ' about what OneNote notebooks are available. ' You want all the data and thus are providing an empty string ' for the bstrStartNodeID parameter. oneNote.GetHierarchy "", hsNotebooks, notebookXml ', xs2010 ' Use the MSXML Library to parse the XML. Dim doc As MSXML2.DOMDocument60 Set doc = New MSXML2.DOMDocument60 doc.setProperty "SelectionNamespaces", "xmlns:one=""http://schemas.microsoft.com/office/onenote/2007/onenote""" If doc.LoadXML(notebookXml) Then Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook") Else Set GetFirstOneNoteNotebookNodes = Nothing End If End Function Private Function GetAttributeValueFromNode(node As MSXML2.IXMLDOMNode, attributeName As String) As String If node.Attributes.getNamedItem(attributeName) Is Nothing Then GetAttributeValueFromNode = "Not found." Else GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text End If End Function
Where do I go from here?
Note: this is a regression in OneNote 2016, OneNote 2013 worked fine.