Retrieving Notebook Metadata in OneNote 2010
Handy Programming Tips for Microsoft OneNote 2010: Learn how to retrieve notebook meta-data from Microsoft OneNote 2010.
Applies to: Excel 2010 | Office 2007 | Office 2010 | OneNote 2010 | PowerPoint 2010 | Word 2010
In this article
Add Library References to the Excel Workbook
Add a Standard Module to the Excel Workbook
Add the Code to the Visual Basic Editor
Run the Code
Next Steps
Published: August 2011
Provided by: Frank Rice, Microsoft Corporation
Microsoft OneNote 2010 enables you to take and organize notes, include images and web pages, record audio, share your notes with others, and generally keep information from the many parts of your life organized within one simple application. In this topic, you use the GetHierarchy method to retrieve meta-data and other data about a notebook. To complete this task, you must do the following:
Add Library References to the Excel Workbook
Add a Standard Module to the Excel Workbook
Add the Code to the Visual Basic Editor
Run the Code
Next Steps
Note
To use the code in this topic, you need to run it from a Microsoft Office 2010 host program. These include Microsoft Excel 2010, Microsoft PowerPoint 2010, and Microsoft Word 2010. However, OneNote 2010 is not considered a host program. Excel is used in this topic.
Add Library References to the Excel Workbook
To add references to the workbook
Start Excel 2010.
On the Developer tab, click Visual Basic. This opens the Visual Basic Editor.
Note
If you do not see the Developer tab in Excel, click the File tab, and then click Options. In the categories pane, click Customize Ribbon, select Developer, and then click OK.
On the Tools menu, click References.
Scroll to Microsoft OneNote 14.0 Object Library, and then select it.
Next, scroll to Microsoft XML, 6.0, select it, and then click OK.
Add a Standard Module to the Excel Workbook
In this task, you insert a standard module into the Excel workbook.
To add a standard module to the Excel workbook
- On the Insert menu, click Module. This adds Module1 to the Projects pane on the left side of the Visual Basic Editor.
Add the Code to the Visual Basic Editor
In this task, you add programming code to the Visual Basic Editor.
To add code to the Visual Basic Editor
In the Projects pane, click Module1.
Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.
Sub ListOneNoteNotebooks() ' Connect to OneNote 2010. ' OneNote will be started if it's not running. Dim oneNote As OneNote14.Application Set oneNote = New OneNote14.Application ' Get the XML that represents the OneNote notebooks available. Dim notebookXml As String ' notebookXml gets filled in with an XML document providing information ' about what OneNote notebooks are available. ' You want all the data. Thus you provide an empty string ' for the bstrStartNodeID parameter. oneNote.GetHierarchy "", hsNotebooks, notebookXml, xs2010 ' Use the MSXML Library to parse the XML. Dim doc As MSXML2.DOMDocument Set doc = New MSXML2.DOMDocument If doc.LoadXML(notebookXml) Then ' Find all the Notebook nodes in the one namespace. Dim nodes As MSXML2.IXMLDOMNodeList Set nodes = doc.DocumentElement.SelectNodes("//one:Notebook") Dim node As MSXML2.IXMLDOMNode Dim noteBookName As String Dim temp As String ' Walk the collection of Notebooks. ' Read attribute values and write them ' out to the Immediate window of your VBA host. For Each node In nodes noteBookName = node.Attributes.getNamedItem("name").Text Debug.Print "Notebook Name and Location: "; vbCrLf & " " & _ noteBookName & " is at " & _ node.Attributes.getNamedItem("path").Text Debug.Print "Additional data for " & noteBookName Debug.Print " ID: " & node.Attributes.getNamedItem("ID").Text ' Not all notebooks will have all additional data below. Debug.Print " Color: " & GetAttributeValueFromNode(node, "color") Debug.Print " Is Unread: " & GetAttributeValueFromNode(node, "isUnread") Debug.Print " Last Modified: " & GetAttributeValueFromNode(node, "lastModifiedTime") Next Else MsgBox "OneNote 2010 XML Data failed to load." End If End Sub 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
The ListOneNoteNotebooks procedure uses the MSXML library to parse the XML returned from OneNote 2010 and outputs the notebook metadata to the Immediate window of Excel.
Run the Code
In this task, you run the code.
To run the code
In the Visual Basic Editor, press F5 to run the code.
Examine the results in the Immediate window.