Export (0) Print
Expand All

Publishing to a PDF file in OneNote 2010

Office 2010

Office Quick Note banner

Handy Programming Tips for Microsoft OneNote 2010: Learn how to publish the contents of a Microsoft OneNote 2010 notebook to a PDF file.

Last modified: July 29, 2011

Applies to: Excel 2010 | 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 and Publish methods to iterate through the first notebook, its first section, and then retrieve the contents of the first page. You then output that information to a PDF file. 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.

    Note Note

    Before running the code, you need to open a OneNote 2010 notebook.

    ' Change this value to a valid folder on your computer.
    Const FILE_PATH = "C:\Windows\Temp\"
    
    Sub PublishFirstPageOfFirstSectionOfFirstNotebookToPdf()
        ' Connect to OneNote 2010.
        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
                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 sectionID As String
                    sectionID = GetAttributeValueFromNode(secNode, "ID")
                                    
                    ' Load the XML for the Pages for the Section requested.
                    Dim pagesXml As String
                    oneNote.GetHierarchy sectionID, hsPages, pagesXml, xs2010
                    
                    Dim pagesDoc As MSXML2.DOMDocument
                    Set pagesDoc = New MSXML2.DOMDocument
                    
                    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)
    
                            Dim pageName As String
                            Dim pageID As String
                            
                            pageName = GetAttributeValueFromNode(pageNode, "name")
                            pageID = GetAttributeValueFromNode(pageNode, "ID")
    
                            ' Get the user's specified output folder.
                            Dim outputFolder As String
                            outputFolder = FILE_PATH
                            
                            ' Create a file name using the page's name.
                            Dim fileName As String
                            fileName = pageName & ".pdf"
                            
                            ' Combine the two values into a single
                            ' Variable so it's easier to use twice.
                            Dim publishContentTo As String
                            publishContentTo = outputFolder & fileName
                            
                            ' Publish the page content to a PDF file
                            ' in the user specified folder.
                            ' The last parameter is not necessary unless you use the
                            ' pfEmf enum.
                            ' Note this method will fail if the file already exists
                            ' at the location specified.
                            oneNote.Publish pageID, publishContentTo, pfPDF
                            
                            ' Display where the file was published to the user.
                            MsgBox "Page " & pageName & " was published to " & publishContentTo, vbInformation, "Publish OK"
                        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 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 PublishFirstPageOfOfFirstSectionOfFirstNotebookToPdf procedure uses the MSXML library to iterate through the first notebook, its first section, and finally gets the first page and outputs the page's content to a PDF file in the directory specified.

In this task, you run the code.

To run the code

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

  • Navigate to the directory specified on your computer and observe that the PDF file has been created.

Show:
© 2014 Microsoft