簡體   English   中英

如何使用C#OneNote 2013 15.0 COM類型庫創建新的筆記本或部分?

[英]How to create a new notebook or section using C# OneNote 2013 15.0 COM Type Library?

我無法使用OneNote 2013開發人員參考創建新筆記本或在現有筆記本中創建新部分。

這里的MSDN指南表明它是可能的,但沒有提供如何操作的示例。

每當我嘗試添加一個新的筆記本時,我得到一個HRESULT:0x80042015錯誤代碼 ,表明筆記本不存在。 我知道! 我正在嘗試添加它:-)

此外,每當我嘗試向現有筆記本添加新部分時,我都會得到一個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); } 

我想我對這個問題的處理方法不正確。 我想如果我編輯了xml數據以包含一個新的Notebook或Section,那么COM庫就足夠智能來檢測新添加的元素。 進一步閱讀后,創建新Notebook或Section的正確方法是使用OpenHierarchy()方法。

這是我為現有Notebook創建新Section的工作副本。 我還沒有嘗試創建一個新的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"; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM