Find and Replace string in Word Document using C# and VB.Net in Windows Application

Last Reply 8 months ago By dharmendr

Posted 8 months ago

Hi! I have question and answers in word file. This question and answers has many errors. I want using windows application correct its and save into file after correct.

Input file with question and answers:

@1.Which keyboard shortcut bolds selected text?

$A)Ctrl+B

$B)Alt+B

$C)File/Format/Bold

$D)None of the above

@2What would you see while spell checking the phrase "My father was write"?

$А)The word "write" is mispelled

$Б)No errors

$С)The verb of the phrase will be highlighted

$Д)A blue squiggly underline under the word "write"

@3Which elements of a Word document can be displayed in color

$А)Only graphics

$В)Only text

$С)All elements

$Д)All elements, but only if you have a color printer

 

Output result file with question and answers:

@1.Which keyboard shortcut bolds selected text?

$A)Ctrl+B;

$B)Alt+B;

$C)File/Format/Bold;

$D)None of the above;

@2.What would you see while spell checking the phrase "My father was write"?

$A)The word "write" is misspelled;

$B)No errors;

$C)The verb of the phrase will be highlighted;

$D)A blue squiggly underline under the word "write";

@3.Which elements of a Word document can be displayed in color?

$A)Only graphics;

$B)Only text;

$C)All elements;

$D)All elements, but only if you have a color printer;

You are viewing reply posted by: dharmendr 8 months ago.
Posted 8 months ago Modified on 8 months ago

Hi PRA,

You can't correct all the statement. Only you can replace Д and Б in the sentences. Can't add the .(dot) after the number of each question.

For this you need to write custom code.

Refer below code to replace the Д and Б.

Namespace

C#

using Word = Microsoft.Office.Interop.Word;

VB.Net

Imports Word = Microsoft.Office.Interop.Word

Code

C#

private void button1_Click(object sender, EventArgs e)
{
    object fileName = @"D:\Test.docx";
    Word.Application word = new Word.Application();
    Word.Document doc = new Word.Document();
    object missing = System.Type.Missing;
    try
    {
        doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        doc.Activate();

        foreach (Word.Range docRange in doc.StoryRanges)
        {
            docRange.Text = docRange.Text.Replace("$Д)", "$D)").Replace("$Б)", "$B)");
        }
        doc.Save();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        ((Word._Document)doc).Close();
        ((Word._Application)word).Quit();
    }
}

VB.Net

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim fileName As Object = "D:\Test.docx"
    Dim word As Word.Application = New Word.Application()
    Dim doc As Word.Document = New Word.Document()
    Dim missing As Object = System.Type.Missing

    Try
        doc = word.Documents.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
        doc.Activate()

        For Each docRange As Word.Range In doc.StoryRanges
            docRange.Text = docRange.Text.Replace("$Д)", "$D)").Replace("$Б)", "$B)")
        Next

        doc.Save()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        (CType(doc, Word._Document)).Close()
        (CType(word, Word._Application)).Quit()
    End Try
End Sub