1
votes

I'm creating a VBA macro to create a new autocorrect entry from a text selection. The text is math text so often has subscripts or superscript is italic, etc. For example, assume that the target is "M1" where "1" is subscripted, then the autocorrect entry would be "m1".

If you do this manually, you select the target text formatted as desired, and in the AutoCorrect dialog check "Replace text as you type, Replaced: with: Formatted text.

However, I cannot find the AutoCorrect option property that corresponds to this function, so the result is always plain text.

I tried recording a macro (which in fact worked) and then checking all of the options that were captured by Word but none of actually did the job of preserving the formatting.

Here is the code:

Sub NewAutoCorrect()

    Dim strEntry As String

    strEntry = InputBox("Enter the AutoCorrect entry")

    AutoCorrect.Entries.Add Name:=strEntry, Value:=Selection

    With AutoCorrect
        .CorrectInitialCaps = True
        .CorrectSentenceCaps = True
        .CorrectDays = True
        .CorrectCapsLock = True
        .ReplaceText = True
        .ReplaceTextFromSpellingChecker = True
        .CorrectKeyboardSetting = False
        .DisplayAutoCorrectOptions = True
        .CorrectTableCells = True
    End With
End Sub
1

1 Answers

0
votes

Try:

AutoCorrect.Entries.AddRichText Name:=strEntry, Range:=Selection.Range