I'm a relative c# amateur, and I'm having trouble getting up to speed with what I guess is Office Interop (correct me if I'm wrong):
I want to have a console app that creates a new page in One Note 2010. The new page will always go into the same section, which will already exist. The page title will be a string in the Windows clipboard. I know how to do the clipboard part (the program also creates a folder in a specified path and names it using the string in the clipboard), but I'm having trouble getting started with the One Note part.
I've been trying to understand these articles (the second one has examples in VB only, so I also have to deal with that):
http://msdn.microsoft.com/en-us/library/gg649853.aspx
http://code.msdn.microsoft.com/windowsdesktop/OneNote-2010-Create-New-880f8ee3
But I'm still basically lost. I don't need to find the names of any sections or anything, I know that my new pages will always go into a notebook called Tasks in a section called Notes, at least in the first version/while I'm still learning.
I'm looking for nice, focused explanation of how to create a new One Note page from C#. The MSDN articles assume all sorts of prior knowledge that I don't have, and I'd rather get a jump start and learn by doing than spend a month reading. Once the basic program works, I'll spend lots of time tweaking it, which should be a great way to learn.
For a detailed article, check out this MSDN Magazine link.
I used the sample code there to create a quick snippet for you to create a new page in a given section in a given notebook.
If you are using Visual Studio 2010, there are a couple of "gotchas" listed in the article:
First, due to a mismatch of the OneNote interop assembly that shipped with Visual Studio 2010, you should not directly reference the
Microsoft.Office.Interop.OneNote component on the .NET tab of the Add Reference dialog, but instead reference the Microsoft OneNote 14.0 Type Library component on the COM tab. This still results in the addition of a OneNote interop assembly to your project’s references.Second, the OneNote 14.0 Type Library is not compatible with the Visual Studio 2010 “NOPIA” feature (in which primary interop assemblies are not embedded in the application by default). Therefore, make sure to set the Embed Interop Types property to False for the OneNote interop assembly reference.
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;
}
}
}