Word VBA - Paste a word to Word Autocorrect dialog box

I

Irman Wardhana

I need help
Default behavior:
I select a word in a document then open Autocorrect dialog box. The word I select automatically appears in the 'With textbox' and the 'Replace textbox' is empty.

What I need is:
the word automatically appears in the 'Replace textbox' and the 'With textbox' is empty.

Is there a way to do that using Word vba macro, maybe by paste the word to the 'Replace textbox'?

I only manage to open the Autocorrect dialog box, using:

Dialogs(wdDialogToolsAutoCorrect).Show

then I don't know what to do.
 
S

Stefan Blom

When you select something in the document before opening the AutoCorrect
dialog box, Word assumes that you want to create a formatted AutoCorrect
entry. As far as I know, there is no way to add formatting to the word or
expression being replaced by AutoCorrect.

However, if you are saying that you want to work with AutoCorrect in code,
don't use the dialog box. Instead, work with the AutoCorrect object
directly. For example, to add an AutoCorrect entry:

Dim r As AutoCorrect
Set r = Application.AutoCorrect

r.Entries.Add "wtbr", "words to be replaced"

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Irman Wardhana" wrote in message

I need help
Default behavior:
I select a word in a document then open Autocorrect dialog box. The word I
select automatically appears in the 'With textbox' and the 'Replace textbox'
is empty.

What I need is:
the word automatically appears in the 'Replace textbox' and the 'With
textbox' is empty.

Is there a way to do that using Word vba macro, maybe by paste the word to
the 'Replace textbox'?

I only manage to open the Autocorrect dialog box, using:

Dialogs(wdDialogToolsAutoCorrect).Show

then I don't know what to do.
 
H

Harold Druss

Irman Wardhana said:
I need help
Default behavior:
I select a word in a document then open Autocorrect dialog box. The word I
select automatically appears in the 'With textbox' and the 'Replace
textbox' is empty.

What I need is:
the word automatically appears in the 'Replace textbox' and the 'With
textbox' is empty.

Is there a way to do that using Word vba macro, maybe by paste the word to
the 'Replace textbox'?

I only manage to open the Autocorrect dialog box, using:

Dialogs(wdDialogToolsAutoCorrect).Show

then I don't know what to do.

All followers of Allen Wyatt's word tips will be interesed in the answer!
 
I

Irman Wardhana

Hi, Stefan Blom
Thanks for the advice.
I do not intend to add formatting word to be replaced by AutoCorrect. What I mean is this:
I created a procedure "useAutoCorrect" and it was assigned to F1 as shortcut.

Sub useAutocorrect()
Dim sKata As String
Dim bExist As Boolean
Dim Rng As Range
Dim acEntry As AutoCorrectEntry

Set Rng = Selection.Range
Rng.MoveStart Unit:=wdWord, Count:=-1
bExist = False
sKata = RTrim(Rng.Text)
For Each acEntry In AutoCorrect.Entries
If acEntry.Name = sKata Then
AutoCorrect.Entries(sKata).Apply Range:=Rng
bExist = True
End If
Next acEntry
If bExist = True Then Exit Sub
Dialogs(wdDialogToolsAutoCorrect).Show
'Selection.InsertAfter sKata

End Sub


This procedure will trigger the Autocorrect. So let's say there is an entry "worldp"> "world peace" in the Autocorrect entry. When I typed "worldp" then press F1 then it will change to "world peace".
But if there is no entry "worldp" in the Autocorrect entry, then I want the Autocorrect dialog box to be opened (this is what I can do so far) but then I want "worldp" appears automatically in the 'Replace textbox'. I still can't figure it out how to do this. I tried the last line of code

Selection.InsertAfter sKata

but it inserts "worldp" in the document instead of in the 'Replace textbox'.

Related to the problem, I have another question. Is there any "autocorrect event" in Word? If there is "autocorrect event", I want to 'intercept' it with my procedure above.
 
S

Stefan Blom

I'm not sure why you want the dialog box to be displayed. If the AutoCorrect
entry doesn't exist, just create by accessing the AutoCorrects collection
directly.

As far as I know, there is no event relating to AutoCorrect insertion.
However, to find out for certain, you may want to ask in Office programming
and customization forum at Answers; you'll find more posters there. Go to
http://answers.microsoft.com/en-us/office/forum/customize?tab=all.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Irman Wardhana" wrote in message
Hi, Stefan Blom
Thanks for the advice.
I do not intend to add formatting word to be replaced by AutoCorrect. What I
mean is this:
I created a procedure "useAutoCorrect" and it was assigned to F1 as
shortcut.

Sub useAutocorrect()
Dim sKata As String
Dim bExist As Boolean
Dim Rng As Range
Dim acEntry As AutoCorrectEntry

Set Rng = Selection.Range
Rng.MoveStart Unit:=wdWord, Count:=-1
bExist = False
sKata = RTrim(Rng.Text)
For Each acEntry In AutoCorrect.Entries
If acEntry.Name = sKata Then
AutoCorrect.Entries(sKata).Apply Range:=Rng
bExist = True
End If
Next acEntry
If bExist = True Then Exit Sub
Dialogs(wdDialogToolsAutoCorrect).Show
'Selection.InsertAfter sKata

End Sub


This procedure will trigger the Autocorrect. So let's say there is an entry
"worldp"> "world peace" in the Autocorrect entry. When I typed "worldp" then
press F1 then it will change to "world peace".
But if there is no entry "worldp" in the Autocorrect entry, then I want the
Autocorrect dialog box to be opened (this is what I can do so far) but then
I want "worldp" appears automatically in the 'Replace textbox'. I still
can't figure it out how to do this. I tried the last line of code

Selection.InsertAfter sKata

but it inserts "worldp" in the document instead of in the 'Replace textbox'.

Related to the problem, I have another question. Is there any "autocorrect
event" in Word? If there is "autocorrect event", I want to 'intercept' it
with my procedure above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top