none
Opening OneNote 2010 / 2016 with VB.net

    Question

  •      Hi Everyone, 

                I have been trying to open OneNote 2010 OR 2016 in vb.net for a few days now. Every example I have found is either for VBA, or on the new dev.onenote which doesn't include vb.net code.

                Or a mixture of vb.net with C, it's very annoying, I can't even get the name value of a OneNote sample. My current sample I'm trying to work right now is 

     

    Imports Microsoft.Win32

    Imports System.Security.Permissions

    Imports MicrosoftOffice.Interop

    Public Class Form 1

    Private Sub Button2_Click(Sender As Object, e As EventArgs) Handles Button2.Click

    Dim oneNote As OneNote.Application oneNote = New OneNote.Application ' Get all of the Notebook nodes. Dim nodes As MSXML2.IXMLDOMNodeList nodes = GetFirstOneNoteNotebookNodes(oneNote) If Not nodes Is Nothing Then ' Get the first OneNote Notebook in the XML document. Dim node As MSXML2.IXMLDOMNode node = nodes(0) Dim noteBookName As String noteBookName = node.attributes.getNamedItem("name").text <--------- Object Reference not set to an instance of an object ' 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, Microsoft.Office.Interop.OneNote.HierarchyScope.hsSections, sectionsXml) Dim secDoc As MSXML2.DOMDocument secDoc = New MSXML2.DOMDocument If secDoc.loadXML(sectionsXml) Then ' select the Section nodes Dim secNodes As MSXML2.IXMLDOMNodeList secNodes = secDoc.documentElement.selectNodes("//one:Section[@name='Tickets']") If Not secNodes Is Nothing Then ' Get the first section. Dim secNode As MSXML2.IXMLDOMNode 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, Microsoft.Office.Interop.OneNote.NewPageStyle.npsDefault) ' Get the contents of the page. Dim outXML As String oneNote.GetPageContent(newPageID, outXML, Microsoft.Office.Interop.OneNote.PageInfo.piAll) Dim doc As MSXML2.DOMDocument 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 pageNode = doc.selectSingleNode("//one:Page") ' Find the Title element. Dim titleNode As MSXML2.IXMLDOMNode 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 cdataChild = titleNode.selectSingleNode("text()") ' Change the title in the local XML copy. cdataChild.text = TextBox1.Text ' Write the update to OneNote. oneNote.UpdatePageContent(doc.xml) Dim newElement As MSXML2.IXMLDOMElement Dim newNode As MSXML2.IXMLDOMNode ' Create Outline node. newElement = doc.createElement("one:Outline") newNode = pageNode.appendChild(newElement) ' Create OEChildren. newElement = doc.createElement("one:OEChildren") newNode = newNode.appendChild(newElement) ' Create OE. newElement = doc.createElement("one:OE") newNode = newNode.appendChild(newElement) ' Create TE. newElement = doc.createElement("one:T") newNode = newNode.appendChild(newElement) ' Add the text for the Page's content. Dim cd As MSXML2.IXMLDOMCDATASection cd = doc.createCDATASection(TextBox2.Text & vbCrLf & TextBox3.Text) newNode.appendChild(cd) ' Update OneNote with the new content. oneNote.UpdatePageContent(doc.xml) ' Print out information about the update. MsgBox("A new page was created in Section '" & sectionName & "' in Notebook '" & noteBookName & "'.") 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 OneNote.Application) As MSXML2.IXMLDOMNodeList ' Working source code Dim notebookXml As String oneNote.GetHierarchy("", Microsoft.Office.Interop.OneNote.HierarchyScope.hsNotebooks, notebookXml) Dim doc As MSXML2.DOMDocument doc = New MSXML2.DOMDocument If doc.loadXML(notebookXml) Then GetFirstOneNoteNotebookNodes = doc.documentElement.selectNodes("//one:Notebook[@name='NoteBookName']") Else GetFirstOneNoteNotebookNodes = Nothing End If End Function



    Monday, April 17, 2017 8:31 PM

All replies

  • Hi

    This works on my machine, can't say it's universal though.

            Process.Start("shell:AppsFolder\Microsoft.Office.OneNote_8wekyb3d8bbwe!microsoft.onenoteim")
    


    Regards Les, Livingston, Scotland

    Monday, April 17, 2017 9:12 PM
  • Les,   

        Thank you for your reply. I should have been clearer. It's not just booting up OneNote, I'm trying to pull data from any of the books also. I know if I could even just get the 'name' of one of the books from within I could probably take it from there.

        But a few lines down the 

    noteBookName = node.attributes.getNamedItem("name").text

    I get an "Object Reference not set to an instance of an object" error thrown at me.

    Thanks for any assistance.


    • Edited by Mr Linoge Tuesday, April 18, 2017 12:58 AM block code
    Tuesday, April 18, 2017 12:57 AM
  • I get an "Object Reference not set  to an instance of an object" error thrown at me.

    When it stops with that error, which of the objects referred to on that line is Nothing?

    node
    node.attributes
    node.attributes
    .getNamedItem("name")
    node.attributes.getNamedItem("name").Text




    • Edited by AcamarMVP Tuesday, April 18, 2017 1:04 AM fmt
    Tuesday, April 18, 2017 1:04 AM
  • Hi Mr Linoge,

    I find this case may be helpful to you, please refer to :https://social.msdn.microsoft.com/Forums/windows/en-US/1a312686-1e1f-45c0-b754-56e98e3af94f/how-to-create-a-new-onenote-notebook-using-vbnet?forum=officegeneral

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 18, 2017 7:05 AM
    Moderator
  • Hi Cherry,

           That is the code I am using already, that I had posted above. I also had pointed out the part that isn't working.

            Thanks,

             Linoge

    Tuesday, April 18, 2017 11:40 AM
  • Not sure why you would want to use MSXML2.DOMDocument (which is COM) instead of the XmlDocument Class in .NET. Once you have loaded XmlDocument you can iterate through all of the nodes. There are C# examples at the below link which can be easily converted to VB.NET, in addition to the examples for the XmlDocument Class:

    https://msdn.microsoft.com/en-us/library/office/jj680120.aspx


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, April 18, 2017 12:20 PM
  • Hi Paul,

         This is what I'm lost on. I'm terrible at converting code between the two. I had tried to find samples, because I can usually learn and build on an example. But I can't find one for vb.net that will open using examples available. It seems like everyone is using VBA or other web based scripting languages to manipulate the data in OneNote, (now that there is API availability).

         I would of never known I should be using 'XmlDocument Class' if it weren't for you mentioning it.

    So 

    PublicClass XmlDocument
    	Inherits XmlNode

    and then Instead of Dim node As MSXML2.IXMLDOMNode...I would just Dim node as...?

    Thanks for any help!

    Linoge



    Tuesday, April 18, 2017 2:17 PM
  • I don't really know what you want to do with your OneNote documents, but here are a couple of short examples that should get you started:

        Sub GetAllSections()
    
            Dim strXML As String
            Dim onApplication As OneNote.Application = New OneNote.Application
            onApplication.GetHierarchy(Nothing, OneNote.HierarchyScope.hsPages, strXML)
            Clipboard.SetText(strXML)
            MessageBox.Show("The XML has been copied to the clipboard")
    
        End Sub

    Find a particular section:

            Dim strNamespace As String = "http://schemas.microsoft.com/office/onenote/2013/onenote"
            Dim outputXML As String
            Dim onApplication As OneNote.Application = New OneNote.Application
            onApplication.GetHierarchy(Nothing, HierarchyScope.hsSections, outputXML)
            Dim xmlDoc As XmlDocument = New XmlDocument
            xmlDoc.LoadXml(outputXML)
            Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable)
            nsmgr.AddNamespace("one", strNamespace)
            Dim xmlNode As XmlNode = xmlDoc.SelectSingleNode("//one:Section[@name='Sample_Section']", nsmgr)


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, April 18, 2017 7:05 PM
  • Hi Acamar,

         The red error line is under the .text if that is what you're asking? When I hover over 'node' it says node|nothing. Sorry for my confusion, I usually don't go out of my way for help like this.

          Thanks,

           Linoge

    Tuesday, April 18, 2017 7:31 PM
  • The red error line is under the .text if that is what you're asking? When I hover over 'node' it says node|nothing.

    Then you need to look at how many items are in the nodes collection.   if the count is zero, then the line
        nodes =   GetFirstOneNoteNotebookNodes(oneNote)

    has not found any nodes.  However, I would expect that to give a different error.  If the count is greater than zero and the line fails because node is Nothing, then the first node in the collection does not actually exist. Try to look at other nodes instead, and see if you can detect which one you should be using.

    Tuesday, April 18, 2017 9:15 PM
  • Paul,

        Thank you so much! This is great. I can see that it actually copies all of the xml, and I can paste it into notepad. Though I see that it only is retrieving the title of the notebooks, and the titles of the pages. Is there some way in the HierarchyScope to get the content also? I looked on MSDN but it looks like most of the examples are using the COM way I had originally posted.

        I could not figure out how to specifically target that content. Say I had 3 notebooks, Test1 Test2 and Test3, titles Test1Name, Test2Name, and Test3name in each of those books..... and the content of Test2Name book was "Test 2 Text"

       How could  I set that Test2Name text to a variable/textbox?

        I know it involves

    dim xmlNode as XmlNode = xmlDox.SelectSingleNode"//one:Section[Section[@name='Test2Name']"

    But I know it can't be converted to a string?

        Thank you so much for your help so far Paul.

         Linoge


    • Edited by Mr Linoge Friday, April 21, 2017 3:23 PM
    Thursday, April 20, 2017 7:57 PM
  • Hi everyone,

        Using Paul's example, does anyone know how I would be able to retrieve the actual text and not just the book titles? And assign it to a variable or a text box?

        Thanks so much for all of the help so far.

        Linoge

    Monday, April 24, 2017 2:59 PM
  • Hi Paul,

       Sorry to bother you again. I've still been trying to retrieve the actual text and assign it to either a string variable or a text box. Any guidance would be SO appreciated. At that point I could mark this question complete, and move on with my project.

        Thank you so much !

        -Linoge

    Friday, May 12, 2017 3:47 PM
  • Hi Paul

           To recap I'm trying to programmatically find/save the name of a onenote page to a variable,then read the title of it saving that to a variable, and then read the content of that page to a variable. The code I have so far only can copy the xml of the main book and assign it to a variable. Though I do see the pageID in the xml. I have been trying for around 7 months now to figure this out. Any help is appreciated.

          Of in your last example if you could explain the parts of:

    //one:Section[@name='Sample_Section']", nsmgr)

           and how to find/navigate them from my simple example.

           Thanks!

             -Linoge

    Wednesday, November 15, 2017 2:49 AM