/// <summary> /// Call this to get the running word application, or create a new instance /// </summary> /// <returns>IWordApplication</returns> private static IWordApplication GetOrCreateWordApplication() { IWordApplication wordApplication = COMWrapper.GetOrCreateInstance <IWordApplication>(); InitializeVariables(wordApplication); return(wordApplication); }
/// <summary> /// Call this to get the running outlook application, or create a new instance /// </summary> /// <returns>IOutlookApplication</returns> private static IOutlookApplication GetOrCreateOutlookApplication() { IOutlookApplication outlookApplication = COMWrapper.GetOrCreateInstance <IOutlookApplication>(); InitializeVariables(outlookApplication); return(outlookApplication); }
/// <summary> /// Call this to get the running Excel application, or create a new instance /// </summary> /// <returns>IExcelApplication</returns> private static IExcelApplication GetOrCreateExcelApplication() { IExcelApplication excelApplication = COMWrapper.GetOrCreateInstance <IExcelApplication>(); InitializeVariables(excelApplication); return(excelApplication); }
/// <summary> /// Call this to get the running powerpoint application, or create a new instance /// </summary> /// <returns>IPowerpointApplication</returns> private static IPowerpointApplication GetOrCreatePowerpointApplication() { IPowerpointApplication powerpointApplication = COMWrapper.GetOrCreateInstance <IPowerpointApplication>(); InitializeVariables(powerpointApplication); return(powerpointApplication); }
private static IOutlookApplication OutlookApplication() { IOutlookApplication outlookApplication = (IOutlookApplication)COMWrapper.GetOrCreateInstance(typeof(IOutlookApplication)); try { outlookVersion = new Version(outlookApplication.Version); } catch { } return(outlookApplication); }
public static void InsertIntoNewPresentation(string tmpFile, Size imageSize, string title) { using (IPowerpointApplication powerpointApplication = COMWrapper.GetOrCreateInstance <IPowerpointApplication>()) { if (powerpointApplication != null) { powerpointApplication.Visible = true; IPresentation presentation = powerpointApplication.Presentations.Add(MsoTriState.msoTrue); AddPictureToPresentation(presentation, tmpFile, imageSize, title); presentation.Application.Activate(); } } }
public static void InsertIntoNewWorkbook(string tmpFile) { using (IExcelApplication excelApplication = COMWrapper.GetOrCreateInstance <IExcelApplication>()) { if (excelApplication != null) { excelApplication.Visible = true; object template = Missing.Value; IWorkbook workbook = excelApplication.Workbooks.Add(template); InsertIntoExistingWorkbook(workbook, tmpFile); } } }
public static void ExportToPage(Bitmap imageToExport, OneNotePage page) { using (MemoryStream pngStream = new MemoryStream()) { imageToExport.Save(pngStream, ImageFormat.Png); string base64String = Convert.ToBase64String(pngStream.GetBuffer()); string imageXmlStr = string.Format(XML_IMAGE_CONTENT, base64String, imageToExport.Width, imageToExport.Height); string pageChangesXml = string.Format(XML_OUTLINE, new object[] { imageXmlStr, page.PageID, ONENOTE_NAMESPACE_2010, page.PageName }); using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) { LOG.InfoFormat("Sending XML: {0}", pageChangesXml); oneNoteApplication.UpdatePageContent(pageChangesXml, DateTime.MinValue, XMLSchema.xs2010, false); } } }
/// <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; while (xmlReader.Read()) { if ("one:Page".Equals(xmlReader.Name)) { if ("true".Equals(xmlReader.GetAttribute("isCurrentlyViewed"))) { OneNotePage page = new OneNotePage(); page.PageName = xmlReader.GetAttribute("name"); page.PageID = xmlReader.GetAttribute("ID"); pages.Add(page); // For debugging //string pageXml = ""; //oneNoteApplication.GetPageContent(page.PageID, out pageXml, PageInfo.piAll, XMLSchema.xs2010); //LOG.DebugFormat("Page XML: {0}", pageXml); } } } } } finally { if (reader != null) { reader.Dispose(); } } } } } } catch (Exception ex) { LOG.Warn("Problem retrieving onenote destinations, ignoring: ", ex); } return(pages); }
public static void ExportToPage(ISurface surfaceToUpload, OneNotePage page) { using (MemoryStream pngStream = new MemoryStream()) { SurfaceOutputSettings pngOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false); ImageOutput.SaveToStream(surfaceToUpload, pngStream, pngOutputSettings); string base64String = Convert.ToBase64String(pngStream.GetBuffer()); string imageXmlStr = string.Format(XML_IMAGE_CONTENT, base64String, surfaceToUpload.Image.Width, surfaceToUpload.Image.Height); string pageChangesXml = string.Format(XML_OUTLINE, new object[] { imageXmlStr, page.PageID, ONENOTE_NAMESPACE_2010, page.PageName }); using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) { LOG.InfoFormat("Sending XML: {0}", pageChangesXml); oneNoteApplication.UpdatePageContent(pageChangesXml, DateTime.MinValue, XMLSchema.xs2010, false); } } }
/// <summary> /// Create a new page in the "unfiled notes section", with the title of the capture, and export the capture there. /// </summary> /// <param name="surfaceToUpload">ISurface</param> /// <returns>bool true if export worked</returns> public static bool ExportToNewPage(ISurface surfaceToUpload) { using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) { OneNotePage newPage = new OneNotePage(); string unfiledNotesSectionID = GetSectionID(oneNoteApplication, SpecialLocation.slUnfiledNotesSection); if (unfiledNotesSectionID != null) { string pageId = ""; oneNoteApplication.CreateNewPage(unfiledNotesSectionID, out pageId, NewPageStyle.npsDefault); newPage.ID = pageId; // Set the new name, this is automatically done in the export to page newPage.Name = surfaceToUpload.CaptureDetails.Title; return(ExportToPage(oneNoteApplication, surfaceToUpload, newPage)); } } return(false); }
public static void InsertIntoNewDocument(string tmpFile, string address, string tooltip) { using (IWordApplication wordApplication = COMWrapper.GetOrCreateInstance <IWordApplication>()) { if (wordApplication != null) { wordApplication.Visible = true; wordApplication.Activate(); // Create new Document object template = string.Empty; object newTemplate = false; object documentType = 0; object documentVisible = true; IWordDocument wordDocument = wordApplication.Documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible); // Add Picture using (IInlineShape shape = AddPictureToSelection(wordApplication.Selection, tmpFile)) { if (!string.IsNullOrEmpty(address)) { object screentip = Type.Missing; if (!string.IsNullOrEmpty(tooltip)) { screentip = tooltip; } try { wordDocument.Hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing); } catch (Exception e) { LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message); } } } try { wordApplication.Activate(); } catch { } try { wordDocument.Activate(); } catch { } try { wordDocument.ActiveWindow.Activate(); } catch { } } } }
public static bool InsertIntoNewPresentation(string tmpFile, Size imageSize, string title) { bool isPictureAdded = false; using (IPowerpointApplication powerpointApplication = COMWrapper.GetOrCreateInstance <IPowerpointApplication>()) { if (powerpointApplication != null) { powerpointApplication.Visible = true; IPresentation presentation = powerpointApplication.Presentations.Add(MsoTriState.msoTrue); try { AddPictureToPresentation(presentation, tmpFile, imageSize, title); isPictureAdded = true; presentation.Application.Activate(); } catch (Exception e) { LOG.Error(e); } } } return(isPictureAdded); }
public static void InsertIntoNewDocument(string tmpFile) { using (IWordApplication wordApplication = COMWrapper.GetOrCreateInstance <IWordApplication>()) { if (wordApplication != null) { wordApplication.Visible = true; wordApplication.Activate(); // Create new Document object template = string.Empty; object newTemplate = false; object documentType = 0; object documentVisible = true; IWordDocument wordDocument = wordApplication.Documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible); // Add Picture AddPictureToSelection(wordApplication.Selection, tmpFile); wordDocument.Activate(); wordDocument.ActiveWindow.Activate(); } } }
/// <summary> /// Export the capture to the specified page /// </summary> /// <param name="surfaceToUpload">ISurface</param> /// <param name="page">OneNotePage</param> /// <returns>bool true if everything worked</returns> public static bool ExportToPage(ISurface surfaceToUpload, OneNotePage page) { using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) { return(ExportToPage(oneNoteApplication, surfaceToUpload, page)); } }
/// <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); }
private static IWordApplication WordApplication() { return((IWordApplication)COMWrapper.GetOrCreateInstance(typeof(IWordApplication))); }
private static IPowerpointApplication PowerpointApplication() { return((IPowerpointApplication)COMWrapper.GetOrCreateInstance(typeof(IPowerpointApplication))); }
private static IExcelApplication ExcelApplication() { return((IExcelApplication)COMWrapper.GetOrCreateInstance(typeof(IExcelApplication))); }