Displaying Hyphenated Words in DatatGridView


Using this code, we can find all hyphenated words from a word document and display in datagridview

Hyphenated Words


This function reads all paragraphs and write into a new textfile line by line. Because reading text from textfile is easier than reading from word document.

public void Hyphenated_Words(Office.IRibbonControl control)
{
string ref_style = "";
consis = "Hyp";
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 in that form and write the following function in the load event of that form.

private void rtbForm_Load(object sender, EventArgs e)
{
//Hyphenated Words
MatchCollection results;
this.Text = "Check consistency for Hyphenated words";
this.dataGridView1.Columns[0].HeaderText = "Hyphenated Words";
string docName = null;
int row = 0;
string[] strArr = null;
int count = 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(\\w+)[\\-]([\\w\\-]+)\\b");
//IEnumerable results = myReg.Matches(line)
// .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();
char[] splitchar = { '-' };
strArr = s.Split(splitchar);
for (count = 0; count <= strArr.Length - 1; count++)
{
//MessageBox.Show(strArr[count]);
if (Information.IsNumeric(strArr[count])) { }
else
{
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);
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: