Creating new One Note 2010 page from C#

2019-03-22 02:37发布

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.

标签: c# onenote
2条回答
Fickle 薄情
2楼-- · 2019-03-22 03:00

If MS Interop is not an option for you try to look on Aspose.Note. Creating a new page is quite an easy 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")
查看更多
走好不送
3楼-- · 2019-03-22 03:18

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;
        }
    }
}
查看更多
登录 后发表回答