Word VBA Code

J

Jon McDermott

I am looking for a way to check each word that has been typed in by the user
in a word document.

i need to check after each word has been typed and compare it to a list of
words for changing. This would work very similarly to the autocorrect
function.

I have tried running a macro to visualise the VB code it generates but it
does not capture by single words only sentences ( as far as i can see).

Can anyone elaborate on the precedure for this ?

many thanks
 
J

Jean-Guy Marcil

Jon McDermott said:
I am looking for a way to check each word that has been typed in by the user
in a word document.

i need to check after each word has been typed and compare it to a list of
words for changing. This would work very similarly to the autocorrect
function.

I have tried running a macro to visualise the VB code it generates but it
does not capture by single words only sentences ( as far as i can see).

Can anyone elaborate on the precedure for this ?

Can you show us the "Sentence" code you have?

Are you trying to do this in real time?
 
J

Jon McDermott

Jean-Guy Marcil said:
Can you show us the "Sentence" code you have?

Are you trying to do this in real time?

when you say the "sentence" code , do you mean for word or from the macro ?
There is no particular sentence that i need to parse in word.

Yes i need to do this in real time preferably - if this is not possible then
i may have look at creating a toolbar that will allow whatever text has been
typed so far gets parsed and all substitions made at that point.

let me elaborate.

user types the word "granny" into their document, my code sees the word
"granny" has been typed and replaces it with "Grandmother" (the same as i
could do with autocorrect. (but i do not want use autocorrect)).

therefore the code must be 'aware' of each word that has been entered
(autocorrect can do it but finding the method it uses is the problem i face)

here is what the macro i recorded returned :-

Sub test()
'
' test Macro
' checking user input
'
Selection.TypeText Text:="this is s test."
End Sub

as you can see it has stored the whole line upto the point i hit return.

a line more like

Selection.TypeText Word

would more suit my purpose but i dont know if this exists or not ?

Please bear with me if this reads a little vaguely as i have not coded for a
few years and am just trying to rediscover the array of changes that have
been implemented sinced then.
 
F

fumei via OfficeKB.com

"as you can see it has stored the whole line upto the point i hit return"

Stored? No, it has not stored anything.

Selection.TypeText Text:="this is s test."

is an instruction. Add the text "this is a test" at the Selection. Nothing
is stored.

First of all, there is no "word" in Word. There is no word object. There
are only Ranges.

Second of all, and this is an extrapolation of the first, the only way you
could do this "real-time" is to check after every character, and when you
check and find a space, THEN you would have to check back to find the
previous space, THEN grab the range in between and check that range.text.

Real-time analysis of user entered text is neither easy, nor efficient.
Think of some other way to do your corrections.

Yes, AutoCorrect does a good job, but the method is NOT exposed to VBA. You
can use VBA to add entries - and this may be an option for you. Obviously
you have some sort of logic, like granny to Grandmother. Why not simply add
them as autocorrect entries? You can do a big whack of them with VBA, using
an array.

But again, the method of detecting single "words" used by AutoCorrect is NOT
exposed to VBA. You would have to do the hard way (checking each character...
), and it is a royal pain in the butt, and sucks up resources as your code
would have to running all the time.

Jon said:
[quoted text clipped - 11 lines]
Are you trying to do this in real time?

when you say the "sentence" code , do you mean for word or from the macro ?
There is no particular sentence that i need to parse in word.

Yes i need to do this in real time preferably - if this is not possible then
i may have look at creating a toolbar that will allow whatever text has been
typed so far gets parsed and all substitions made at that point.

let me elaborate.

user types the word "granny" into their document, my code sees the word
"granny" has been typed and replaces it with "Grandmother" (the same as i
could do with autocorrect. (but i do not want use autocorrect)).

therefore the code must be 'aware' of each word that has been entered
(autocorrect can do it but finding the method it uses is the problem i face)

here is what the macro i recorded returned :-

Sub test()
'
' test Macro
' checking user input
'
Selection.TypeText Text:="this is s test."
End Sub

as you can see it has stored the whole line upto the point i hit return.

a line more like

Selection.TypeText Word

would more suit my purpose but i dont know if this exists or not ?

Please bear with me if this reads a little vaguely as i have not coded for a
few years and am just trying to rediscover the array of changes that have
been implemented sinced then.
 
G

Greg Maxey

Jon,

First I share fumei views and the difficulty and inefficieny of your
approach.

However, you could perhaps get close using a macro that executes on the
spacebar.

Something like this:

Sub SpaceBarMacro()
Dim oRng As Word.Range
Dim bRngProcess As Boolean
Dim i As Long
bRngProcess = True
Selection.TypeText " "
Set oRng = Selection.Range
oRng.Start = Selection.Sentences(1).Start
Select Case oRng.Words.Last
Case Is = "granny "
oRng.Words.Last = "grandmother "
i = 1
Case Is = "bubba "
oRng.Words.Last = "a southern gentleman "
i = 3
Case Else
bRngProcess = False
End Select
If bRngProcess Then
oRng.MoveEnd wdWord, i
oRng.Collapse wdCollapseEnd
oRng.Select
End If
End Sub

And then assign that macro to the spacebar with something like this:

Sub SetKeyBinding()
CustomizationContext = ThisDocument
KeyBindings.Add KeyCode:=BuildKeyCode(Arg1:=wdKeySpacebar),
KeyCategory:=wdKeyCategoryCommand, _
Command:="Module1.SpaceBarMacro"
End Sub


Jon said:
when you say the "sentence" code , do you mean for word or from the
macro ? There is no particular sentence that i need to parse in word.

Yes i need to do this in real time preferably - if this is not
possible then i may have look at creating a toolbar that will allow
whatever text has been typed so far gets parsed and all substitions
made at that point.

let me elaborate.

user types the word "granny" into their document, my code sees the
word "granny" has been typed and replaces it with "Grandmother" (the
same as i could do with autocorrect. (but i do not want use
autocorrect)).

therefore the code must be 'aware' of each word that has been entered
(autocorrect can do it but finding the method it uses is the problem
i face)

here is what the macro i recorded returned :-

Sub test()
'
' test Macro
' checking user input
'
Selection.TypeText Text:="this is s test."
End Sub

as you can see it has stored the whole line upto the point i hit
return.

a line more like

Selection.TypeText Word

would more suit my purpose but i dont know if this exists or not ?

Please bear with me if this reads a little vaguely as i have not
coded for a few years and am just trying to rediscover the array of
changes that have been implemented sinced then.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 

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