Finding capital words in a Word Document and displays in DataGridView
Finding capital words in a Word Document and displays in a DataGridView
This function is used to read all paragraphs from a active word document and stores in a textfile.
public void cap_words(Office.IRibbonControl control)
{
string ref_style = "";
consis = "Caps";
rtbForm rtb = new rtbForm();
int paraCnt = 0;
object wUnit = Word.WdUnits.wdStory;
object missing = System.Reflection.Missing.Value;
try
{
docName = Path.GetFileNameWithoutExtension(prow.WrdApp.ActiveDocument.FullName);
}
catch (Exception erd)
{
MessageBox.Show(erd.Message);
}
StreamWriter TextFile = new StreamWriter(prow.WrdApp.ActiveDocument.Path + "\\" + docName + ".txt");
paraCnt = prow.WrdApp.ActiveDocument.Paragraphs.Count;
for (int z = 1; z <= paraCnt; z++)
{
object sty = prow.WrdApp.ActiveDocument.Paragraphs[z].get_Style();
Word.Style styleName = (Word.Style)sty;
TextFile.WriteLine(prow.WrdApp.ActiveDocument.Paragraphs[z].Range.Text);
}
TextFile.Close();
prow.WrdApp.Selection.HomeKey(ref wUnit, ref missing);
rtbForm objRtb = new rtbForm();
objRtb.Show();
}
Create a new win form and place a datagridview and type the following function on form_load event. This function used to find all the capital words from the textfile which created in the previous function using regular experessions and that words displays in datagridview.
private void rtbForm_Load(object sender, EventArgs e)
{
//Capitalised Words
if (Ribbon1.consis == "Caps")
{
this.Text = "Check consistency for Capitalised words";
this.dataGridView1.Columns[0].HeaderText = "Capitalised Words";
MatchCollection results;
string docName = null;
int row = 0;
docName = Path.GetFileNameWithoutExtension(prow.WrdApp.ActiveDocument.FullName);
string line;
StreamReader file = new StreamReader(prow.WrdApp.ActiveDocument.Path + "\\" + docName + ".txt");
while ((line = file.ReadLine()) != null)
{
Regex myReg = new Regex(@"(\b[A-Z]\w*)");
//IEnumerable
// .OfType
// .Select(m => m.Value)
// .Distinct();
results = myReg.Matches(line);
//foreach (string s in results)
foreach (Match s1 in results)
{
string s = s1.Value.ToString();
if (s.Length > 2)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[row].Cells["Column1"].Value = s;
dataGridView1.Rows[row].Cells["Column2"].Value = line;
dataGridView1.Rows[row].Cells["Column3"].Value = "Click here";
row = row + 1;
}
}
}
file.Close();
if (File.Exists(prow.WrdApp.ActiveDocument.Path + "\\" + docName + ".txt"))
{
File.Delete(prow.WrdApp.ActiveDocument.Path + "\\" + docName + ".txt");
}
dataGridView1.Sort(dataGridView1.Columns["Column1"], System.ComponentModel.ListSortDirection.Ascending);
}
}
Please add description to the article.
Meetu Choudhary
Site Coordinator