Check Units in a word document
Find all units like ten grams,5 seconds,so on.. in a word document
Check Units
Finding all units like ten grams,5 seconds,so on...using regular expressions and calling replace form
private void Check_Units(object sender, EventArgs e)
{
string ref_style = "";
MatchCollection results;
string Num = null;
string myAbbrPref = null;
string myExpPref = null;
string myAbbrUnit = null;
string myExpUnit = null;
string myUnits2 = null;
Num = "\\b(\\d[\\,\\d\\." + Strings.ChrW(8211) + "\\-]*|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|hundred|thousand|[mb]illion)";
myAbbrPref = "(Y|Z|E|P|T|G|M|k|h|da|d|c|m|µ|n|p|f|a|z|y)";
myExpPref = "(yotta|zeta|exa|peta|tera|giga|mega|kilo|hector?|deca|deci|centi|milli|micro|nano|pico|femto|atto|zepto|yocto)";
myAbbrUnit = "(((amps?|B|b|bp|Bq|C|cal|cd|Da?|eV|F|g|Gy|H|h|Hz|J|K|L|l|lm|lx|M|m|mins?|mol|N|Np|nt|Pa|rads?|s|sr|Sv|T|U|V|W|Wb)\\b)|Å|a\\.m\\.u\\.|\" & ChrW(937))";
myExpUnit = "(ångstroms?|angstroms?|amps?|amperes?|atomic mass units?|amus?|bels?|bytes?|bar|base ?pairs?|becq[uerel]*s?|coloumbs?|calories?|candelas?|daltons?|electron ?volts?|farads?|grams?|grays?|henrys?|hours?|hertzs?|joules?|(deg[ree]*|degK|kelvins?)|lit[er]{2}s?|lumens?|luxs?|molar ?mass|met[re]{2}s?|minutes?|moles?|newtons?|nepers?|nucleotides?|pascals?|radians?|sec(ond)?s?|steradians?|sieverts?|teslas?|units?|volts?|watts?|webers?|ohms?|svedbergs?)\\b";
myUnits2 = "(%|°|°C|\\-?mer|S|[kM]bp)\\b";
Regex myReg;
int tPoint = 0;
int paraCount = 0;
object Count = 0;
object Unit = Word.WdUnits.wdCharacter;
object Extend = Word.WdMovementType.wdExtend;
object wUnit = Word.WdUnits.wdStory;
object missing = System.Reflection.Missing.Value;
paraCount = prow.WrdApp.ActiveDocument.Paragraphs.Count;
for (int k = 1; k <= paraCount; k++)
{
object sty = prow.WrdApp.ActiveDocument.Paragraphs[k].get_Style();
Word.Style styleName = (Word.Style)sty;
myReg = new Regex(Num + "[\\- ]?" + myAbbrPref + "[\\- ]?" + myAbbrUnit);
//IEnumerable
// .OfType
// .Select(m => m.Value)
// .Distinct();
results = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
//foreach (string s in results)
foreach (Match s1 in results)
{
string s = s1.Value.ToString();
MatchCollection mtCol = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
tPoint = prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text.IndexOf(s);
prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Characters[tPoint + 1].Select();
Count = s.Length - 1;
prow.WrdApp.ActiveWindow.Selection.MoveRight(ref Unit, ref Count, ref Extend);
frmReplace frmRepl = new frmReplace(s);
frmRepl.Text = "Checking Units";
frmRepl.lbltxt = "";
frmRepl.ShowDialog();
if (frmRepl.lbltxt == "rep")
{
prow.WrdApp.ActiveWindow.Selection.Text = frmRepl.repltxt;
}
else if (frmRepl.lbltxt == "cls")
{
return;
}
myReg = new Regex(Num + "[\\- ]?" + myExpPref + "[\\- ]?" + myAbbrUnit);
//IEnumerable
// .OfType
// .Select(m => m.Value)
// .Distinct();
results = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
//foreach (string s in results)
foreach (Match s1 in results)
{
string s = s1.Value.ToString();
MatchCollection mtCol = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
tPoint = prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text.IndexOf(s);
prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Characters[tPoint + 1].Select();
Count = s.Length - 1;
prow.WrdApp.ActiveWindow.Selection.MoveRight(ref Unit, ref Count, ref Extend);
frmReplace frmRepl = new frmReplace(s);
frmRepl.Text = "Checking Units";
frmRepl.lbltxt = "";
frmRepl.ShowDialog();
if (frmRepl.lbltxt == "rep")
{
prow.WrdApp.ActiveWindow.Selection.Text = frmRepl.repltxt;
}
else if (frmRepl.lbltxt == "cls")
{
return;
}
}
//------------------------------------------------------------------------------------
myReg = new Regex(Num + "[\\- ]?" + myExpPref + "[\\- ]?" + myExpUnit);
//IEnumerable
// .OfType
// .Select(m => m.Value)
// .Distinct();
results = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
//foreach (string s in results)
foreach (Match s1 in results)
{
string s = s1.Value.ToString();
MatchCollection mtCol = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
tPoint = prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text.IndexOf(s);
prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Characters[tPoint + 1].Select();
Count = s.Length - 1;
prow.WrdApp.ActiveWindow.Selection.MoveRight(ref Unit, ref Count, ref Extend);
frmReplace frmRepl = new frmReplace(s);
frmRepl.Text = "Checking Units";
frmRepl.lbltxt = "";
frmRepl.ShowDialog();
if (frmRepl.lbltxt == "rep")
{
prow.WrdApp.ActiveWindow.Selection.Text = frmRepl.repltxt;
}
else if (frmRepl.lbltxt == "cls")
{
return;
}
}
//------------------------------------------------------------------------------------
myReg = new Regex(Num + "[\\- ]?" + myAbbrUnit);
//IEnumerable
// .OfType
// .Select(m => m.Value)
// .Distinct();
results = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
//foreach (string s in results)
foreach (Match s1 in results)
{
string s = s1.Value.ToString();
MatchCollection mtCol = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
tPoint = prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text.IndexOf(s);
prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Characters[tPoint + 1].Select();
Count = s.Length - 1;
prow.WrdApp.ActiveWindow.Selection.MoveRight(ref Unit, ref Count, ref Extend);
frmReplace frmRepl = new frmReplace(s);
frmRepl.Text = "Checking Units";
frmRepl.lbltxt = "";
frmRepl.ShowDialog();
if (frmRepl.lbltxt == "rep")
{
prow.WrdApp.ActiveWindow.Selection.Text = frmRepl.repltxt;
}
else if (frmRepl.lbltxt == "cls")
{
return;
}
}
//------------------------------------------------------------------------------------
myReg = new Regex(Num + "[\\- ]?" + myExpUnit);
//IEnumerable
// .OfType
// .Select(m => m.Value)
// .Distinct();
results = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
//foreach (string s in results)
foreach (Match s1 in results)
{
string s = s1.Value.ToString();
MatchCollection mtCol = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
tPoint = prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text.IndexOf(s);
if (tPoint != -1)
{
prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Characters[tPoint + 1].Select();
Count = s.Length - 1;
prow.WrdApp.ActiveWindow.Selection.MoveRight(ref Unit, ref Count, ref Extend);
frmReplace frmRepl = new frmReplace(s);
frmRepl.Text = "Checking Units";
frmRepl.lbltxt = "";
frmRepl.ShowDialog();
if (frmRepl.lbltxt == "rep")
{
prow.WrdApp.ActiveWindow.Selection.Text = frmRepl.repltxt;
}
else if (frmRepl.lbltxt == "cls")
{
return;
}
}
}
//------------------------------------------------------------------------------------
myReg = new Regex(Num + "[\\- ]?" + myUnits2);
//IEnumerable
// .OfType
// .Select(m => m.Value)
// .Distinct();
results = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
//foreach (string s in results)
foreach (Match s1 in results)
{
string s = s1.Value.ToString();
MatchCollection mtCol = myReg.Matches(prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text);
tPoint = prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Text.IndexOf(s);
prow.WrdApp.ActiveDocument.Paragraphs[k].Range.Characters[tPoint + 1].Select();
Count = s.Length - 1;
prow.WrdApp.ActiveWindow.Selection.MoveRight(ref Unit, ref Count, ref Extend);
frmReplace frmRepl = new frmReplace(s);
frmRepl.Text = "Checking Units";
frmRepl.lbltxt = "";
frmRepl.ShowDialog();
if (frmRepl.lbltxt == "rep")
{
prow.WrdApp.ActiveWindow.Selection.Text = frmRepl.repltxt;
}
else if (frmRepl.lbltxt == "cls")
{
return;
}
}
//------------------------------------------------------------------------------------
}
}
prow.WrdApp.ActiveWindow.Selection.WholeStory();
prow.WrdApp.Selection.HomeKey(ref wUnit, ref missing);
MessageBox.Show("Checked Units patterns", "Copy Editing", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
Create a new windows form named frmReplace and Set the following controls
Label1 -> Text = Selected Text
Label2 -> Text = Replace With
TextBox1 -> Name=txtSelText and Text=""
TextBox2 -> Name=txtreplace and Text=""
Button1 -> Name=btnReplace and text = &Replace
Button2 -> Name=btnNext and text = &Next
Button1 -> Name=btnClose and text = &Close
write the following code within that form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace W2XML
{
public partial class frmReplace : Form
{
public string ctxt = "";
public string lbltxt = "";
public string repltxt = "";
public frmReplace(string ctxt)
{
InitializeComponent();
txtSelText.Text = ctxt;
txtReplace.Text = repltxt;
}
private void btnReplace_Click(object sender, EventArgs e)
{
if ( txtReplace.Text.Trim() == null)
{
MessageBox.Show("Null value in Replace text. Please check");
return;
}
lblTag.Text = "rep";
lbltxt = "rep";
repltxt = txtReplace.Text;
this.Hide();
}
private void btnNext_Click(object sender, EventArgs e)
{
lblTag.Text = "next";
lbltxt = "next";
this.Hide();
}
private void btnClose_Click(object sender, EventArgs e)
{
lblTag.Text = "cls";
lbltxt = "cls";
this.Hide();
}
private void frmReplace_Load(object sender, EventArgs e)
{
txtReplace.Select();
}
}
}