创建从C#新的注2010页 [英] Creating new One Note 2010 page from C#

查看:139
本文介绍了创建从C#新的注2010页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相对的C#业余的,而且我有麻烦了什么,我的猜测是办公室互操作(纠正我,如果我错了),以加速:



我想有一个控制台应用程序,在2010年一注新页面创建一个新的页面总是会进入同款,这将已经存在。页面标题将在Windows剪贴板中的字符串。我知道该怎么做剪贴板部分(程序还会在指定的路径,并使用在剪贴板中的字符串,其名称的文件夹),但我有入门的一个注释部分的麻烦。



我一直在试图理解这些文章(第二个只在VB中有例子,所以我也有对付的):



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



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



不过,我还在基本丧失。我不需要找到任何部分或任何东西的名字,我知道我的新页面总是会进入一个名为在一个名为票据,至少在第一个版本部分任务笔记本/而我还在学习。



我正在寻找漂亮,有重点的如何从C#一个新的页面说明解释。 MSDN文章认为我没有各种先验知识,而我宁愿得到一个跳跃开始和不是花了一个月的阅读边学边做。一旦基本程序工作,我会花很多时间调整它,这应该是一个学习的好方法。


解决方案

对于一个详细的文章,看看这个 MSDN杂志链接



我使用的样例代码在那里建立一个快速的片段让你在给定的笔记本给定段创造了新的一页。



如果您使用Visual Studio 2010中,有一对夫妇的文章中列出的问题的:




首先,由于的不匹配该运
与Visual Studio 2010的OneNote互操作程序集,你不应该直接引用添加
引用对话框的.NET选项卡上的结果
Microsoft.Office.Interop.OneNote组成部分,而是引用COM选项卡上的微软的OneNote 14.0
类型库的组成部分。这仍然导致
添加的OneNote互操作程序集到项目的引用。



二,OneNote的14.0类型库是不是与$ B $兼容b Visual Studio 2010中无PIA功能(在主互操作程序
组件没有嵌入默认情况下,应用程序)。因此,
确保到嵌入互操作类型属性设置为False
OneNote的互操作程序集引用。




 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Xml.Linq的;
使用Microsoft.Office.Interop.OneNote;

命名空间的OneNote
{
类节目
{
静态应用onenoteApp =新的应用程序();
静态的XNamespace NS = NULL;

静态无效的主要(字串[] args)
{
GetNamespace();
串notebookId = GetObjectId(NULL,HierarchyScope.hsNotebooks,任务);
串sectionId = GetObjectId(notebookId,HierarchyScope.hsSections,注意事项);
串PAGEID = CreatePage(sectionId,测试);
}

静态无效GetNamespace()
{
字符串的XML;
onenoteApp.GetHierarchy(NULL,HierarchyScope.hsNotebooks,出XML);

VAR DOC = XDocument.Parse(XML);
NS = doc.Root.Name.Namespace;
}

静态字符串GetObjectId(字符串parentId的,HierarchyScope范围,字符串对象名)
{
字符串的XML;
onenoteApp.GetHierarchy(parentId的,范围从XML);

VAR DOC = XDocument.Parse(XML);
VAR节点名称=;

开关(范围)
{
的情况下(HierarchyScope.hsNotebooks):节点名称=笔记本;打破;
的情况下(HierarchyScope.hsPages):节点名称=页面;打破;
的情况下(HierarchyScope.hsSections):节点名称=部分;打破;
默认:
返回NULL;
}

VAR节点= doc.Descendants(NS +节点名称)。凡(N =方式> n.Attribute(name)的值==对象名).FirstOrDefault();

返回node.Attribute(ID)值。
}

静态字符串CreatePage(字符串sectionId,串页面名称)
{
//创建新页面
串PAGEID;
onenoteApp.CreateNewPage(sectionId,出PAGEID,NewPageStyle.npsBlankPageWithTitle);

//获取标题并将其设置为我们的页面名称
字符串的XML;
onenoteApp.GetPageContent(PAGEID,出XML,PageInfo.piAll);
VAR DOC = XDocument.Parse(XML);
VAR标题= doc.Descendants(NS +T)第一()。
title.Value =页面名;

//更新页面
onenoteApp.UpdatePageContent(doc.ToString());

返回PAGEID;
}
}
}


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.

解决方案

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

这篇关于创建从C#新的注2010页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