Cleaning Active Word Document


Cleaning Active Word Document ie. removing unnecessary empty paras, section breaks, page breaks and column breaks

The following functions are used to remove empty para, section breaks, page breaks, column breaks and unnecessaty spaces


public void document_cleanup() {
remove_empty_para();

//remove bold para
int pCount = prow.WrdApp.ActiveDocument.Paragraphs.Count;
for (int i = 1; i <= pCount; i++) {
prow.WrdApp.ActiveDocument.Paragraphs[i].Range.Select();
if (prow.WrdApp.Selection.Font.Bold == -1) {
prow.WrdApp.Selection.Font.Bold = 0;
}
}
//remove italic para
for (int i = 1; i <= pCount; i++)
{
prow.WrdApp.ActiveDocument.Paragraphs[i].Range.Select();
if (prow.WrdApp.Selection.Font.Italic == -1)
{
prow.WrdApp.Selection.Font.Italic = 0;
}
}
}

public void remove_empty_para()
{

//empty_para_remove
object missing = System.Reflection.Missing.Value;
foreach(Word.Paragraph ap in prow.WrdApp.ActiveDocument.Paragraphs){
if (ap.Range.Text == "\r" || ap.Range.Text == " \r" || ap.Range.Text == " \r" || ap.Range.Text == "\t\r")
{
ap.Range.Select();
ap.Range.Delete(ref missing, ref missing);
}
}

//section breaks
w_replace("^b", "^p");

//page breaks
w_replace("^m", "^p");

//column breaks
w_replace("^n", "^p");

//space ^p
w_replace(" ^p", "^p");
w_replace(" ^p", "^p");
w_replace(" ^p", "^p");
w_replace(" ^p", "^p");

//prabu
w_replace("^p^p", "^p");
w_replace("^p^p^p", "^p");
w_replace("^p^p^p^p", "^p");


}

public void w_replace(string sourceTxt, string desTxt) {

object missing = System.Reflection.Missing.Value;
object findtext = "";
object matchcase = false;
object matchwholeword = false;
object matchwildcards = false;
object soundslike = false;
object matchwordforms = false;
object forward = true;
object wrap = Word.WdFindWrap.wdFindContinue;
object format = true;
object replacewith = missing;
object replace = true;
object replaceAll = Word.WdReplace.wdReplaceAll;
object matchkashida = false;
object matchdiacritics = false;
object alehamza = false;
object matchcontrol = false;
object wUnit = Word.WdUnits.wdStory;
object pUnit = Word.WdUnits.wdWord;
prow.WrdApp.Selection.HomeKey(ref wUnit, ref missing);
prow.WrdApp.Selection.Find.ClearFormatting();
prow.WrdApp.Selection.Find.Replacement.ClearFormatting();
findtext = sourceTxt;
prow.WrdApp.Selection.Find.Replacement.Font.Bold = 0;
prow.WrdApp.Selection.Find.Replacement.Font.Italic = 0;
prow.WrdApp.Selection.Find.Replacement.Font.Superscript = 0;
prow.WrdApp.Selection.Find.Replacement.Font.Subscript = 0;
prow.WrdApp.Selection.Find.Replacement.Text = desTxt;
prow.WrdApp.Selection.Find.Execute(ref findtext, ref matchcase, ref matchwholeword, ref matchwildcards, ref soundslike, ref matchwordforms, ref forward, ref wrap, ref format, ref replacewith, ref replaceAll, ref matchkashida, ref matchdiacritics, ref alehamza, ref matchcontrol);


}


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: