4

我是一个相对的 c# 业余爱好者,我无法跟上我猜是 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

但我基本上还是迷路了。我不需要找到任何部分或任何东西的名称,我知道我的新页面总是会进入一个名为 Tasks 的笔记本中的一个名为 Notes 的部分中,至少在第一个版本中/当我还在学习时。

我正在寻找关于如何从 C# 创建新的 One Note 页面的精彩、集中的解释。MSDN 文章假定了我没有的各种先验知识,我宁愿快速开始,边做边学,也不愿花一个月的时间阅读。一旦基本程序工作,我会花很多时间来调整它,这应该是一个很好的学习方式。

4

1 回答 1

9

有关详细文章,请查看此MSDN 杂志链接

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

如果您使用的是 Visual Studio 2010,文章中列出了几个“陷阱”:

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

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

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;
        }
    }
}
于 2011-11-18T19:43:56.547 回答