我无法使用OneNote 2013开发人员参考创建新笔记本或在现有笔记本中创建新部分。
关于MSDN here的指导意见表明它是可能的,但没有提供如何做的示例。
每当我尝试添加新笔记本时,我都会得到HRESULT: 0x80042015 error code,表示笔记本不存在。我知道!我正在尝试添加它: - )
此外,每当我尝试向现有笔记本添加新部分时,我都会收到一个HRESULT:0x80042014错误代码,表示该对象不存在。
这是一个代码片段,展示了我正在尝试做的事情。任何帮助将不胜感激!
try
{
_app.GetHierarchy(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsNotebooks, out strXml);
using (var stringReader = new StringReader(strXml))
{
var xdoc = XDocument.Load(stringReader);
// Get the first notebook. Currently, I couldn't find a way to create a new Notebook using the OneNote interops.
var notebook = xdoc.Root.Descendants().First();
if (!notebook.Descendants().Any(d => d.Name == "My New Section"))
{
// Get ready to create a new section
XNamespace ns = "http://schemas.microsoft.com/office/onenote/2013/onenote";
var myNewSectionElement = new XElement(ns + "Section");
myNewSectionElement.Add(new XAttribute("name", "My New Section"));
myNewSectionElement.Add(new XAttribute("path", @"C:\MyNewSection.one"));
myNewSectionElement.Add(new XAttribute("ID", @"{5F786510-79D4-4D0B-BC93-A637700D7543}{1}{B0}"));
notebook.Add(myNewSectionElement);
// Update the heirarchy
var strBuilder = new StringBuilder();
using (var stringWriter = new StringWriter(strBuilder))
{
xdoc.Save(stringWriter);
_app.UpdateHierarchy(strBuilder.ToString(), Microsoft.Office.Interop.OneNote.XMLSchema.xs2013);
}
}
else
{
Output = "My New Section already exists.\r\n";
}
}
}
catch (Exception ex)
{
Output = string.Format("{0}:{1}\r\n", ex.GetType(), ex.Message);
}
答案 0 :(得分:0)
我想我对问题的处理方法不正确。我想如果我编辑了xml数据以包含一个新的Notebook或Section,那么COM库就足够智能来检测新添加的元素。在进一步阅读之后,创建新Notebook或Section的正确方法是使用OpenHierarchy()方法。
这是我创建现有Notebook的新部分的工作副本。我还没有尝试过创建一个新的Notebook,但我认为这种方法很相似。
private void DoOpenHierarchy(Microsoft.Office.Interop.OneNote.HierarchyScope scope)
{
Output = "Open Hierarchy Section...\r\n";
var strXml = string.Empty;
var objectId = string.Empty;
_app.GetHierarchy(null, scope, out strXml);
try
{
var xdoc = XDocument.Parse(strXml);
var ns = xdoc.Root.Name.Namespace;
if (scope == Microsoft.Office.Interop.OneNote.HierarchyScope.hsSections)
{
var noteBook = xdoc.Root.Descendants(ns + "Notebook").FirstOrDefault();
if (noteBook != null)
{
var sectionName = "My New Section";
Output += string.Format("Attempting to create section '{0}' in {1}...\r\n", sectionName, noteBook.Attribute("name").Value);
var location = string.Format("{0}\\{1}.one", noteBook.Attribute("path").Value, sectionName);
_app.OpenHierarchy(location, string.Empty, out objectId, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);
Output += string.Format("Section ID Created: {0}\r\n", objectId.ToString());
}
else
{
Output += "ERROR: Not able to determine a 'path' in order to store new section.\r\n";
}
}
}
catch (Exception ex)
{
Output += string.Format("{0}:{1}\r\n", ex.GetType(), ex.Message);
}
Output += "\r\nOpen Hierarchy Section Done.\r\n";
}