【问题标题】:Creating new One Note 2010 page from C#从 C# 创建新的 One Note 2010 页面
【发布时间】:2011-11-18 17:57:45
【问题描述】:

我是一个相对的 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 文章假设了我没有的各种先验知识,我宁愿快速开始,边做边学,也不愿花一个月的时间阅读。基本程序运行后,我会花大量时间对其进行调整,这应该是一种很好的学习方式。

【问题讨论】:

    标签: c# onenote


    【解决方案1】:

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

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

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

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

    其次,OneNote 14.0 类型库不兼容 Visual Studio 2010 “NOPIA”功能(其中主要互操作 默认情况下,程序集不嵌入到应用程序中)。所以, 确保将 Embed Interop Types 属性设置为 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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-25
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多