/// <summary> /// Export the file to the supplied inspector /// </summary> /// <param name="inspector">Inspector</param> /// <param name="currentItem">Item</param> /// <param name="tmpFile"></param> /// <param name="attachmentName"></param> /// <returns></returns> private static bool ExportToInspector(ICommonExplorer inspectorOrExplorer, IItem currentItem, string tmpFile, string attachmentName) { if (currentItem == null) { LOG.Warn("No current item."); return false; } OlObjectClass itemClass = currentItem.Class; bool isMail = OlObjectClass.olMail.Equals(itemClass); bool isAppointment = OlObjectClass.olAppointment.Equals(itemClass); if (!isMail && !isAppointment) { LOG.Warn("Item is no mail or appointment."); return false; } MailItem mailItem = null; try { if (isMail) { //mailItem = COMWrapper.Cast<MailItem>(currentItem); mailItem = (MailItem)currentItem; if (mailItem.Sent) { LOG.WarnFormat("Item already sent, can't export to {0}", currentItem.Subject); return false; } } // Make sure the inspector is activated, only this way the word editor is active! // This also ensures that the window is visible! inspectorOrExplorer.Activate(); bool isTextFormat = false; if (isMail) { isTextFormat = OlBodyFormat.olFormatPlain.Equals(mailItem.BodyFormat); } if (isAppointment || !isTextFormat) { // Check for wordmail, if so use the wordexporter // http://msdn.microsoft.com/en-us/library/dd492012%28v=office.12%29.aspx // Earlier versions of Outlook also supported an Inspector.HTMLEditor object property, but since Internet Explorer is no longer the rendering engine for HTML messages and posts, HTMLEditor is no longer supported. IWordDocument wordDocument = null; if (inspectorOrExplorer is IExplorer) { var explorer = inspectorOrExplorer as IExplorer; wordDocument = explorer.ActiveInlineResponseWordEditor; } else if (inspectorOrExplorer is IInspector) { var inspector = inspectorOrExplorer as IInspector; if (inspector.IsWordMail()) { wordDocument = inspector.WordEditor; } } if (wordDocument != null) { try { if (WordExporter.InsertIntoExistingDocument(wordDocument.Application, wordDocument, tmpFile, null, null)) { LOG.Info("Inserted into Wordmail"); wordDocument.Dispose(); return true; } } catch (Exception exportException) { LOG.Error("Error exporting to the word editor, trying to do it via another method", exportException); } } else if (isAppointment) { LOG.Info("Can't export to an appointment if no word editor is used"); return false; } else { LOG.Info("Trying export for outlook < 2007."); } } // Only use mailitem as it should be filled!! LOG.InfoFormat("Item '{0}' has format: {1}", mailItem.Subject, mailItem.BodyFormat); string contentID; if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) { contentID = Guid.NewGuid().ToString(); } else { LOG.Info("Older Outlook (<2007) found, using filename as contentid."); contentID = Path.GetFileName(tmpFile); } // Use this to change the format, it will probably lose the current selection. //if (!OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) { // LOG.Info("Changing format to HTML."); // currentMail.BodyFormat = OlBodyFormat.olFormatHTML; //} bool inlinePossible = false; if (inspectorOrExplorer is IInspector && OlBodyFormat.olFormatHTML.Equals(mailItem.BodyFormat)) { // if html we can try to inline it // The following might cause a security popup... can't ignore it. try { IHTMLDocument2 document2 = (inspectorOrExplorer as IInspector).HTMLEditor as IHTMLDocument2; if (document2 != null) { IHTMLSelectionObject selection = document2.selection; if (selection != null) { IHTMLTxtRange range = selection.createRange(); if (range != null) { // First paste, than attach (otherwise the range is wrong!) range.pasteHTML("<BR/><IMG border=0 hspace=0 alt=\"" + attachmentName + "\" align=baseline src=\"cid:" + contentID + "\"><BR/>"); inlinePossible = true; } else { LOG.DebugFormat("No range for '{0}'", inspectorOrExplorer.Caption); } } else { LOG.DebugFormat("No selection for '{0}'", inspectorOrExplorer.Caption); } } else { LOG.DebugFormat("No HTML editor for '{0}'", inspectorOrExplorer.Caption); } } catch (Exception e) { // Continue with non inline image LOG.Warn("Error pasting HTML, most likely due to an ACCESS_DENIED as the user clicked no.", e); } } // Create the attachment (if inlined the attachment isn't visible as attachment!) using (IAttachment attachment = mailItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible ? 0 : 1, attachmentName)) { if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) { // Add the content id to the attachment, this only works for Outlook >= 2007 try { IPropertyAccessor propertyAccessor = attachment.PropertyAccessor; propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); } catch { // Ignore } } } } catch (Exception ex) { LOG.WarnFormat("Problem while trying to add attachment to Item '{0}' : {1}", inspectorOrExplorer.Caption, ex); return false; } try { inspectorOrExplorer.Activate(); } catch (Exception ex) { LOG.Warn("Problem activating inspector/explorer: ", ex); return false; } LOG.Debug("Finished!"); return true; }
/// <summary> /// Export the file to the supplied inspector /// </summary> /// <param name="inspector">Inspector</param> /// <param name="currentItem">Item</param> /// <param name="tmpFile"></param> /// <param name="attachmentName"></param> /// <returns></returns> private static bool ExportToInspector(ICommonExplorer inspectorOrExplorer, IItem currentItem, string tmpFile, string attachmentName) { if (currentItem == null) { LOG.Warn("No current item."); return(false); } OlObjectClass itemClass = currentItem.Class; bool isMail = OlObjectClass.olMail.Equals(itemClass); bool isAppointment = OlObjectClass.olAppointment.Equals(itemClass); if (!isMail && !isAppointment) { LOG.Warn("Item is no mail or appointment."); return(false); } MailItem mailItem = null; try { if (isMail) { //mailItem = COMWrapper.Cast<MailItem>(currentItem); mailItem = (MailItem)currentItem; if (mailItem.Sent) { LOG.WarnFormat("Item already sent, can't export to {0}", currentItem.Subject); return(false); } } // Make sure the inspector is activated, only this way the word editor is active! // This also ensures that the window is visible! inspectorOrExplorer.Activate(); bool isTextFormat = false; if (isMail) { isTextFormat = OlBodyFormat.olFormatPlain.Equals(mailItem.BodyFormat); } if (isAppointment || !isTextFormat) { // Check for wordmail, if so use the wordexporter // http://msdn.microsoft.com/en-us/library/dd492012%28v=office.12%29.aspx // Earlier versions of Outlook also supported an Inspector.HTMLEditor object property, but since Internet Explorer is no longer the rendering engine for HTML messages and posts, HTMLEditor is no longer supported. IWordDocument wordDocument = null; if (inspectorOrExplorer is IExplorer) { var explorer = inspectorOrExplorer as IExplorer; wordDocument = explorer.ActiveInlineResponseWordEditor; } else if (inspectorOrExplorer is IInspector) { var inspector = inspectorOrExplorer as IInspector; if (inspector.IsWordMail()) { wordDocument = inspector.WordEditor; } } if (wordDocument != null) { try { if (WordExporter.InsertIntoExistingDocument(wordDocument.Application, wordDocument, tmpFile, null, null)) { LOG.Info("Inserted into Wordmail"); wordDocument.Dispose(); return(true); } } catch (Exception exportException) { LOG.Error("Error exporting to the word editor, trying to do it via another method", exportException); } } else if (isAppointment) { LOG.Info("Can't export to an appointment if no word editor is used"); return(false); } else { LOG.Info("Trying export for outlook < 2007."); } } // Only use mailitem as it should be filled!! LOG.InfoFormat("Item '{0}' has format: {1}", mailItem.Subject, mailItem.BodyFormat); string contentID; if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) { contentID = Guid.NewGuid().ToString(); } else { LOG.Info("Older Outlook (<2007) found, using filename as contentid."); contentID = Path.GetFileName(tmpFile); } // Use this to change the format, it will probably lose the current selection. //if (!OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) { // LOG.Info("Changing format to HTML."); // currentMail.BodyFormat = OlBodyFormat.olFormatHTML; //} bool inlinePossible = false; if (inspectorOrExplorer is IInspector && OlBodyFormat.olFormatHTML.Equals(mailItem.BodyFormat)) { // if html we can try to inline it // The following might cause a security popup... can't ignore it. try { IHTMLDocument2 document2 = (inspectorOrExplorer as IInspector).HTMLEditor as IHTMLDocument2; if (document2 != null) { IHTMLSelectionObject selection = document2.selection; if (selection != null) { IHTMLTxtRange range = selection.createRange(); if (range != null) { // First paste, than attach (otherwise the range is wrong!) range.pasteHTML("<BR/><IMG border=0 hspace=0 alt=\"" + attachmentName + "\" align=baseline src=\"cid:" + contentID + "\"><BR/>"); inlinePossible = true; } else { LOG.DebugFormat("No range for '{0}'", inspectorOrExplorer.Caption); } } else { LOG.DebugFormat("No selection for '{0}'", inspectorOrExplorer.Caption); } } else { LOG.DebugFormat("No HTML editor for '{0}'", inspectorOrExplorer.Caption); } } catch (Exception e) { // Continue with non inline image LOG.Warn("Error pasting HTML, most likely due to an ACCESS_DENIED as the user clicked no.", e); } } // Create the attachment (if inlined the attachment isn't visible as attachment!) using (IAttachment attachment = mailItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible ? 0 : 1, attachmentName)) { if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) { // Add the content id to the attachment, this only works for Outlook >= 2007 try { IPropertyAccessor propertyAccessor = attachment.PropertyAccessor; propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); } catch { // Ignore } } } } catch (Exception ex) { LOG.WarnFormat("Problem while trying to add attachment to Item '{0}' : {1}", inspectorOrExplorer.Caption, ex); return(false); } try { inspectorOrExplorer.Activate(); } catch (Exception ex) { LOG.Warn("Problem activating inspector/explorer: ", ex); return(false); } LOG.Debug("Finished!"); return(true); }