Office Compatible 97—AutoCorrect

by Ross Comer
Development Lead, Microsoft Office Compatible

June 1996

Abstract

This article provides a detailed description of the AutoCorrect feature of Office Compatible and the steps necessary to implement this feature into your custom application.  It also provides sample code for both a C/C++ interface and an OLE Automation interface accessible from Visual Basic or Visual Basic for Applications.

Description

AutoCorrect provides a mechanism for automatically correcting typographical or misspelling errors as a user types, without having to run SpellCheck for those errors.  AutoCorrect corrects common errors such as TWo INitial CAps, lowercasing of days of the week (monday, etc), first letter capitalization of sentences, and accidental cAPS LOCK being held down. This feature includes a default list of common typing errors and a dialog interface that allows a user to add personal typing or spelling errors and their corresponding automatic corrections.

The API set provides OLE Automation and C interfaces that bring up the AutoCorrect dialogs.

Dialogs

The AutoCorrect dialog provides four default correction rules which the user can select using the checkboxes. The user can also add other errors and their corresponding corrections to the default list of exact pattern corrections. A menu option or toolbar button which calls ShowDialog will display this dialog. 

The Exceptions sub-dialog allows the user to also maintain an exception list to the rules selected on the main AutoCorrect dialog. These exceptions include not capitalizing after a specific letter sequence, allowing multiple initial capitalization of specified letter sequences, and other letter sequences as defined by the user. The dialog tab views for exceptions are shown below.

C Interface

To use the AutoCorrect functionality, you will need to get the AutoCorrect interface. In order to get the AutoCorrect interface, use the PiacGetAutoCorrect() method in the IOfficeCompatible interface. If the result of this call is non-NULL, the call succeeded, and you are holding the AutoCorrect interface. Once you have the interface, you can access and manipulate its properties as needed. For the example below, check that pAutoCorrect is non-NULL. 

   // Get the AutoCorrect interface
   IMsocAutoCorrect *pAutoCorrect = pOC->PiacGetAutoCorrect();

The AutoCorrect interface is automatically released when the IOfficeCompatible interface is released. There is no need to do any other releases on this interface.

For more information about the IOfficeCompatible interface, refer to the article “Application Initialization when Using the Office Compatible DLL”.

The AutoCorrect Interface

The following properties can get and set the various AutoCorrect settings. Making these calls with the Boolean value set to true will check the checkbox; false will clear the checkbox. Each call responds to one of the five checkboxes available on the main AutoCorrect dialog. They are listed in the following code in the order they appear on the dialog.

   // TwoInitalCapitals:  
   //   Whether TWo INitial CApitals are corrected
   BOOL FTwoInitialCapitals();
   void SetTwoInitialCapitals(BOOL fTwoInitialCapitals);

   // CapitalizeSentences: 
   //   Whether the first word of sentences are capitalized
   BOOL FCapitalizeSentences();
   void SetCapitalizeSentences(BOOL fCapitalizeSentences);

   // CapitalizeNamesOfDays:  
   //   Whether monday, tuesday, etc are capitalized
   BOOL FCapitalizeNamesOfDays();
   void SetCapitalizeNamesOfDays(BOOL fCapitalizeNamesOfDays);

   // CapsLock:  
   //   Whether the cAPS lOCK key being held down is automatically fixed
   BOOL FCapsLock();
   void SetCapsLock(BOOL fCapsLock);

   // ReplaceText: 
   //   Whether text is automatically replaced from the replacement list
   BOOL FReplaceText(); 
   void SetReplaceText(BOOL fReplaceText);

The FCorrect method checks the given text for a possible AutoCorrection.  This function should be called whenever the text being typed by the user changes. It should not be called for non-printing characters (arrow keys, delete, etc). If FCorrect returns true, then use the values in pwchTo, pcchTo, and pcchReplace to replace the text in the original buffer.

   // Check for AutoCorrection on the string passed in
   // pwchBuffer: text to check,  “This is teh “
   // ichBuffer: index of character just typed (12)
   // pwchTo: buffer to hold new text to replace original text, “the”
   // pcchTo: in: length of buffer pwchTo (100)
   //         out: length of replacement text, (3)
   // pcchReplace: out: number of characters to replace (3)
   BOOL FCorrect(LPCWSTR pwchBuffer, unsigned int ichBuffer,
         WCHAR *pwchTo, unsigned int *pcchTo, unsigned int *pcchReplace);

The call ShowDialog brings up the AutoCorrect dialog. Refer to the previous section for a description of the AutoCorrect main dialog.  HwndParent should be the main window of the application. The sub dialogs are called using existing code for the Exceptions command button on the dialog. You will not need extra code to make those dialogs available to the user.

   // Show the AutoCorrect dialog
   void ShowDialog(HWND hwndParent);

The AutoCorrectEntries Interface

The AutoCorrectEntries are the word-pairs that AutoCorrect searches for when replacing text. For example, “teh” and “the” are a common pair.

Use IMsocAutoCorrectEntries to get the AutoCorrectEntries interface. PiaceGetAutoCorrectEntries returns the interface to the AutoCorrectEntries.

   // Get the AutoCorrectEntries interface, NULL on failure
   IMsocAutoCorrectEntries *PiaceGetAutoCorrectEntries()

