private string GetBodyTextIfWordMail(IWSInspector safeInspector) { // For Office XP, when the cursor wasn't positioned within the body text section // (e.g. in the To box), the safe inspector text field threw an exception when accessed // so it's now being done via the Word document object. string bodyText; MSWord.Document doc = safeInspector.WordEditor as MSWord.Document; MSWord.Application application = null; MSWord.Window window = null; MSWord.StoryRanges storyRanges = null; MSWord.Range range = null; try { // Need to set the focus, otherwise the Range.Text properties throw COM exceptions // if the cursor wasn't in the body text section application = doc.Application; window = application.ActiveWindow; window.SetFocus(); storyRanges = doc.StoryRanges; range = storyRanges[MSWord.WdStoryType.wdMainTextStory]; bodyText = range.Text; } catch (COMException) // may throw from O2007 { bodyText = safeInspector.Text; } finally { if (range != null && Marshal.IsComObject(range)) Marshal.ReleaseComObject(range); if (storyRanges != null && Marshal.IsComObject(storyRanges)) Marshal.ReleaseComObject(storyRanges); if (window != null && Marshal.IsComObject(window)) Marshal.ReleaseComObject(window); if (application != null && Marshal.IsComObject(application)) Marshal.ReleaseComObject(application); if (doc != null && Marshal.IsComObject(doc)) Marshal.ReleaseComObject(doc); } return bodyText; }
internal string GetSafeMailItemText(IWSInspector safeInspector) { //VL/DS : In OfficeXP, with an empty message body, rich or plain text format, safeInspector.Text //throws a 'catastrophic failure' which is missed by the catch block! //So, determine if message is empty before calling safeInspector.Text, and return empty string when //necessary. if (null == _wsMailItem.Body) return String.Empty; string trimmedBody = _wsMailItem.Body; trimmedBody = trimmedBody.Trim(); if (string.IsNullOrEmpty(trimmedBody)) return String.Empty; try { return safeInspector.Text; // this throws if there is no text and format is rtf } catch { return String.Empty; } }