/// <summary> /// Gets the paragraph after the one at the caret /// </summary> /// <param name="paragraph">the paragraph</param> public void GetParagraphAfterCaret(out String paragraph) { String retVal = String.Empty; Word.Range paraRange = WordApp.Selection.Range.Paragraphs[1].Range; if (paraRange != null) { Word.Range next = paraRange.Next(Word.WdUnits.wdParagraph); while (next != null) { if (!String.IsNullOrEmpty(next.Text.Trim())) { retVal = next.Text; break; } next = next.Next(Word.WdUnits.wdParagraph); } if (next == null) { retVal = String.Empty; } } paragraph = retVal; }
/// <summary> /// Returns the paragraph before the one at the /// caret position /// </summary> /// <param name="paragraph">the paragraph</param> public void GetParagraphBeforeCaret(out String paragraph) { String retVal = String.Empty; Word.Range paraRange = WordApp.Selection.Range.Paragraphs[1].Range; if (paraRange != null) { Word.Range prev = paraRange.Previous(Word.WdUnits.wdParagraph); while (prev != null) { if (!String.IsNullOrEmpty(prev.Text.Trim())) { retVal = prev.Text; break; } prev = prev.Next(Word.WdUnits.wdParagraph); } if (prev == null) { retVal = String.Empty; } } paragraph = retVal; }
/// <summary> /// Returns the sentence after the one at the caret position /// </summary> /// <param name="sentence">senctence</param> public void GetSentenceAfterCaret(out String sentence) { sentence = String.Empty; Word.Range sentenceRange = WordApp.Selection.Range.Sentences[1]; if (sentenceRange != null) { Word.Range next = sentenceRange.Next(Word.WdUnits.wdSentence); if (next != null) { sentence = next.Text; } } }
/// <summary> /// Returns the offset of the start of the next sentence /// </summary> /// <returns>offset, -1 on error</returns> private int getStartOfNextSentence() { if (WordApp != null) { Word.Range sentenceRange = WordApp.Selection.Range.Sentences[1]; Word.Range nextSentence = sentenceRange.Next(); if (nextSentence != null) { int start = nextSentence.Start; return(start); } } return(-1); }
/// <summary> /// Extend selection /// </summary> /// <param name="range"></param> private void ExtendSelection() { string r = Convert.ToChar(0x000D).ToString(); // \r Microsoft.Office.Interop.Word.Range range = Globals.ThisAddIn.Application.ActiveDocument.Range(Globals.ThisAddIn.Application.Selection.Range.Start, Globals.ThisAddIn.Application.Selection.Range.End); if (range.Tables.Count > 0) { foreach (Microsoft.Office.Interop.Word.Table table in range.Tables) { if (range.Start > table.Range.Start) { range.Start = table.Range.Start; } if (range.End < table.Range.End) { range.End = table.Range.End; // +1; Microsoft.Office.Interop.Word.Range nextChar = range.Next(Microsoft.Office.Interop.Word.WdUnits.wdCharacter); if (nextChar != null && !nextChar.Text.Equals(r)) { nextChar.InsertParagraphBefore(); } range.End++; } } } if (range.Paragraphs.Count > 1) { range = Globals.ThisAddIn.Application.ActiveDocument.Range(range.Paragraphs.First.Range.Start, range.Paragraphs.Last.Range.End); } range.Select(); }