我正在尝试以编程方式打开一个便笺页面。我在C#中使用OneNote2013。我遍历笔记本,部分组,部分和最后页面,获取页面ID并尝试使用该PageId打开页面。但它会抛出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;
}