Programmatic search and replacement in MS Word with Microsoft.Office.Interop.Word



  • There's a code

    using Word = Microsoft.Office.Interop.Word;
    

    static void Main(string[] args)
    {
    Word.Application objWord = new Word.Application();
    objWord.Visible = true;
    Word.Document doc = objWord.Documents.Open("test.docx");
    object findText = "A";
    object replaceWithText = "B";
    object matchCase = false;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundsLike = false;
    object matchAllWordForms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiacritics = false;
    object matchAlefHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = Word.WdReplace.wdReplaceAll;
    object wrap = Word.WdFindWrap.wdFindContinue;
    doc.Activate();
    objWord.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
    ref matchWildCards, ref matchSoundsLike,
    ref matchAllWordForms, ref forward,
    ref wrap, ref format, ref replaceWithText,
    ref replace, ref matchKashida, ref matchDiacritics,
    ref matchAlefHamza, ref matchControl);
    }

    This code replaces all the letters "A" to "B," but somehow ignores the columns (maybe something else) how do you fix it? I need to change the symbol A to B throughout the document (text, columns, tables, contents, etc.)

    P.S. should be replaced by a symbol, the line is not. Maybe there's more "speed methods"



  • Maybe we should look the other way. Microsoft.Office.Interop.Word.HeadersFooters It's a characteristic. section.Headers and section.Footers which are taken if the document is broken Word.Document docfor sections, for example

    foreach (Microsoft.Office.Interop.Word.Section section in doc.Sections)
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2