从C#创建新的OneNote 2010页面

4

我是一个相对初学者,对于我猜测的Office Interop(如果我错了请纠正)感到困惑:

我想要创建一个控制台应用程序,在One Note 2010中创建一个新页面。 新页面将始终放入同一部分中,该部分已经存在。 页面标题将是Windows剪贴板中的字符串。 我知道如何处理剪贴板部分(程序还会在指定路径中创建一个文件夹,并使用剪贴板中的字符串命名),但是我开始处理One Note部分时遇到了麻烦。

我一直在尝试理解这些文章(第二篇文章仅提供VB示例,因此我还需要处理它):

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

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

但我还是基本上迷失了。我不需要找到任何部分的名称或其他内容,我知道我的新页面总是会放在一个名为“任务”的笔记本中的一个名为“笔记”的部分中,至少在第一个版本/我还在学习的时候。

我正在寻找一个关于如何从C#创建新的One Note页面的简洁明了的解释。MSDN文章假设我有各种我没有的先前知识,我宁愿通过实践获得启动和学习,而不是花一个月的时间阅读。一旦基本程序运行正常,我将花费大量时间进行调整,这应该是一个很好的学习方式。

1个回答

10

如需详细文章,请查看MSDN Magazine链接

我使用那里的示例代码为您创建了一个快速片段,以便在给定笔记本中的给定部分中创建新页面。

如果您正在使用Visual Studio 2010,则文章中列出了一些“要点”:

首先,由于与Visual Studio 2010一起发布的OneNote互操作程序集不匹配,因此您不应直接引用“添加引用”对话框上.NET选项卡上的Microsoft.Office.Interop.OneNote组件,而应引用COM选项卡上的Microsoft OneNote 14.0类型库组件。这仍会导致将OneNote互操作程序集添加到项目的引用中。

其次,OneNote 14.0类型库与Visual Studio 2010的“NOPIA”功能不兼容(默认情况下不嵌入主互操作程序集)。因此,请确保将Interop类型嵌入属性设置为false,以针对OneNote互操作程序集引用。

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;
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接