Export (0) Print
Expand All

Retrieving Window Metadata in OneNote 2010

Office 2010

Office Quick Note banner

Handy Programming Tips for Microsoft OneNote 2010: Learn how to retrieve window meta-data from a Microsoft OneNote 2010 notebook.

Last modified: July 29, 2011

Applies to: Excel 2010 | Office 2010 | OneNote 2010 | PowerPoint 2010 | VBA | 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 Windows and Window objects to retrieve meta-data and data about the windows in one or more notebooks. 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 should open one or two OneNote 2010 notebooks.

    Public Sub GetWindowInfoAndCurrentPageData()
        ' Connect to OneNote 2010.
     
        ' For this code to do something interesting,
        ' you should manually start OneNote first so at least
        ' one, preferably two, OneNote windows are visible.
        Dim oneNote As OneNote14.Application
        Set oneNote = New OneNote14.Application
        
        Dim intCurrentWindowCount As Integer
        intCurrentWindowCount = 0
            
        ' Walk the list of current windows.
        Dim oneNoteWindow As OneNote14.Window
        For Each oneNoteWindow In oneNote.Windows
            intCurrentWindowCount = intCurrentWindowCount + 1
            With oneNoteWindow
                Debug.Print "Window " & intCurrentWindowCount
                Debug.Print "  Active: " & .Active
                ' You can use the next of IDs to get more information
                ' using the GetHierarchy method.
                Debug.Print "  Current Notebook ID: " & .CurrentNotebookId
                Debug.Print "  Current Page ID: " & .CurrentPageId
                Debug.Print "  Current Section ID: " & .CurrentSectionId
                Debug.Print "  Current Section Group ID: " & .CurrentSectionGroupId
                Debug.Print "  Docked Location: " & .DockedLocation
                Debug.Print "  Full Page View: " & .FullPageView
                Debug.Print "  Side Note: " & .SideNote
            End With
        Next
        
        If intCurrentWindowCount = 0 Then
            Debug.Print "No visible OneNote windows."
        Else
            ' Get the Current Window.
            Set oneNoteWindow = oneNote.Windows.CurrentWindow
            
            ' If the current Window isn't a SideNote window, continue.
            If Not oneNoteWindow.SideNote Then
                ' Get the active page's xml.
                Dim pageXml As String
                oneNote.GetPageContent oneNoteWindow.CurrentPageId, pageXml, piBasic, xs2010
                
                Dim pageDoc As MSXML2.DOMDocument
                Set pageDoc = New MSXML2.DOMDocument
                
                Dim pageName As String
                If pageDoc.LoadXML(pageXml) Then
                    Dim nodes As MSXML2.IXMLDOMNodeList
                    Set nodes = pageDoc.DocumentElement.SelectNodes("//one:Page")
                            
                    If Not nodes Is Nothing Then
                        Dim pageNode As MSXML2.IXMLDOMNode
                        Set pageNode = nodes(0)
            
                        pageName = GetAttributeValueFromNode(pageNode, "name")
        
                        ' Output information about the current page.
                        Debug.Print "Current Page Name: " & pageName
                        Debug.Print "Current Page ID: " & oneNoteWindow.CurrentPageId
                        Debug.Print "Current Page XML Below: "
                        
                        Debug.Print pageXml
                    End If
                End If
            End If
        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 GetWindowInfoAndCurrentPageData procedure uses the MSXML library to parse the XML returned from OneNote 2010 and then outputs information about the currently open window(s) to the Immediate window of Excel.

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.

Show:
© 2014 Microsoft