![]() |
|
#1
|
|||
|
|||
|
I am importing some old Word macros for use in Word 2013. One of them has the statement:
WordBasic.AutoCorrectNow which is no longer recognized in Visual Basic. This statement would evaluate the word to the left of the cursor position and if it matched an autocorrect template would do the autocorrection. How can I do Autocorrect in New Visual Basic? |
|
#2
|
||||
|
||||
|
The difficulty with creating a function to perform an autocorrect on a text is in determining what to autocorrect. Auto correct entries can be longer than a word and can include punctuation which throws out the potential word count. The only solution I can see would be to select the text you wish you check against the autocorrect list and then run the macro - something like
Code:
Sub ApplyAutoCorrect()
Dim i As Long
Dim orng As Range
Set orng = Selection.Range
orng.MoveStartWhile Chr(32)
orng.MoveEndWhile Chr(32), wdBackward
If Len(orng) > 0 Then
For i = 1 To Word.Application.AutoCorrect.Entries.Count
If orng.Text = Word.Application.AutoCorrect.Entries(i).Name Then
Word.Application.AutoCorrect.Entries(i).Apply orng
Exit For
End If
Next i
End If
lbl_Exit:
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#3
|
|||
|
|||
|
Thanks, I solved the problem of potential multiple words and various word terminators by just selecting text the size of each entry. Also "Option Compare Text" is needed to make it case-insensitive.
Option Compare Text Sub AutoCorrectNow() Dim orng As Range For Each Entry In Word.Application.AutoCorrect.Entries Set orng = Selection.Range orng.MoveStart wdCharacter, -Len(Entry.Name) If orng.Text = Entry.Name Then Entry.Apply orng Exit For End If Next End Sub |
|
#4
|
|||
|
|||
|
I had to add the following statement at the beginning.
Dim Entry as Object Word 2010 |
|
#5
|
|||
|
|||
|
When I run this, it seems to require selecting the text that should trigger AutoCorrect and then running the macro for that term. It does not work if I select a block of text and run it. Here is one that Hans Vogelar tweaked for me based on it. It is not perfect but covers everything in the attached sample document.
Code:
Sub AutoCorrectNow2()
' Graham Mayor Bob Sundquist, Hans Vogelar and Charles Kenyon 2022-06-10
' https://eileenslounge.com/viewtopic.php?f=26&t=38297&p=296123#p296123
' https://www.msofficeforums.com/word-vba/29148-how-replace-wordbasic-autocorrectnow-old-macro.html
' Runs autocorrect on selected text as if it were being retyped
'
'
Dim oWrd As range
Dim Entry As Object
Dim iCount As Long, i As Long
On Error Resume Next
Let iCount = Selection.Words.Count
For i = 1 To iCount
Set oWrd = Selection.Words(i)
For Each Entry In Word.Application.AutoCorrect.Entries
If oWrd.Text = Entry.Name Then
oWrd.Text = Entry.Value
Exit For
ElseIf oWrd.Text = Entry.Name & " " Then
oWrd.Text = Entry.Value & " "
Exit For
End If
Next Entry
Next i
Set oWrd = Nothing
Set Entry = Nothing
End Sub
This will only handle single-word entries. |
|
#6
|
|||
|
|||
|
Here is one that handles more including multi-word AC entries. It is slow but may be worth using.
Code:
Sub AutoCorrectBruteReplace2()
' https://answers.microsoft.com/en-us/msoffice/forum/all/macro-for-autocorrect/91f9bdb1-47ac-4cec-9842-5f1ee38bd7cf?page=1
' rianvillareal 2022-06-11 modified by Charles Kenyon with help by Hans Vogelar to use range and work with selected text
' It is slow. Handles multi-word entries. Does not handle fractions
' If text is selected, it operates only on selected text
' If nothing selected, it operates on entire document
'
Dim oEntry As Word.AutoCorrectEntry
Dim oRng As range
Set oRng = Selection.range
For Each oEntry In AutoCorrect.Entries
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = oEntry.Name
.Replacement.Text = oEntry.Value
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
|
|
| Tags |
| autocorrectnow |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Macro on Search and Replace | davidhuy | Word VBA | 1 | 12-19-2014 04:47 AM |
| Need Macro to Replace Text | rsrasc | Word VBA | 2 | 11-10-2014 06:26 PM |
| Windows 95 WordBasic (!!!) macro | NobodysPerfect | Word VBA | 4 | 08-25-2014 02:03 AM |
Find and Replace Macro
|
amparete13 | PowerPoint | 3 | 03-11-2014 05:29 AM |
Macro - replace with condition
|
ubns | Word VBA | 1 | 05-02-2012 12:52 AM |