Once you have the interface, you can access and manipulate the list of entries.

   // Count:  Returns the number of AutoCorrectEntries
   int Count();
   
   // GetEntry:  Fills in the values of AutoCorrectEntries(i)
   // i: index of entry to get
   // pwchName: buffer to hold name of entry
   // cchName: length of pwchName buffer
   // pwchValue: buffer to hold value of entry
   // cchValue: length of pwchValue buffer
   HRESULT GetEntry(int i, WCHAR *pwchName, int cchName, WCHAR *pwchValue, 
      int cchValue);

   // Delete:  Removes entry i
   HRESULT Delete(int i);

   // AddEntry:  Creates a new entry 
   HRESULT AddEntry(WCHAR *pwchName, WCHAR *pwchValue);

Sample C Code

// Get the OfficeCompatible interface
IOfficeCompatible *pOC = CreateOfficeCompatible(L”SampleApp”, L”SampleAplication”)

if (pOC != NULL)
   {
   // Get the AutoCorrect interface
   IMsocAutoCorrect *pAutoCorrect = pOC->PiacGetAutoCorrect();

   if (pAutoCorrect == NULL)
      return();

   pAutoCorrect->ShowDialog(hwndMain);
   
   WCHAR rgOriginalText[200];
   wcscpy(rgOriginalText,L“Test of teh “);

   int ichText = wcslen(pOriginalText);
   WCHAR rgwchTo[100];
   unsigned int cchTo = sizeof(rgwchTo);
   unsigned int cchReplace;

   // If there is a correction to perform
   if (pAutoCorrect->FCorrect(rgOriginalText, ichText, rgwchTo, &cchTo, &cchReplace))
      {
      // copy the new text over the old text
      wcscpy(&rgOriginalText[ichText - cchReplace], rgwchTo);
      }
   }

Automation Interface

You can also access the AutoCorrect feature through the Office Compatible automation interfaces. Since the automation interfaces are based on the same code, they appear and function like their C counterparts, except they use VB syntax. 

As with the C model, you need to establish your application as an Office Compatible user. Use CreateObject to return an application object, using the following call.

   Set OC = CreateObject("OfficeCompatible.Application")

You will also need to initialize your object using the following call.

   OC.Init "OCBSamp", "Office Compatible Basic Sample App"

For more information about automation initialization, refer to the article “Application Initialization when Using the Office Compatible DLL”.

The AutoCorrect Object

Once you have created your application object, you can then access the AutoCorrect properties. Since in this case the parent and the application are the same thing, they return the same object.

      Application - returns the application object
      Parent - returns the application object

The following Boolean properties correspond to the checkboxes available on the AutoCorrect dialog. They can be used to get and set these values. Setting the Boolean value to true will check the checkbox; false will clear the checkbox. They are listed here in the order they appear on the dialog.

      TwoInitialCaps
      CapitalizeSentences
      CapitalizeNamesOfDays
      CapsLock
      ReplaceText
      AutoCorrectEntries - returns the AutoCorrectEntries collection

Methods available to the AutoCorrect object.

The method AutoCorrectText corresponds to the FCorrect call in the C interface.

      AutoCorrectText(String bstrText) - returns a non-blank string if correction has been made

The method ShowDialog brings up the AutoCorrect dialog. Refer to the section “Dialogs” for a description of the AutoCorrect main dialog. As with the C interface, the sub dialogs are called using existing code for the Exceptions command button on the dialog. You will not need extra code to make those dialogs available to the user.

      ShowDialog()

The following summarizes the properties and methods for the autocorrect automation interface.

   properties
      Application – returns the Application object
      Parent – returns the parent object
   methods
      ReplaceText – sets or returns the ‘replace text’ flag
      TwoInitialCapitols – sets or returns the ‘two initial capitols’ flag
      CapitolizeNamesOfDays – sets or returns the ‘capitolize names of days’ flag
      CapitolizeSentences – sets or returns the ‘capitolize sentences’ flag
      CapsLock – sets or returns the ‘caps lock’ check flag
      Correct([in]bstrCorrect, [in, opt] boolCheckNow)– perform an autocorrect operation on a given string and returns the corrected string
      ShowDialog – display the autocorrect dialog
      AutocorrectEntries – returns the auto correct entries object

The AutoCorrectEntries Collection

The AutoCorrectEntries are the word-pairs that AutoCorrect searches for when replacing text.  For example, “teh” and “the” are a common pair.

Use AutoCorrect.AutoCorrectEntries to get the AutoCorrectEntries collection.

   properties
      Application - returns the Application object
      Parent - returns the Autocorrect object
      Count - returns the number of entries
   methods
      Item([in]Index) - Returns AutoCorrectEntry Index
      Add([in]String Name, [in]String Value) - Adds a new entry
      Remove([in]Index) - Removes entry at Index

The AutoCorrectEntry Object

This object defines a single entry in the AutoCorrectEntries Collection.

AutoCorrectEntry
   properties
      Application - returns the Application object
      Parent - returns the Autocorrect object
      Name – set or return the name of the entry
      Value – set or return the value of the entry
   methods
      Delete - Delete this entry

Sample VB Code

   Set OC = CreateObject("OfficeCompatible.Application")
   If OC Is Nothing Then Exit Sub
    
   On Error Resume Next
   OC.Init "OCBSamp", "Office Compatible Basic Sample App"

   If (Err.Number <> 0) Then
      Set OC = Nothing
      On Error GoTo 0
      Exit Sub
   End If
        
   On Error GoTo 0

   Set AutoCorrect = OC.AutoCorrect
   If Not AutoCorrect Is Nothing Then
      AutoCorrect.ShowDialog

      strOriginal = “This is teh “

      strNew = AutoCorrect.Correct(strOriginal)
    If (strNew <> "") Then
        StrOriginal = strNew ‘ AutoCorrect occurred
    End If   End If