Creating a new One Note 2010 page with C #

I'm a relative C # lover, and I'm having trouble speeding things up with what I think is Office Interop (correct me if I'm wrong):

I want to have a console application that creates a new page in One Note 2010. The new page will always be in the same section that already exists. The page title will be a line in the Windows clipboard. I know how to make part of the clipboard (the program also creates a folder at the specified path and names it using a line in the clipboard), but I can’t get started with the One Note part.

I am trying to understand these articles (the second one has examples only in VB, so I also have to deal with this):

http://msdn.microsoft.com/en-us/library/gg649853.aspx

http://code.msdn.microsoft.com/windowsdesktop/OneNote-2010-Create-New-880f8ee3

But I'm still mostly lost. I don’t need to find the names of any sections or anything else, I know that my new pages will always be included in a notebook called “Tasks” in the “Notes” section, at least in the first version /, while I’m all still involved.

I am looking for a good, focused explanation of how to create a new One Note page with C #. MSDN articles suggest all kinds of prior knowledge that I don’t have, and I would rather start a jump and learn than spend a month reading. Once the base program is working, I will spend a lot of time setting it up, which should be a great way to learn.

+4
c # onenote
source share
2 answers

For a detailed article, see this MSDN Magazine link .

I used the sample code to create a quick snippet to create a new page in this section on this laptop.

If you are using Visual Studio 2010, there are a couple of "gotchas" in this article:

Firstly, due to a mismatch between the OneNote build that ships with Visual Studio 2010, you should not directly reference the Microsoft.Office.Interop.OneNote Component in the .NET tab of the Add tab of the Help dialog, but instead, refer to Microsoft OneNote 14.0 Enter the library on the COM tab. This still leads to the addition of a single-user OneNote build to your projects.

Secondly, the OneNote 14.0 type library is not compatible with the Visual Studio 2010 "NOPIA" feature (in which the primary assembly interface is not built into the application by default). Therefore, make sure that the Embed Interop Types property is set to False for OneNote Link Assembly Link.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Microsoft.Office.Interop.OneNote; namespace OneNote { class Program { static Application onenoteApp = new Application(); static XNamespace ns = null; static void Main(string[] args) { GetNamespace(); string notebookId = GetObjectId(null, HierarchyScope.hsNotebooks, "Tasks"); string sectionId = GetObjectId(notebookId, HierarchyScope.hsSections, "Notes"); string pageId = CreatePage(sectionId, "Test"); } static void GetNamespace() { string xml; onenoteApp.GetHierarchy(null, HierarchyScope.hsNotebooks, out xml); var doc = XDocument.Parse(xml); ns = doc.Root.Name.Namespace; } static string GetObjectId(string parentId, HierarchyScope scope, string objectName) { string xml; onenoteApp.GetHierarchy(parentId, scope, out xml); var doc = XDocument.Parse(xml); var nodeName = ""; switch (scope) { case (HierarchyScope.hsNotebooks): nodeName = "Notebook"; break; case (HierarchyScope.hsPages): nodeName = "Page"; break; case (HierarchyScope.hsSections): nodeName = "Section"; break; default: return null; } var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault(); return node.Attribute("ID").Value; } static string CreatePage(string sectionId, string pageName) { // Create the new page string pageId; onenoteApp.CreateNewPage(sectionId, out pageId, NewPageStyle.npsBlankPageWithTitle); // Get the title and set it to our page name string xml; onenoteApp.GetPageContent(pageId, out xml, PageInfo.piAll); var doc = XDocument.Parse(xml); var title = doc.Descendants(ns + "T").First(); title.Value = pageName; // Update the page onenoteApp.UpdatePageContent(doc.ToString()); return pageId; } } } 
+9
source share

If MS Interop is not an option for you, try looking at Aspose.Note . Creating a new page is a fairly simple task:

 var doc = new Document(); var page = new Page(doc); page.Title = new Title(doc) { TitleText = new RichText(doc) { Text = "Title text.", DefaultStyle = TextStyle.DefaultMsOneNoteTitleTextStyle }, TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), DefaultStyle = TextStyle.DefaultMsOneNoteTitleDateStyle }, TitleTime = new RichText(doc) { Text = "12:34", DefaultStyle = TextStyle.DefaultMsOneNoteTitleTimeStyle } }; page.AppendChild(outline); doc.AppendChild(page); doc.Save("output.one") 
0
source share

All Articles