I want to do the following:
- Check if the specific OneNote Notebook "NoteBookName" exist
- If the specific OneNote Notebook "NoteBookName" does not exist, create a new notebook with the name "NoteBookName"
- Once confirmed, check if the section "Tickets" under the notebook "NoteBookName" exists
- If it is not present, create a new section called "Tickets"
The source code (VB.NET) shown below, is working fine. But when new data needs to be added to a existing page, it does not update the page. The source code creates a new page with the same name containing the new data. How do I prevent this from happening?
Sub CreateNewPage() 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 ' 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
In advance,
Your assistance is most welcome.
Roland
Running a: virt. WIN8 and VS Ultimate 2012 - Visual Basic ... Hosted in: MS Windows Server 2012 Std on Acer Aspire 5750 ... Intel Core i5-2430M 2.4GHz - 16 GB DDR3 memory - 1 TB HDD