Identifying a Range
A Range has several properties to help identify it. The…
A Range has several properties to help identify it. The…
In this post, i'll share my experience using Visual Studio…
You have several ways to get a range in a word document. VSTO have already considered several document-level collections such as Paragraphs, Sentences, Words and Characters that return Range Object. The most common way to get a Range is to use the Range method on the Documen object. Start and End position represent the start and end position of the Range you want to get within the document. If you omit the Start parameter, it defaults to 0, which is the first position in the document. If you omit the End parameter, it defaults to the last position in the document.
In the nampspace code, type this.
[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Reflection;
using System.Windows.Forms;
[/sourcecode]
Get all Range
To get all range in the document, type the code bellow:
[sourcecode language=”csharp”]
public void sampleCode()
{
object missingValue = System.Reflection.Missing.Value;
Word.Range range = Globals.ThisAddIn.Application.ActiveDocument.Range(ref missingValue, ref missingValue);
MessageBox.Show(String.Format("{0}", range.Text));
}
[/sourcecode]
MessageBox used for output of the code. Beside it, we know Console.WriteLine. But, MessageBox more efficient. (lebih…)