Microsoft Word AutoCorrect VBA, C#, VB.Net example

Microsoft Word AutoCorrect VBA, C#, VB.Net example

AutoCorrect

Auto Correct object use to automatically correct spellings in your document. Developer can customize default dictionary to add his own words or control the behavior of Auto Correct feature in Word. In this article we will see how to enable AutoCorrect feature using code:

Code example

Public EnableAutoCorrect()
	With AutoCorrect 
		.CorrectCapsLock = True 
		.CorrectDays = True 
		.CorrectHangulAndAlphabet=True
		.CorrectKeyboardSetting=True
		.CorrectInitialCaps=True
		.CorrectTableCells=True
		.HangulAndAlphabetAutoAdd=True
		.FirstLetterAutoAdd=True
		.Entries.Add Name:="recognise", Value:="recognize" 
	End With
End Sub

In above code we added an Auto Correct entry where code instructed to replace “recognise” to “recognize” automatically:

Output

Now put a space and see the output

C# code example

private void btnProtect_Click(object sender, RibbonControlEventArgs e)
{            
    //Bind application reference
    wordApp.Application oApplication = Globals.ThisAddIn.Application;

    //Declare autocorrect object
    wordApp.AutoCorrect oAutoCorrect = oApplication.AutoCorrect;

    //Set various flags
    oAutoCorrect.CorrectCapsLock = true;
    oAutoCorrect.CorrectDays = true;
    oAutoCorrect.CorrectHangulAndAlphabet = true;
    oAutoCorrect.CorrectKeyboardSetting = true;
    oAutoCorrect.CorrectInitialCaps = true;
    oAutoCorrect.CorrectTableCells = true;
    oAutoCorrect.HangulAndAlphabetAutoAdd = true;
    oAutoCorrect.FirstLetterAutoAdd = true;
    oAutoCorrect.Entries.Add("recognise", "recognize");
}

VB.Net code example

Private Sub btnAutoCorrectExample_Click(sender As Object, e As RibbonControlEventArgs) Handles btnAutoCorrectExample.Click
    Dim oAutoCorrect As word.AutoCorrect
    oAutoCorrect=Globals.ThisAddIn.Application.AutoCorrect

    oAutoCorrect.CorrectCapsLock=True
    oAutoCorrect.CorrectDays=True
    oAutoCorrect.CorrectHangulAndAlphabet=True
    oAutoCorrect.CorrectKeyboardSetting=True
    oAutoCorrect.CorrectInitialCaps=True
    oAutoCorrect.CorrectTableCells=True
    oAutoCorrect.HangulAndAlphabetAutoAdd=True
    oAutoCorrect.FirstLetterAutoAdd=True
    oAutoCorrect.Entries.Add("recognise","recognize")
End Sub

VBA code example

Public Sub AutoCorrectVBAExample()
    With Application.AutoCorrect
        .CorrectCapsLock = True
        .CorrectDays = True
        .CorrectHangulAndAlphabet = True
        .CorrectKeyboardSetting = True
        .CorrectInitialCaps = True
        .CorrectTableCells = True
        .HangulAndAlphabetAutoAdd = True
        .FirstLetterAutoAdd = True
        .Entries.Add "serialise", "serialize"
    End With
End Sub

Next >> Special Symbols vs Multi-Byte Symbols Word

Leave a Reply

Your email address will not be published.