How To Write To A OneNote 2013 Page Using C# and The OneNote Interop

后端 未结 4 1659
独厮守ぢ
独厮守ぢ 2020-11-29 08:56

I have seen many articles about this but all of them are either incomplete or do not answer my question. Using C# and the OneNote Interop, I would like to simp

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 09:26

    This is just what I've gleaned from reading examples on the web (of course, you've already read all of those) and peeking into the way OneNote stores its data in XML using ONOMspy (http://blogs.msdn.com/b/johnguin/archive/2011/07/28/onenote-spy-omspy-for-onenote-2010.aspx).

    If you want to work with OneNote content, you'll need a basic understanding of XML. Writing text to a OneNote page involves creating an outline element, whose content will be contained in OEChildren elements. Within an OEChildren element, you can have many other child elements representing outline content. These can be of type OE or HTMLBlock, if I'm reading the schema correctly. Personally, I've only ever used OE, and in this case, you'll have an OE element containing a T (text) element. The following code will create an outline XElement and add text to it:

    // Get info from OneNote
    string xml;
    onApp.GetHierarchy(null, OneNote.HierarchyScope.hsSections, out xml);
    XDocument doc = XDocument.Parse(xml);
    XNamespace ns = doc.Root.Name.Namespace;
    
    // Assuming you have a notebook called "Test"
    XElement notebook = doc.Root.Elements(ns + "Notebook").Where(x => x.Attribute("name").Value == "Test").FirstOrDefault();
    if (notebook == null)
    {
        Console.WriteLine("Did not find notebook titled 'Test'.  Aborting.");
        return;
    }
    
    // If there is a section, just use the first one we encounter
    XElement section;
    if (notebook.Elements(ns + "Section").Any())
    {
        section = notebook.Elements(ns + "Section").FirstOrDefault();
    }
    else
    {
        Console.WriteLine("No sections found.  Aborting");
        return;
    }
    
    // Create a page
    string newPageID;
    onApp.CreateNewPage(section.Attribute("ID").Value, out newPageID);
    
    // Create the page element using the ID of the new page OneNote just created
    XElement newPage = new XElement(ns + "Page");
    newPage.SetAttributeValue("ID", newPageID);
    
    // Add a title just for grins
    newPage.Add(new XElement(ns + "Title",
        new XElement(ns + "OE",
            new XElement(ns + "T",
                new XCData("Test Page")))));
    
    // Add an outline and text content
    newPage.Add(new XElement(ns + "Outline",
        new XElement(ns + "OEChildren",
            new XElement(ns + "OE",
                new XElement(ns + "T",
                    new XCData("Here is some new sample text."))))));
    
    // Now update the page content
    onApp.UpdatePageContent(newPage.ToString());
    

    Here's what the actual XML you're sending to OneNote looks like:

    
      
        <OE>
          <T><![CDATA[Test Page]]></T>
        </OE>
      
      
        
          
            
          
        
      
    
    

    Hope that helps get you started!

提交回复
热议问题