PDA

View Full Version : Solved: find a word together with the words coming after or before of it



ablajan
07-03-2005, 01:03 PM
hello! everybody:
I encountered another problem.
I want to find a word together with the words coming after or before of this word.
For example.
I am learning visual basic programming in my university.
I want to find 'basic'the first time .next
find 'visual basic programming '
next time 'learning visual basic programming in '
...........
I tried the following code:but I only find the following words of 'basic'
could not find together the words coming before and after.
Dim rngReplace As Word.Range
Dim rngFound As Word.Range
Set rngReplace = ActiveDocument.Content
With rngReplace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<basic>&<*>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
' Find all in the document
'Do While .Execute
'but I only find the following words of 'basic'could not find together the words 'coming before and after.

I need your kindly help.

fumei
07-04-2005, 07:23 AM
1. This does sound like a school project.

2. please try to break up lines within the vba tags. Long lines stretch out so scrolling left right is required.

3. work through the logic: You have a range object set. In that range you look for "basic".

That is all you are doing. How COULD it find the previous word, or the following word? You never tell it to.

Another suggestion, or perhaps a hint, is to remind you that you are using a Range object here. You are not selecting anything.

Also, you have a variable rngFound. In your posted code, this variable is never used. It may help a wee bit if you posted complete code. Or is this variable not used?

ablajan
07-04-2005, 11:43 AM
Thank you for your suggestion.
My code is (in here I only find the basic programming)
Selection.Find.ClearFormatting
With Selection.Find
Selection.Find.ClearFormatting
With Selection.Find
.Text = "basic <*>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
End SubSelection.Find.Execute
I want to find the one or two words coming before or after of "basic" by one click at same time..
could you help me

fumei
07-05-2005, 12:31 PM
Sorry, but this is not good enough. The code you posted is not much different from your original post.


My code is (in here I only find the basic programming)Yes, I can see that. Shrug...your code still does not have any instruction to look for other words.

You must step it out logically. In your original post you mention a search, then a "next". You must step things out.

1. search for "basic"
2. ????

What is it? Perform a new search? Are you going to loop through all instances of "basic", and ALWAYS do a new search for the word previously, and the word following? This would be two searches.

You could take the first search ("basic"), select the result, expand the selection to include the word prior, AND the word following. then you would have the words.

Are you:

1. searching for "basic"
2. searching for "basic" PLUS checking the previous word; THEN checking the following,

What do you want to do with a successful search. Suppose you have "basic programming", but NOT "visual basic programming" - what are you going to do. The previous word is not a search hit, but the following is. Now what?

This can be done. As I suggested, study the Selection and Range objects. You are going to have to take the found word - "basic" , make a range of the previous word , check it to see if it is "visual", reset the range to become the following word, check it to see if it is "programming".

Find in Word is very effective, but a little tricky. Please try and make it work for you. I have given some hints here. I don't want to just post a solution. I will help if you post some code that you are having problems with. Right now, your code does not do anything other than search for "basic". Please look up help on the Range object.

Hint: You can use .MoveStart and MoveEnd on range objects.
Hint: Look up creating objects in help. You should declare a Range object, set it for the found "basic", then using MoveStart and End shift it around to check the preceding and following words. make sure you destroythe object each time you find a "basic"
Hint: look up loops in help.
Hint: Do While .Execute(FindText:= "basic", Forward:=True, _
Format:=True) = True

You can check the content with the range.Text property.

ablajan
07-06-2005, 08:47 AM
Thank you Gerry.
I used this code

Selection.Find.ClearFormatting
With Selection.Find
Selection.Find.ClearFormatting
With Selection.Find
.Text = "basic"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
if selection.found =true thenSelection.MoveRight Unit:=wdCharacter, count:=1
end if
End Sub

Selection.Find.Execute
orSelection.MoveRight Unit:=wdCharacter, count:=2
Selection.Moveleft Unit:=wdCharacter, count:=2 or etc seperately.,Because I am a beginner I could not find the solution.I want to find 'basic' for the first click .next click ,at the same time,
find 'visual basic programming '
and next click find 'learning visual basic programming in '
...........
your help highly appreciated.

fumei
07-06-2005, 11:28 PM
OK. You are going to have to explain.

WHAT click????? What is clicking?

And please, learn how to post properly here. Use the VBA tags. The code posted above is not an improvement.

Further, what are you DOING when, and if you find this? Do you want to select the words?

What are you trying to do? I understand that you want to find a word ("basic") and see if the previous word is something else ("visual")...but, big deal. So what? WHAT is the purpose of this?

You also have not looked into the Range object.

If you want to use VBA (or VB for that matter) with Word, this is a must.

I have a complete working solution. Please read my posts carefully. I have given some serious clues.

ablajan
07-07-2005, 01:49 PM
hi!
tell your truth, I did not know about the range.

my objection is find any word together with the words around it.
I used selection.find and only can find "basic programming".
could not find the word before "basic".

fumei
07-08-2005, 08:47 AM
I understand what your objective is. There is a way to do - at least as far as you express it - what you want.

However;

You have not answered the question - What ARE you trying to do?

You have not answered the question - WHAT clicking?

You have not tried using MoveStart or MoveEnd - you used MoveRight etc etc. Maybe if you tried using my suggestions you could at least find the text you want. I can find words before, and after, any word I want. That is step 1. But again, so what, big deal. Say there are 17 instances of "visual basic programming" You find the first one....NOW WHAT? You have a lovely message box stating "Yes, I found one", then go to the next and display a messagebox stating "I found one"...for 17 times?

WHAT ARE YOU TRYING TO DO?
WHAT ARE YOU "CLICKING"?

I apologize for the full caps "shouting". I am finding this a little frustrating. While I NOT going to post the code, here is how you do it. At least to determine IF the previous word is "visual" and the following word is "programming". As you seem to refuse to state what you actually want to do, this is as far as it can go.
Two Boolean variables: Before, and After

Search word = "basic"
Do While the (execution of the search = True) = True
[comment]the execution direction is Forward and does execute. This makes the Found (ie "basic") SELECTED.[end comment]

Make a range object = Selection (which is now "basic").

Collapse the Range object to the Start of itself.
[comment] this makes the range a point just before "basic"[end comment]

MOVE the start of the range one word previous
[comment]this make the range object the word previous to "basic"[end comment]

Check to see if range text = "visual"; if it is make Before = True
[comment]if you want to retain this word, make a temporary bookmark[end comment]

Collapse the Range to End.
[comment]this makes the Range a point just before "basic" again.[end comment]

MOVE the Range ONE word forward.
[comment]this makes the range now the following word. NOTE: as all this is using a RANGE object, remember the Selection is still "basic".[end comment]

Check if range text is "programming"; if it is After = True
[comment]if you want to retain this word, make a temporary bookmark[end comment]

Check Before and After. If both are true, then YUP, this instance of the word "basic" has "visual" before and "programming" after it.

....then....I don't know what you want to do.....

Loop
[comment]that is, go to the next search function of looking for "basic"[end comment]

Again, as you have not explained "click", have not explained what you want to actual do if this combination of words is found, I think the best I can do now is wish you good luck.

fumei
07-08-2005, 08:56 AM
As added comments to anyone else reading....

If the requirements (and this is not stated, but say it is) are to bookmark each "visual basic programming" you could use the temporary bookmarks to define another bookmark to encompass the entire phrase.

Also, if you just want to find "visual basic programming"...then, gosh, do a search for "visual basic programming". It is unclear to me the requirement for BUILDING a string to be found. Why not just look for that string? Why does there have to be logic that makes a check of separate words?

ablajan
07-08-2005, 10:01 AM
thank you FOR your help and your helpful suggestion.
my goal is list the words one by one, two by two at one time around ANY word that I want to find.
click means command.I'll try to do my best.

ablajan
07-10-2005, 10:16 AM
hi! GERRY:
Your wrote my intention very clearly on your post.IT IS A WORD ANALYSING PROGRAM.
could you help me write it in program?
in my following examples examples "basic" is the target word.
example:I am learning visual basic programming in my university
my intention is list the following:
'basic'
visual basic programming
learning visual basic programming in
am learning visual basic programming in my
...........................................................
your consideration and help highly appreciated.

TonyJollans
07-10-2005, 08:35 PM
Hi ablajan.

Is it really critical that you find these ever-expanding strings in the order you keep showing?

With "Use Wildcards" checked, this Find string ...

[! ]{1,} basic [! ]{1,}

... will locate instances of the word "basic" in context. It would need some adjustment to cope with punctuation if that was wanted but could be easily extended to give, say, two words either side of the keyword. In a loop you could end up with a list of each occurrence with one word each side which I would assume would allow you to do your analysis.

fumei
07-10-2005, 08:43 PM
You keep repeating, and repeating, and repeating, and repeating....yes, I know you are trying to find "basic", "visual" and programming. You keep repeating this in your posts. I am sorry, do I seem a little dense? Do you think if you just keep repeating this that, like magic, your answer will appear.

Look, you are learning, I understand that...so I understand that it can be difficult. However, learn! I have given you real solid things to try and work on.

You are not, it seems, trying very hard at all. You keep repeating your request. You have not posted serious questions that would come about by some one actually trying hard.

You do not answer questions. I have asked a couple of times about what you want to DO when you find these words - and it does not matter what words. You can EASILY have whatever input words you want. You have not answered why you are doing this in the first place. Why not just search for "visual basic programming"? I asked because it is relevant. You do not answer.

It seems to me that you are looking for a quick answer and no real learning. You keeping asking for help but you offer little work.

No I will not "help" you write it. Why would I? I have already written it myself. It is finished. Done. Except I found another use. I have it hunt for a word, then check if the FIRST word of the sentence is a particular other word, and if it is write a log file of the current page number, while setting a bookmark for the paragraph - for easy retrieval.

Good luck. The answer is not hard. Every thing you need, conceptually, is there.

Anne Troy
07-10-2005, 08:59 PM
I just thought I'd jump in here and say something to ablajan, and hope that I don't offend. If you want someone to WRITE your code for you, you might want to go to www.rentacoder.com (http://www.rentacoder.com). Someone there will likely write it for $5.

fumei
07-10-2005, 09:44 PM
Wow, that just cracked me up. Thanks princess.

MOS MASTER
07-11-2005, 09:09 AM
I just thought I'd jump in here and say something to ablajan, and hope that I don't offend. If you want someone to WRITE your code for you, you might want to go to www.rentacoder.com (http://www.rentacoder.com/). Someone there will likely write it for $5.

Wow...$5...that's mucho cheap.:eek: ..Well you have to a whole lot better over here. :rofl:

fumei
07-11-2005, 12:40 PM
I don't think you get much for $5.

Anne Troy
07-11-2005, 12:42 PM
Hm. Shall we put it to the test?

Any wagers???

:)

fumei
07-11-2005, 12:43 PM
You know......hmmmmmm, I am VERY tempted. Just to see what comes out of it. Hmmmmmmm.

Anne Troy
07-11-2005, 12:47 PM
I've got an account over there. You write it, I'll put up the $5.

MOS MASTER
07-11-2005, 12:51 PM
You know......hmmmmmm, I am VERY tempted. Just to see what comes out of it. Hmmmmmmm.

Go Gerry...Go Ann...everybody Woot! :hi:

I'm temped as well but knowing Ann she's gonna kick my butt! :rofl:

Get here buddy. :whistle: