Export (0) Print
Expand All
3 out of 4 rated this helpful - Rate this topic

Creating a Page in a Notebook in OneNote 2010

Office 2010

Office Quick Note banner

Handy Programming Tips for Microsoft OneNote 2010: Learn how to programmatically create a new page in a Microsoft OneNote 2010 notebook.

Applies to:   Microsoft 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 create a new page in a notebook and then add text to it. To complete this task, you must do the following:

Note 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.

To add references to the workbook

  1. Start Excel 2010.

  2. On the Developer tab, click Visual Basic. This opens the Visual Basic Editor.

    Note 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.

  3. On the Tools menu, click References.

  4. Scroll to Microsoft OneNote 14.0 Object Library, and then select it.

  5. Next, scroll to Microsoft XML, 6.0, select it, and then click OK.

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.

In this task, you add programming code to the Visual Basic Editor.

To add code to the Visual Basic Editor

  1. In the Projects pane, click Module1.

  2. Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.

    Sub CreateNewPage()
        ' Connect to OneNote 2010.
        ' To see the results of the code,
        ' you'll want to ensure the OneNote 2010 user
        ' interface is visible.
        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 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.DOMDocument
            Set secDoc = New MSXML2.DOMDocument
        
            If secDoc.LoadXML(sectionsXml) Then
                ' select the Section nodes
                Dim secNodes As MSXML2.IXMLDOMNodeList
                Set secNodes = secDoc.DocumentElement.SelectNodes("//one:Section")
                
                If Not secNodes Is Nothing Then
                    ' Get the first section.
                    Dim secNode As MSXML2.IXMLDOMNode
                    Set secNode = secNodes(0)
                    
                    Dim sectionName As String
                    sectionName = secNode.Attributes.getNamedItem("name").Text
                    Dim sectionID As String
                    sectionID = secNode.Attributes.getNamedItem("ID").Text
                    
                    ' Create a new blank Page in the first Section
                    ' using the default format.
                    Dim newPageID As String
                    oneNote.CreateNewPage sectionID, newPageID, npsDefault
                    
                    ' Get the contents of the page.
                    Dim outXML As String
                    oneNote.GetPageContent newPageID, outXML, piAll, xs2010
                    
                    Dim doc As MSXML2.DOMDocument
                    Set doc = New MSXML2.DOMDocument
                    ' Load Page's XML into a MSXML2.DOMDocument object.
                    If doc.LoadXML(outXML) Then
                        ' Get Page Node.
                        Dim pageNode As MSXML2.IXMLDOMNode
                        Set pageNode = doc.SelectSingleNode("//one:Page")
    
                        ' Find the Title element.
                        Dim titleNode As MSXML2.IXMLDOMNode
                        Set titleNode = doc.SelectSingleNode("//one:Page/one:Title/one:OE/one:T")
                        
                        ' Get the CDataSection where OneNote store's the Title's text.
                        Dim cdataChild As MSXML2.IXMLDOMNode
                        Set cdataChild = titleNode.SelectSingleNode("text()")
                        
                        ' Change the title in the local XML copy.
                        cdataChild.Text = "A Page Created from VBA"
                        ' Write the update to OneNote.
                        oneNote.UpdatePageContent doc.XML
                        
                        Dim newElement As MSXML2.IXMLDOMElement
                        Dim newNode As MSXML2.IXMLDOMNode
                        
                        ' Create Outline node.
                        Set newElement = doc.createElement("one:Outline")
                        Set newNode = pageNode.appendChild(newElement)
                        ' Create OEChildren.
                        Set newElement = doc.createElement("one:OEChildren")
                        Set newNode = newNode.appendChild(newElement)
                        ' Create OE.
                        Set newElement = doc.createElement("one:OE")
                        Set newNode = newNode.appendChild(newElement)
                        ' Create TE.
                        Set newElement = doc.createElement("one:T")
                        Set newNode = newNode.appendChild(newElement)
                        
                        ' Add the text for the Page's content.
                        Dim cd As MSXML2.IXMLDOMCDATASection
                        Set cd = doc.createCDATASection("Text added to a new OneNote page via VBA.")
    
                        newNode.appendChild cd
                     
                        
                        ' Update OneNote with the new content.
                        oneNote.UpdatePageContent doc.XML
                        
                        ' Print out information about the update.
                        Debug.Print "A new page was created in "
                        Debug.Print "Section " & sectionName & " in"
                        Debug.Print "Notebook " & noteBookName & "."
                        Debug.Print "Contents of new Page:"
                        
                        Debug.Print doc.XML
                    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 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 CreateNewPage procedure uses the GetHierarchy, GetPageContent, and UpdatePageContent methods to create a new page in a notebook and then set its title to A Page Created from VBA. The code then sets the new page's content to Text added to a new OneNote page via VBA.

In this task, you run the code.

To run the code

  • In the Visual Basic Editor, press F5 to run the code.

  • Open the notebook (if it is not already open) and examine it to see the new page.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.