Advertisement
Bucher100

OneNote Class - VB.NET

Dec 6th, 2024
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.18 KB | Source Code | 0 0
  1. Class SurroundingClass
  2.     Shared onenoteApp As Application = New Application()
  3.     Shared ns As XNamespace = Nothing
  4.  
  5.     Private Shared Sub Main(ByVal args As String())
  6.         GetNamespace()
  7.         Dim notebookId As String = GetObjectId(Nothing, OneNote.HierarchyScope.hsNotebooks, "MyNotebook")
  8.         Dim sectionId As String = GetObjectId(notebookId, OneNote.HierarchyScope.hsSections, "Sample_Section")
  9.         Dim firstPageId As String = GetObjectId(sectionId, OneNote.HierarchyScope.hsPages, "MyPage")
  10.         GetPageContent(firstPageId)
  11.         Console.Read()
  12.     End Sub
  13.  
  14.     Private Shared Sub GetNamespace()
  15.         Dim xml As String
  16.         onenoteApp.GetHierarchy(Nothing, OneNote.HierarchyScope.hsNotebooks, xml)
  17.         Dim doc = XDocument.Parse(xml)
  18.         ns = doc.Root.Name.[Namespace]
  19.     End Sub
  20.  
  21.     Private Shared Function GetObjectId(ByVal parentId As String, ByVal scope As OneNote.HierarchyScope, ByVal objectName As String) As String
  22.         Dim xml As String
  23.         onenoteApp.GetHierarchy(parentId, scope, xml)
  24.         Dim doc = XDocument.Parse(xml)
  25.         Dim nodeName = ""
  26.  
  27.         Select Case scope
  28.             Case (OneNote.HierarchyScope.hsNotebooks)
  29.                 nodeName = "Notebook"
  30.             Case (OneNote.HierarchyScope.hsPages)
  31.                 nodeName = "Page"
  32.             Case (OneNote.HierarchyScope.hsSections)
  33.                 nodeName = "Section"
  34.             Case Else
  35.                 Return Nothing
  36.         End Select
  37.  
  38.         Dim node = doc.Descendants(ns & nodeName).Where(Function(n) n.Attribute("name").Value = objectName).FirstOrDefault()
  39.         Return node.Attribute("ID").Value
  40.     End Function
  41.  
  42.     Private Shared Function GetPageContent(ByVal pageId As String) As String
  43.         Dim xml As String
  44.         onenoteApp.GetPageContent(pageId, xml, OneNote.PageInfo.piAll)
  45.         Dim doc = XDocument.Parse(xml)
  46.         Dim outLine = doc.Descendants(ns & "Outline").First()
  47.         Dim content = outLine.Descendants(ns & "T").First()
  48.         Dim contentVal As String = content.Value
  49.         content.Value = "modified"
  50.         onenoteApp.UpdatePageContent(doc.ToString())
  51.         Return Nothing
  52.     End Function
  53. End Class
Tags: VB.NET OneNote
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement