PDA

View Full Version : Simple Find and Replace



caz1805
04-04-2006, 12:57 PM
Hi i am very new to VBA, but been trying to learn over the last couple of weeks. Made a simple find and replace program that replaces 'Hello' with 'Bonjour'. Was just wondering was there any way that this replacement could occur automatically so as i type hello it is replaced after the space bar is pressed. I have managed to do this in Visual Basic, but struggling to find anything within VBA. Any help would be excellent.

Caz

Heres the code.

Sub Test()
With ActiveDocument.Content.Find
.Text = "Hello"
With .Replacement
.Text = "Bonjour"
End With
.Execute Format:=True, Replace:=wdReplaceAll
End With
End Sub

fumei
04-04-2006, 02:15 PM
1. Are you saying that whenever the spacebar is pressed you want code that will check the previous word to see if it is "hello", and if it is, change it to "bonjour"?

2. How did you do this in VB????

caz1805
04-04-2006, 02:24 PM
Yeh so when the spacebar is pressed I want code that will check the previous word to see if it is "hello", and if it is, change it to "bonjour". So exactly what you said.

Here is the code in visual basic.


Private Sub Text2_KeyUp(KeyCode As Integer, Shift As Integer)
Dim Word As String
Dim Prts() As String
Dim NewWord As String

If KeyCode = vbKeySpace Then
Prts = Split(Text2.Text, " ")

Word = Prts(UBound(Prts) - 1)
Select Case LCase(Word)
Case "hello": NewWord = "Bonjour"
Case "bye": NewWord = "Au revior"
Case Else: NewWord = Word
End Select

Text1.Text = Replace(Text2.Text, Word, NewWord)
Text1.SelStart = Len(Text2.Text)
End If
End Sub

fumei
04-04-2006, 03:02 PM
Well, OK....except, if i understand correctly, this code is running on a form. At least I think it is, since you are using Text1.SelStart. There is no such beast in a DOCUMENT.

So sure, it is not so crazy doing a check on EVERY keystroke - as this does - when you have a fairly limited amount of text.

But in a document???? Sorry, but that is nuts. EVERY keystroke, and check if it is a spacebar, and then if it is, check the last word to see if it is "hello"? In a real document? Hmmmmmmmmm.

You can do the exact same thing for a VBA form, but sheeeesh, not in a real document. Doing that "live" in a document just does not seem practical to me. What would fire the code? It is fine on a form, where you have such events, but normally in a document (as ongoing typing/editing) you do not.

fumei
04-04-2006, 03:04 PM
What I am saying is, your Find and Replace works...so what is wrong with just doing that?

caz1805
04-04-2006, 03:08 PM
Okay thanks for all the help.

So what you are saying is that if i wanted to check every word after i press space, is too much and not viable at all in a DOCUMENT.

But if i tried to do this as a form, it might be able to work? In the end I was going to try and get the program to check if the word is in a database (Access) and then replace the word with a another word in the database. If that makes sense?!?

Thanks for your help.

Caz

caz1805
04-04-2006, 03:13 PM
Because I wanted to know and try and complete this within Word as it will provide more capabilities and be easier to use. I think.

Thanks for all your help gerry.
Caz

fumei
04-04-2006, 06:14 PM
No, what I was saying is that do a check of a keystroke in an environment (a userform) is perhaps reasonable because there will not be all that many keystrokes. However, in a document there could be a LOT of keystrokes.

Further, on a userform there a number of events that can be used to fire your instructions (the keystroke checking). There are NOT such events in a document. Look at it this way.

k-e-y-s-t-r-o-k-e-spacebar.

If you are typing in a textbox - ON A USERFORM - you have the opportunity to use a KeyPress event. Fine. You can check each keypressUp for a spacebar, and move back to check the previous word.

There is no available keypress event while typing in a document. Therefore WHAT would cause code to do the checking? Simply pressing the spacebar will not. So you can say you did this in VB, but this is not totally accurate. You did this on a VB form. You can do the same on a VBA form - they are essentially the same thing. But you did not, do this in a Word document using VB, nor can you in VBA.

So no, you can not do this AS a form (as you put it), but you can do this ON a form.

And no, you can not do this for every word you type in a document. Because, first of all you are not doing this for every word...you are doing it for every single keystroke you EVER make.

Does that make sense to you?

fumei
04-04-2006, 06:19 PM
Uh....why do you not just use AutoCorrect? You could make an AutoCorrect entry as hello, and make the correction bonjour. That way it WOULD make the change automatically.

However, of course you would never be able to type in hello...EVER.

lucas
04-04-2006, 09:01 PM
Looks to me like the simple find and replace macro that Caz started with for the words hello to bonjour would be the most efficient way to handle this. If you put the macro in your normal.dot and assign a button to it you can just finish your letter and hit the button, voila

fumei
04-05-2006, 01:08 PM
Well yes, of course. It is MUCH easier to do the Find / Replace afterwards. As i said, doing it on the fly is nuts.

TonyJollans
04-05-2006, 01:22 PM
As I'm sure Gerry has already said you cannot do this on the fly - nuts or not.

But, also as Gerry has said, AutoCorrect is the way to do it - and autocorrects can be undone with a simple Ctrl+Z immediately or, if you have a new enough version of Word, via the Autocorrect Options button any time during the same edit session.

lucas
04-05-2006, 02:29 PM
Makes sense to me. Sometimes we just overcomplicate things.

fumei
04-06-2006, 10:11 PM
:yes