Use a command button to open a onenote notebook (1 Viewer)

DrPat

Registered User.
Local time
Today, 14:03
Joined
Feb 7, 2011
Messages
39
I would like to add 2 command buttons to an existing form:

1. Create a onenote notebook with a name based on the primary key of the active record

2. Open an existing onenote notebook based on the primary key of the active record.

Any suggestions? (Be gentle, you're dealing with an amateur)

/Pat
 
Last edited:

Estuardo

Registered User.
Local time
Today, 18:03
Joined
May 27, 2003
Messages
134
G'd evening,
I guess you may want to start Reading this.

Here is a sample code from MS to create a simple note:
Code:
Option Compare Database
Option Explicit


'Add references to
'Microsoft OneNote 14.0 Object Library
'Microsoft XML, 6.0


Public Sub CreateNote()
  ' 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
G'd luck
 
Last edited:

mbaycura

New member
Local time
Today, 11:03
Joined
Nov 1, 2007
Messages
2
I've not been able to get this MS code to work in Access or Excel. It might be a windows 8 thing, but i've found at least two sticking points so far. First, my system didn't recognize "MSXML2.DOMDocument", but did after i changed it to "MSXML2.DOMDocument60". After correcting that i tried to run the code again and now it's hanging up not recognizing the reference to ("//one:Notebook") at the line Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook").
I can't find anything to help me understand that path syntax to correct it so i'm stuck.

I don't know anything about VBA which i'm sure is part of the problem, but as written this VBA block does not work as intended at least for a computer using Office 2010 32bit version running on a windows 8 64bit machine.
 

Estuardo

Registered User.
Local time
Today, 18:03
Joined
May 27, 2003
Messages
134
remove the reference to Microsoft XML, 6.0 and add a reference to Microsoft XML, 4.0, that should solve the problem.
Regardless of the windows version, if the referenced libraries exist they should work.
I'd just tested the code and it's working. If you still have problems, debug your code line by line and let us know where the code hangs.
 

mbaycura

New member
Local time
Today, 11:03
Joined
Nov 1, 2007
Messages
2
Thanks, changing the reference from XML 6.0 to 4.0 worked for me.
 

Users who are viewing this thread

Top Bottom