从 C# 创建新的 One Note 2010 页面

发布于 2024-12-16 16:40:22 字数 861 浏览 1 评论 0原文

我是一个相对的 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

但我仍然基本上迷失了。我不需要找到任何部分或任何内容的名称,我知道我的新页面总是会进入一个名为“任务”的笔记本,位于一个名为“注释”的部分中,至少在第一个版本中/当我仍在学习时。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2024-12-23 16:40:22

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

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

如果您使用的是 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;
        }
    }
}

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文