C# OneNote NavigateTo()在打开页面时使用hrObjectDoesNotExist(0x80042014)响应

C# OneNote NavigateTo()在打开页面时使用hrObjectDoesNotExist(0x80042014)响应,c#,onenote,C#,Onenote,我试图以编程方式打开一个单注释页面。我正在用C#和OneNote2013做这件事。我反复浏览笔记本、分区组、分区,最后是页面,获取页面id并尝试使用该页面id打开页面。但它抛出了一个ObjectDoesNotExist异常。我做错了什么?下面的代码(底部为OpenOneNote() public class OneNoteInterface { static Application onenoteApp = new Application(); static XNamespace

我试图以编程方式打开一个单注释页面。我正在用C#和OneNote2013做这件事。我反复浏览笔记本、分区组、分区,最后是页面,获取页面id并尝试使用该页面id打开页面。但它抛出了一个ObjectDoesNotExist异常。我做错了什么?下面的代码(底部为OpenOneNote()

public class OneNoteInterface
{
    static Application onenoteApp = new Application();
    static XNamespace ns = null;

    public static string GetPage(string notebookName, string sectionGroupName, string sectionName, string pageName, out string pageXml, out string sectionId)
    {
        var onenoteApp = new Application();
        pageXml = "";
        sectionId = "";
        string notebookXml;
        onenoteApp.GetHierarchy(null, HierarchyScope.hsChildren, out notebookXml);

        var doc = XDocument.Parse(notebookXml);
        var ns = doc.Root.Name.Namespace;
        foreach (var notebookNode in from node in doc.Descendants(ns +
                "Notebook")
                                     select node)
        {
            //Console.WriteLine(notebookNode.Attribute("name").Value);
            if (!notebookNode.Attribute("name").Value.Equals(notebookName))
                continue;
            string notebookID = notebookNode.Attribute("ID").Value;
            onenoteApp.GetHierarchy(notebookID, HierarchyScope.hsChildren, out notebookXml);
            Console.WriteLine(notebookXml);
            doc = XDocument.Parse(notebookXml);
            ns = doc.Root.Name.Namespace;
            pageXml = "";


            foreach (var sectionGroupNode in from node in doc.Descendants(ns + "SectionGroup") select node)
            {
                //Console.WriteLine("  " + sectionGroupNode.Attribute("name").Value);

                if (sectionGroupNode.Attribute("name").Value.Equals(sectionGroupName))
                {
                    string sectionGroupID = sectionGroupNode.Attribute("ID").Value;
                    onenoteApp.GetHierarchy(sectionGroupID, HierarchyScope.hsChildren, out notebookXml);
                    //Console.WriteLine(notebookXml);
                    doc = XDocument.Parse(notebookXml);
                    ns = doc.Root.Name.Namespace;
                    foreach (var sectionNode in from node in
                                                    doc.Descendants(ns + "Section")
                                                select node)
                    {
                        Console.WriteLine("  " + sectionNode.Attribute("name").Value);
                        if (sectionNode.Attribute("name").Value.Equals(sectionName))
                        {
                            sectionId = sectionNode.Attribute("ID").Value;
                            onenoteApp.GetHierarchy(sectionId, HierarchyScope.hsChildren, out notebookXml);
                            //Console.WriteLine(notebookXml);
                            doc = XDocument.Parse(notebookXml);
                            ns = doc.Root.Name.Namespace;
                            foreach (var pageNode in from node in
                                                         doc.Descendants(ns + "Page")
                                                     select node)
                            {
                                //Console.WriteLine("  " + pageNode.Attribute("name").Value);
                                if (pageNode.Attribute("name").Value.Equals(pageName))
                                {
                                    string pageId = pageNode.Attribute("ID").Value;
                                    onenoteApp.GetPageContent(pageId, out pageXml, PageInfo.piAll);
                                    doc = XDocument.Parse(pageXml);
                                    //Console.WriteLine(pageXml);
                                    return pageId;
                                }
                            }
                            return null;
                        }

                    }
                    return null;
                }

            }
            return null;
        }
        return null;
 }




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



    public Boolean OpenOneNote(string notebookName, string sectionGroupName, string sectionName, string pageName)
    {

        string sectionId = "";
        string pageXml = "";
        string pageId = GetPage(notebookName, sectionGroupName, sectionName, pageName, out pageXml, out sectionId);
        if (pageId != null)
        {

            onenoteApp.NavigateTo(pageId);
            return true;
        }
        return false;

    }