Programmatically Opening and Closing a Notebook in OneNote 2010
Handy Programming Tips for Microsoft OneNote 2010: Learn how to programmatically close, open, and navigate to a Microsoft OneNote 2010 notebook.
Last modified: July 29, 2011
Applies to: Excel 2010 | Office 2010 | OneNote 2010
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 CloseNotebook, OpenHierarchy, and NavigateTo methods to close, open, and then navigate to a notebook in a new window. To complete this task, you must do the following:
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.
To add references to the workbook
Start Excel 2010.
On the Developer tab, click Visual Basic. This opens the Visual Basic Editor.
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.
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 CloseOpenAndNavigateToANoteBook() ' Connect to OneNote 2010. ' OneNote will be started if it's not running. Dim oneNote As OneNote14.Application Set oneNote = New OneNote14.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 Notebook found. Dim node As MSXML2.IXMLDOMNode Set node = nodes(0) ' Get the Notebook name. Dim noteBookName As String noteBookName = node.Attributes.getNamedItem("name").Text ' Get the Notebook path. Dim noteBookPath As String noteBookPath = node.Attributes.getNamedItem("path").Text ' Get the Notebook ID. Dim notebookID As String notebookID = node.Attributes.getNamedItem("ID").Text MsgBox "Closing notebook " & noteBookName, vbInformation, "Closing" ' Close the selected notebook. oneNote.CloseNotebook notebookID, False MsgBox "Opening notebook " & noteBookName, vbInformation, "Closing" ' Open the notebook that was just closed by ' passing the path to OneNote. In addition, pass a parameter ' to get the new ID for the notebook which could be used ' for other operations. Dim reOpenedID As String oneNote.OpenHierarchy noteBookPath, "", reOpenedID, cftNone ' Using the reOpenedID to navigate to the re-opened Notebook ' and show it in a new Window. oneNote.NavigateTo reOpenedID, "", True 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 Private Function GetFirstOneNoteNotebookNodes(oneNote As OneNote14.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.DOMDocument Set doc = New MSXML2.DOMDocument If doc.LoadXML(notebookXml) Then Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook") Else Set GetFirstOneNoteNotebookNodes = Nothing End If End Function
The CloseOpenAndNavigateToANoteBook procedure uses the MSXML library to parse the XML returned from OneNote 2010 and then get the list of notebooks. It then uses information about the first notebook found to first close it, then re-open it, and finally to navigate to the notebook in a new window.