/// <summary> /// Get the captions of all the open word documents /// </summary> /// <returns></returns> public static List<OneNotePage> GetPages() { List<OneNotePage> pages = new List<OneNotePage>(); try { using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance<IOneNoteApplication>()) { if (oneNoteApplication != null) { string notebookXml = ""; oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010); if (!string.IsNullOrEmpty(notebookXml)) { LOG.Debug(notebookXml); StringReader reader = null; try { reader = new StringReader(notebookXml); using (XmlTextReader xmlReader = new XmlTextReader(reader)) { reader = null; OneNoteSection currentSection = null; OneNoteNotebook currentNotebook = null; while (xmlReader.Read()) { if ("one:Notebook".Equals(xmlReader.Name)) { string id = xmlReader.GetAttribute("ID"); if (id != null && (currentNotebook == null || !id.Equals(currentNotebook.ID))) { currentNotebook = new OneNoteNotebook(); currentNotebook.ID = xmlReader.GetAttribute("ID"); currentNotebook.Name = xmlReader.GetAttribute("name"); } } if ("one:Section".Equals(xmlReader.Name)) { string id = xmlReader.GetAttribute("ID"); if (id != null && (currentSection == null || !id.Equals(currentSection.ID))) { currentSection = new OneNoteSection(); currentSection.ID = xmlReader.GetAttribute("ID"); currentSection.Name = xmlReader.GetAttribute("name"); currentSection.Parent = currentNotebook; } } if ("one:Page".Equals(xmlReader.Name)) { // Skip deleted items if ("true".Equals(xmlReader.GetAttribute("isInRecycleBin"))) { continue; } OneNotePage page = new OneNotePage(); page.Parent = currentSection; page.Name = xmlReader.GetAttribute("name"); page.ID = xmlReader.GetAttribute("ID"); if (page.ID == null || page.Name == null) { continue; } page.IsCurrentlyViewed = "true".Equals(xmlReader.GetAttribute("isCurrentlyViewed")); pages.Add(page); } } } } finally { if (reader != null) { reader.Dispose(); } } } } } } catch (COMException cEx) { if (cEx.ErrorCode == unchecked((int)0x8002801D)) { LOG.Warn("Wrong registry keys, to solve this remove the OneNote key as described here: http://microsoftmercenary.com/wp/outlook-excel-interop-calls-breaking-solved/"); } LOG.Warn("Problem retrieving onenote destinations, ignoring: ", cEx); } catch (Exception ex) { LOG.Warn("Problem retrieving onenote destinations, ignoring: ", ex); } pages.Sort(delegate(OneNotePage p1, OneNotePage p2) { if(p1.IsCurrentlyViewed || p2.IsCurrentlyViewed) { return p2.IsCurrentlyViewed.CompareTo(p1.IsCurrentlyViewed); } return p1.DisplayName.CompareTo(p2.DisplayName); }); return pages; }
/// <summary> /// Get the captions of all the open word documents /// </summary> /// <returns></returns> public static List <OneNotePage> GetPages() { List <OneNotePage> pages = new List <OneNotePage>(); try { using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) { if (oneNoteApplication != null) { // ReSharper disable once RedundantAssignment string notebookXml = ""; oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010); if (!string.IsNullOrEmpty(notebookXml)) { Log.Debug(notebookXml); StringReader reader = null; try { reader = new StringReader(notebookXml); using (XmlTextReader xmlReader = new XmlTextReader(reader)) { reader = null; OneNoteSection currentSection = null; OneNoteNotebook currentNotebook = null; while (xmlReader.Read()) { if ("one:Notebook".Equals(xmlReader.Name)) { string id = xmlReader.GetAttribute("ID"); if (id != null && (currentNotebook == null || !id.Equals(currentNotebook.ID))) { currentNotebook = new OneNoteNotebook { ID = xmlReader.GetAttribute("ID"), Name = xmlReader.GetAttribute("name") }; } } if ("one:Section".Equals(xmlReader.Name)) { string id = xmlReader.GetAttribute("ID"); if (id != null && (currentSection == null || !id.Equals(currentSection.ID))) { currentSection = new OneNoteSection { ID = xmlReader.GetAttribute("ID"), Name = xmlReader.GetAttribute("name"), Parent = currentNotebook }; } } if ("one:Page".Equals(xmlReader.Name)) { // Skip deleted items if ("true".Equals(xmlReader.GetAttribute("isInRecycleBin"))) { continue; } OneNotePage page = new OneNotePage { Parent = currentSection, Name = xmlReader.GetAttribute("name"), ID = xmlReader.GetAttribute("ID") }; if (page.ID == null || page.Name == null) { continue; } page.IsCurrentlyViewed = "true".Equals(xmlReader.GetAttribute("isCurrentlyViewed")); pages.Add(page); } } } } finally { reader?.Dispose(); } } } } } catch (COMException cEx) { if (cEx.ErrorCode == unchecked ((int)0x8002801D)) { Log.Warn("Wrong registry keys, to solve this remove the OneNote key as described here: http://microsoftmercenary.com/wp/outlook-excel-interop-calls-breaking-solved/"); } Log.Warn("Problem retrieving onenote destinations, ignoring: ", cEx); } catch (Exception ex) { Log.Warn("Problem retrieving onenote destinations, ignoring: ", ex); } pages.Sort(delegate(OneNotePage p1, OneNotePage p2) { if (p1.IsCurrentlyViewed || p2.IsCurrentlyViewed) { return(p2.IsCurrentlyViewed.CompareTo(p1.IsCurrentlyViewed)); } return(string.Compare(p1.DisplayName, p2.DisplayName, StringComparison.Ordinal)); }); return(pages); }