PDA

View Full Version : Mark words from wordlist in text



premois
05-07-2008, 11:45 AM
I found this macro and my problem is that when I have for example "abc " or " abc" in the wordlist it's searching and replacing also the abc in abcdef but I want it to replace the abc only when a actual space comes before or after the word. Please excuse my english and thanks for help.


Sub CompareWordList()
Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim wrdRef As Object

sCheckDoc = "c:\checklist.doc"
Set docCurrent = Selection.Document
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate

With Selection.Words(1)
End With

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.ColorIndex = wdRed
.Replacement.Text = "^&"
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

For Each wrdRef In docRef.Words
If Asc(Left(wrdRef, 1)) > 32 Then
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdRef

docRef.Close
docCurrent.Activate
End Sub

fumei
05-07-2008, 02:11 PM
"I want it to replace the abc only when a actual space comes before or after the word"

Except....

" abcdef" DOES have an actual space before. So it is functioning exactly as you say you want it to.

premois
05-07-2008, 02:24 PM
I gave two examples "abc " or " abc" in case of "abc " it's wrong. Also if you have xyzabcdef I don't want it to be marked. And an exeption of your example would be if it's the first word in the text then I don't want it to be marked either when the list contains " abc".


Or in other words exactly the chars that are in one row of the list should be marked in the text I but like the macro is now it seems to cut off the spaces. Maybe there is a method you can use instead of .words? Maybe .exactwords or something?

premois
05-10-2008, 09:10 AM
Is it not possible?

fumei
05-11-2008, 06:07 AM
You have to be more specific.

What, precisely, are you searching with/for?

If you search for "abc", then ALL instances of abc - whether it is beginning, middle, or end of anything - will be found.

You have the code, but I have NO idea what the "words" are in your file that you are using for reference.

So.....

Also, it would be much better to use range, rather than Selection.

You say you gave two examples, but would not "abc " be a different pass of the loop from " abc"? It is not like it is checking both at the same time.

I have absolutely no doubt that whatevr you want is possible. It may take more processing though. AND more specific "words" in your reference file.

BTW, what is with:


With Selection.Words(1)
End With


This With does not do anything.

Do the words in the reference file have spaces, or not? You want to look up using LTrim, and RTrim..or just Trim, within your logic. As the issue seems to spaces.

premois
05-14-2008, 05:02 PM
Oh that part you quoted was from me, I was just playing around I don't have any experience with VBA and forgot to remove it. I'm very sorry but I also have no idea what "selection" I should replace.
OK I try to explain to you what I need it for maybe then you'll understand.

I configured a spam filter in my rss feed tool to filter out articles I don't want.
I did that completely manually. That means I searched words or parts of them that wouldn't be in the articles I want to read. For example "episode", "epileptic" and "epic" wouldn't be in the articles I want to read. They all begin with "ep" So I put " ep" in my filter list. With "ep " they wouldn't get filtered out and "ep" would filter out all articles that contain "ep" like fo example every article that contains the word "keep" and so on.

The problem is, when I make a misstake, the rss feed tool doesn't offer the functionality to see by which word an article got filtered and it is very tedious to search for a problematic word manually. Imagine my text here would be one of the articles and you would want to read it, but it was filterd by a single one out of a list of 200 words. Now you need to find this word so a text like this doesn't get filtered again.
So I use this macro to find the problematic word when that happens it doesn't work like the filter of the rss tool though so it markes a lot of positions in the text because the macro doesn't pay attention to the spaces. That's why I ed some of the words in this text to give you an impression. You get some [B]"hot spots" cause ALL "ep" or "plain" in case of " plain " get marked so you only need to look at these but that is still uncomfortable. I'm sorry for the long text and thank you again. I hope you can tell me now what I can modify to make it work.

premois
05-15-2008, 11:55 PM
So it is still not clear ??? (_ = space)
List:
sd_
_abc
as!

Wrong result produced by macro now:
Abcde abcde sdabc arew abeas!

Result I need please:
Abcde abcde sdabc arew abeas!

fumei
05-16-2008, 11:37 AM
This is totatlly, and completely, a matter of logic...not code. If you determine the logic, then the coding may be tedious, but it is not difficult. So, work out the logic.

Wrong result produced by macro now:
Abcde abcde sdabc arew abeas!

Result I need please:
Abcde abcde sdabc arew abeas!

If you can spell out, PRECISELY, what is the logic to get that result, I will help you get the code to do it.

Attached is a demo that does what you describe in your last post - just to show you it can be done. I made:

Result I need please:
Abcde abcde sdabc arew abeas!

be Bold and Red, to make it very clear.

The document opens with a userform displayed. Just click "Run Demo".

premois
05-16-2008, 04:16 PM
I opened the document and I see the text but nothing else happens.
So logic? Well that's difficult when I can't use my own language but I try.

Macro:
Look at the first line of the word list and read every char till you are at the end of the line.
Then find exactly this array of chars in the text and mark it. Then go to next line and do it again and so on. For example if the first line in the wordlist was " 25j kq h1 "?$ qww2!!4" then I want it to mark exactly this but it marks only "25j"...

fumei
05-18-2008, 01:19 PM
Do you have High security? The userform should display on document open. If not, then execute the userform from the VBE.

I have no idea why you woukld only get "25j".

In the demo attached, I made it look for " 25j kq h1 "?$ qww2!!4"

and it finds them, and makes them red and bold.

I have no problem with finding " 25j kq h1 "?$ qww2!!4"

premois
05-18-2008, 07:00 PM
This one worked. And now? I don't know how it is supposed to solve my problem? Do you want money or what do you want for a working macro?

fumei
05-19-2008, 03:08 PM
I want you to work out the macro. I have stated what the issue is. You must spec out precisely, exactly, what it is you want it to do.

You state "this one worked". Fine...that is good. But what is different from what I did, and what you did?

I have made two documents, using your two examples, and made them BOTH work. So, you know it can be done. And it can be done.

It is done by writing out EXACTLY, PRECISELY, what the logic will be.

I will say it again. If you spec out your logic well, I will help you get your code.

If you are getting:

Wrong result produced by macro now:
Abcde abcde sdabc arew abeas!

Result I need please:
Abcde abcde sdabc arew abeas!

There is one reason, and one reason only. Your logic is not fully thought out. It is NOT a code issue, it is a logic issue.

If only get "25j" when you are looking for " 25j kq h1 "?$ qww2!!4", then is only ONE reason...the logic is not fully thought out.

I will give a hint though:

"Look at the first line of the word list and read every char till you are at the end of the line."

read every char till you are at the end of the line.

NOT good logic.

Here is another hint. Are the "lines " terminated by paragraph marks?

I hate to tell you this, as I know it is frustrating and you have put (probably) some time into this...but it is not actually all that difficult.

Here is another hint. In your example where it is NOT doing what you want:

Wrong result produced by macro now:
Abcde abcde sdabc arew abeas!

If you can answer (and it IS a logic question) why it produced the "wrong" result, then voila! That will point to the right answer.

BTW: are you stepping through your code? That will also demonstrate quite a lot.

premois
05-20-2008, 03:13 AM
Is it possible that you get me wrong? Like I said at the very beginning, "I found this marcro...", I've never written it myself and I don't want to learn VBA or its logic since I never needed it before in my life and I'm probably never going to need it again any time soon.
As I can see you wrote two macros (?) in no time so please PLEASE would you just give me a working code so I can move on....???

fumei
05-20-2008, 07:36 AM
BTW: Asc(Left(wrdRef, 1)) > 32

is redundant. Asc() only takes the first character, so there is no need for Left(wrdRef, 1)

If Asc(Left(wrdRef, 1)) > 32 Then

If Asc(wrdRef) > 32 Then

are exactly the same.

Could you please post your existing code that DOES give (in particular) the "error" regarding

sdabc

If your input search string is "sd " - sd, space - then " sdabc" should NOT return a Found, as it is NOT "sd ". Further, if the input search string is " abc" - space, abc - then sdabc should also NOT return a Found.

If you are saying it does find and action it, then again, you are writing the logic incorrectly, OR your input strings are incorrect.

Word does (pretty much) exactly what it is told to do. You have to learn to tell the right things. It can not read your mind.

If I use "sd " (sd, space), I never, EVER, get a hit on " sdabc", as it does not contain sd, space.

So if it IS getting it....your input string is not "sd ". Which may very well be your logic problem.

I would like to help, but every single example you have posted I can make work. So, simply put, I have no problem. Can you post your current code? Plus a sample source file (your list) and a sample document?

fumei
05-20-2008, 07:54 AM
No, sorry. This is not a Help Desk, and you are not paying me to do your work for you. Perhaps someone else may post a solution code for you, but I will not.

I will help you, but no, I am not going to just hand you something because you asked for it.

I perhaps would IF I knew what you are doing, and what the rules of your logic are. But you are not telling me what those rules are, and I have been asking.

I could not do a solution, anyway, without those rules. This is what I have been trying to tell you.

What are those rules?

You are looking for (for example), " sd" - space, sd.

OK, then you can do that. And it will find that. Now, here is a logic question.

IF you find " sd" (space, sd), let's say you have:

A) " sdabc" - space, sd, other characters
B) " sd " - space, sd, space

In BOTH cases, the search string (" sd") is found.

Do you want A to do something, but NOT B?
Do you want A to do something AND B?
Do you want not A, but B?

This is logic, not code.

In which case, you would add logic:

IF " sd" found, check the next character, and if it is not a space, forget it. Eg. " sdabc". Do nothing.

So, the logic is, search, and if found, check something else...in this case, the next character.

OR, you could simply have the search string be " sd " - space, sd, space. In which, it would NOT find " sdabc" as "sd" is NOT preceded by, and followed by a space.

I can give you working code...but I do not know what the logic is that you require. I have been trying to find that out, and you can not seem to tell me.

You do not want to learn - "I don't want to learn VBA or its logic ".

It is not that hard to learn. Perhaps if you did learn you could find more use for VBA.

I will help, but I can not at this point, as again, I have made both examples work easily. So...I do not know what your problem is, as I do not see a problem. Obviously there IS some kind of problem since you are not getting what you want.

You state: "would you just give me a working code so I can move on....???"

For what it is worth, this is working code...



Sub MyDemo()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(FindText:="25j kq h1 """ & "?$ qww2!!4 ", _
Forward:=True) = True
r.Font.Color = wdColorRed
r.Font.Bold = True
Loop
End With
End Sub



Does that help? Probably not.

gwkenny
05-20-2008, 08:07 AM
...I don't want to learn VBA or its logic since I never needed it before in my life and I'm probably never going to need it again any time soon.

...PLEASE would you just give me a working code so I can move on....???

lol. Do you also post under the name Speedy? :)

Gerry isn't asking you for VBA logic. He is asking you for PLAIN decision making logic that determines what gets highlighted.

This logic is the same whether you are in English, Spanish, Swahili, or Fukien.

Give him, or any decent programmer, a set of clear rules to go by and you can get the code you want and you can move on.

But we're clearly not mind readers.

If you can't give rules, at least give lots of examples and then we can see if we can create rules of decision making logic that can be programmed into code. For example:

{space}abc{space} gets highlighted
{space}abcspecial does not get highlighted
etc...

fumei
05-20-2008, 11:32 AM
Thanks gw.

premois, yes, that is correct. I am not asking you to determine VBA logic, as really, there is no such thing. VBA uses logic.

Logic (at least in the terms applicable here) is simply the rules to be used. Logic means explicitly stating what are your decisions.

True, there are aspects of the Object Model that come into play when you are actually coding. However, I will categorically state that if anyone has their decision making logic clearly worked out, then coding it is both easier, and less prone to errors.

You obviously have errors - it is not working the way you want. I will say it again. This is because you have not worked out the logic. I can NOT do that for you. I do not know what the rules are that you want to happen. Tell me the rules, I can code it. Heck...YOU can code it.

I have asked for examples, and yes, you gave some. And???? I had no problem making it work. I too would like you to be able to move on. But without fuller details from you I do not see how I can help.

I can NOT give you any more working code, as I have already given you working code. If it does not work fully for you, that is because you are not giving full logic - the rules.

Let's start with one thing though. Find will find exactly, precisely, what it is told to find. Nothing more, nothing less.

That is also logic.

If the search string is {space}sd:

{space}sdabxcfg = FOUND

zxsdthe = NO

If the search string is {space}sd{space}:

{space}sdabxcfg = NO

zxsdthe = NO

In other words, Find finds exactly what it is told to find. Nothing more, nothing less.

If you (or any other programmer) wants to expand the logic, then....you can do that.

If the search string is {space}sd:

{space}sdabxcfg = FOUND

Additional (for example) logic:
Rule A: If {space}sd is found, AND the next character is "y", then bold the search string and the next character.

Rule B: If {space}sd is found, AND the next character is "p", then underline the search string and the next character.

Rule C: If {space}sd is found, AND the next character is "p", AND the "p" is NOT followed by a space, then underline the search string and delete the "p".

So......start with:

yadd sdyjozaq bluppg sdp hyfgsdpvy


yadd sdyjozaq bluppg sdp hyfgsdvy

sdyjozaq becomes sdyjozaq - Rule A
sdp becomes sdp - Rule B
hyfgsdpvy becomes hyfgsdvy - Rule C

You can do essentially whatever you want. But Word is not a mind reader either. You have to tell it what the rules are. You.

premois
05-20-2008, 11:37 AM
I try again. One search string is always a complete line. A complete line is defined by all characters till to a {retrun} but without the {return}.
A {return} defines the end of the line. A {space} is also a character.

Example wordlist with 5 search strings:
abc{return}
{space}abc{return}
{space}abc{space}{return}
{space}a{space}b{space}c{space}{return}
c{space}a{return}

The macro should compare all search strings (defined above) idividually with a given text.
If a search string is found in the text, then this to 100% matching string should be highlighted in the text.

Example texts:
---------------------------------------
a{space}b{space}c{retrun}
=> None of the 5 search strings match.
---------------------------------------

{space}abc{space}{space}{retrun}
=> Search strings 1, 2 and 3 match the same string in the text. 4 and 5 don't match.
In case of search string 1 the result for the text is:
{space}abc{space}{space}{retrun}
In case of search string 2 the result for the text is:
{space}abc{space}{space}{retrun}
In case of search string 3 the result for the text is:
{space}abc{space}{space}{retrun}
The combined result after a completed search is:
{space}abc{space}{space}{retrun}
----------------------------------------
{space}a{space}b{space}c{return}
{space}abc{return}
=> Search string 1 and 2 match. 3, 4 and 5 don't match.
In case of search string 1 the result for the text is:
{space}a{space}b{space}c{return}
{space}abc{return}
In case of search string 2 the result for the text is:
{space}a{space}b{space}c{return}
{space}abc{return}
The result after a completed search is:
{space}a{space}b{space}c{return}
{space}abc{return}
-----------------------------------------
abc{space}{space}abc{space}aabc{space}abcc{return}
=> Search string 1, 2, 3 and 5 match. 4 doesn't match.
In case of search string 1 the result for the text is:
abc{space}{space}abc{space}aabc{space}abcc{return}
In case of search string 2 the result for the text is:
abc{space}{space}abc{space}aabc{space}abcc{return}
In case of search string 3 the result for the text is:
abc{space}{space}abc{space}aabc{space}abcc{return}
In case of search string 5 the result for the text is:
abc{space}{space}abc{space}aabc{space}abcc{return}
The result after a completed search is:
abc{space}{space}abc{space}aabc{space}abcc{return}
------------------------------------------
abbcc{space}c{space}a{space}b{space}c{space}{return}
=> Search string 4 and 5 match. 1, 2 and 3 don't match.
In case of search string 4 the result for the text is:
abbcc{space}c{space}a{space}b{space}c{space}{return}
In case of search string 5 the result for the text is:
abbcc{space}c{space}a{space}b{space}c{space}{return}
The result after a completed search is:
abbcc{space}c{space}a{space}b{space}c{space}{return}

:help

premois
05-20-2008, 12:14 PM
And the macro like it is now treats the filter words like following:

abc{return} is treated like abc{return}
{space}abc{return} is treated like abc{return}
{space}abc{space}{return} is treated like abc{space}{return}
{space}a{space}b{space}c{space}{return} is treated like a{space}{return} and b{space}{return} and c{space}{return}
c{space}a{return} is treated like c{space}{return} and a{return}

So the result in case of the last text example is at the moment:

abbcc{space}c{space}a{space}b{space}c{space}{return}
In case of search string 4 (splitted into 3 search strings) the result for the text is:
abbcc{space}c{space}a{space}b{space}c{space}{return}
In case of search string 5 (splitted into 2 search strings) the result for the text is:
abbcc{space}c{space}a{space}b{space}c{space}{return}
The combined result after a completed search is:
abbcc{space}c{space}a{space}b{space}c{space}{return}


Both results from both posts in comparison:
abbcc c a b c -> wrong
abbcc c a b c -> right

premois
05-20-2008, 12:43 PM
****, I added rule 5 later so it's not consistent with the example dammit!
I'll edit this...

edit: edited hope it's all consistent now

fumei
05-20-2008, 01:07 PM
That is horribly confusing. No wonder you are having a hard time. OK, let's go.

First off, let's stop using those {space}. I would prefer to use your original underline. _yadda

So that:

abc{return}
{space}abc{return}
{space}abc{space}{return}
{space}a{space}b{space}c{space}{return}
c{space}a{return}

we will make as:

abc
_abc
_abc_
_a_b_c_
c_

OK? Forget about the Returns for now.

Let's now look at your examples.

Example texts:
---------------------------------------
1. a_b_c

Nothing happens. This is correct, no search string applies.

2. _abc__ (two spaces following)

Result: _abc__

3. _a_b_
_abc

Result: _abc

4. abc__abc_aabc_abcc

Result: abc__abc_aabc_abcc

5. abbcc_c_a_b_c_

Result: abbcc_c_a_b_c_

All of the results are 100% correct. The results are precisely what you have told Word to do.

So...what is your problem???

Take #5 as a test. The result is: abbcc_c_a_b_c_, and that is, again, 100% correct.

What you have not told us, is what do you want it to be? Because it IS doing it correctly.

I think you may have a misunderstanding of how searching works. It takes your search string, and does its work. It then takes the next string, and does its thing.

It has NO memory of what it did previously. If YOU want it to, you can, but YOU have to tell it to do that.

So, with abbcc_c_a_b_c_, yes the search string _a_b_c_ will turn it into: abbcc_c_a_b_c_

THEN, the search string c_ turns it into:

abbcc_c_a_b_c_

Which is EXACTLY what it is supposed to do.

Your results are, again, 100% correct. There is absolutely nothing wrong with the results. If the results are not what YOU want, then....tell Word to do it differently.

How? I don't know, as you are not telling ME.

With the example: abbcc_c_a_b_c_ you seem to be implying that the result abbcc_c_a_b_c_ is not correct.

OK, I will not dispute that, but WHAT is not correct?

WHICH rule should not be applied? Here is your list again, and I will use your numbering.

1. abc
2. _abc
3. _abc_
4. _a_b_c_
5. c_

Now....are you saying if 4 applies (found and actioned) ....do NOT use #5?

Do not use it all?
Do not use it on words that #4 has been actioned on?
Use it on words that #4 has been actioned on...but undo the #4 action?

Look at the above questions. Is there anything about code? No, there is not. It is purely logic. And for the nth time...THAT is your problem.

You do not state what is wrong with example #5 (abbcc_c_a_b_c_). Whether you like it or not, it IS correct. Your search list was searched, and actioned correctly.

Perhaps if you actually REALLY stated the logic, we may help you move on. So...

1. tell me what the result of abbcc_c_a_b_c_ SHOULD be;

2. tell what are the conditions/rules that will make it the result it SHOULD be

Because, as it stands, it searched used #4, applied the action, searched using #5 and applied the action.

Word does not remember that #4 was used (and actioned) when it goes on to using #5.

If YOUR logic requires that it do so...while not trivial to implement, Word most certainly can be made to do that. All it needs is to have..........the logic. YOU have to tell Word...wait second, #4 was actioned here, so skip doing #5 (if that is indeed what you want...I have no idea as you have not stated anything).

fumei
05-20-2008, 01:19 PM
Sorry, we are missing each other.

OK, let me get this straight.

#4 is:
{space}a{space}b{space}c{space}{return} is treated like a{return}

or, _a_b_c_

Now, are you telling me that:

abbcc_c_a_b_c_

results in: abbcc_c_a_b_c_

??? Sorry, but I simply do not believe it. There IS no rule to bold just the a.

So either you are NOT excluding the paragraph marks properly, or there is something else you are not stating.

Look, this is getting boring. Do this for me.

1. make a little file with your list, your five example list is fine.

2. make a document with text, and have some of that text contain some examples.

3. in that document, put the code you are ACTUALLY using.

ZIP them together, and post them here. I will look at them. I want to see EXACTLY what you are doing.

premois
05-21-2008, 02:23 AM
And the macro like it is now treats the filter words like following:

abc{return} is treated like abc{return}
{space}abc{return} is treated like abc{return}
{space}abc{space}{return} is treated like abc{space}{return}
{space}a{space}b{space}c{space}{return} is treated like a{space}{return} and b{space}{return} and c{space}{return}
c{space}a{return} is treated like c{space}{return} and a{return}

So the result in case of the last text example is at the moment:

abbcc{space}c{space}a{space}b{space}c{space}{return}
In case of search string 4 (splitted into 3 search strings) the result for the text is:
abbcc{space}c{space}a{space}b{space}c{space}{return}
In case of search string 5 (splitted into 2 search strings) the result for the text is:
abbcc{space}c{space}a{space}b{space}c{space}{return}
The combined result after a completed search is:
abbcc{space}c{space}a{space}b{space}c{space}{return}


Both results from both posts in comparison:
abbcc c a b c -> wrong
abbcc c a b c -> right
I've edited this post shortly after I posted it cause it was not complete...



abbcc c a b c -> This is re result the macro produces with the search strings:
abc
_abc
_abc_
_a_b_c_
c_a

In the last examples you see how it should actually work to produce abbcc c a b c

premois
05-21-2008, 02:25 AM
abc
_abc
_abc_
_a_b_c_
c_ <- I had an a here it's c_a not c_

premois
05-21-2008, 02:29 AM
I'm now going to read your post exactly so if you are here please wait before you answer.

premois
05-21-2008, 02:34 AM
All of the results are 100% correct. The results are precisely what you have told Word to do.
No! Word is doing abbcc c a b c not abbcc c a b c

premois
05-21-2008, 02:38 AM
Now....are you saying if 4 applies (found and actioned) ....do NOT use #5?
But I said: The macro should compare all search strings (defined above) idividually with a given text.
I said nothing about exeptions so there are no exeptions.


Now I'm gonna zip the macro, a text and a worlist...

premois
05-21-2008, 02:56 AM
OK here is a rar file...

Choose one of the mirrors..
http://www.megaupload.com/de/?d=2YZVWK62
http://www.zshare.net/download/12346036fc2f42d2/
http://rapidshare.com/files/116479434/macro.rar.html
http://www.badongo.com/file/9496360

premois
05-21-2008, 03:10 AM
It's not complicated at all. The only thing I'm trying to say is that a string should match a string. Not more not less.

But at the moment word is splitting the strings of a single line in the world list and so it matches strings it shouldn't macht.

The search string _a_b_c_ gets splittet into a_ and b_ and c_ so that means the search string _a_b_c_ also bolds a text like _c_b_a_ what is completely wrong.

premois
05-21-2008, 03:30 AM
I also recorded what I mean so you can see yourself...

http://xxyx.dyndns.org/macro.swf

premois
05-21-2008, 03:38 AM
Second example..
http://xxyx.dyndns.org/macro2.swf

(http://xxyx.dyndns.org/macro.swf)

premois
05-21-2008, 03:53 AM
Another one, it's probably better...
http://xxyx.dyndns.org/macro3.swf

fumei
05-21-2008, 11:28 AM
The swf re: macro3 was very confusing. I know it says "that is how it should work", but it did not tell me how it should work.

I can not use/open rar files, so the files you posted are of no help for me.

I asked for your current code. You have not posted.
I asked for a ZIP file. You posted a rar.

I will state flatly that Word does NOT, ever, "split" search strings.

If a string in your list is getting split, it is because your code is splitting it. Word will NOT do this on its own. It will never match strings that do not match. Ever.

"it matches strings it shouldn't macht."

Sorry, but no, it does not. It can not. It is doing exactly what it is told to do.

"The only thing I'm trying to say is that a string should match a string. Not more not less."

And I am saying that it does exactly that. Not more, not less.

If it is doing that action, it is because it has been told to do that action. Period.

As I do not know what code you are actually using, it is impossible for me to check what is really going on. However, I think you are doing something like what you posted originally, which is going through the characters of your list.

Do not do this! Use the text of each paragraph in your list (after removing the paragraph mark) as a search string.

"The macro should compare all search strings (defined above) idividually with a given text."

I am not sure what you mean by that. Each search string is completely independent of the others. Each search string is applied independently to the whole document. So I do not know what you mean by "given text".

I can not see how I can help you anymore, so here is my last attempt. In the attached ZIP file, there are two files: LastTry.doc and TestList.doc. UnZIP them into any folder (it doesn't matter which folder, as long as they are both in the SAME folder).

TestList.doc contains:

abc
_abc
_abc_
_a_b_c_
c_a

with a space instead of the underline.

LastTry.doc contains:

abbcc c a b c (your example)


In LastTry.doc click "Last Try" on the top toolbar. This executes the following:


Sub TryAgain2()
Dim ThisDoc As Document
Dim myListDoc As Document
Dim oPara As Paragraph
Dim r As Range
Dim mySearchString As String

Set ThisDoc = ActiveDocument
Set myListDoc = Documents.Open(FileName:=ThisDoc.Path & _
"\TestList.doc"

ThisDoc.Activate

For Each oPara In myListDoc.Paragraphs
mySearchString = Left(oPara.Range.Text, _
Len(oPara.Range.Text) - 1)
Set r = ThisDoc.Range
With r.Find
Do While .Execute(Findtext:=mySearchString, _
Forward:=True) = True
r.Font.Bold = True
r.Font.Color = wdColorRed
Loop
End With
Next
myListDoc.Close wdDoNotSaveChanges
End Sub




Here is the logic.

1. make current document (LastTry.doc) a Document object

2. open the file TestList.doc as a Document object

3. activate the LastTry object

4. for each paragraph in TestList

5. make a string variable (mySearchString) equal the text of that paragraph, with the paragraph mark removed

6. set a range object for LastTry.doc

7. With the range .Find

8. search for the searchstring (mySearchString)

9. if found make it bold and red

10. continue on through the entire document

11. repeat 5 - 10 for the next paragraph (in TestList) until the last paragraph...FOR EACH...

In other words:

use each paragraph in TestList.doc
make a string out it without the paragraph mark
use that string to search LastTry.doc and if found make it bold and red

Result? EXACTLY, PRECISELY, as it is told to do.

abbcc c a b c

No! Word is doing abbcc c a b c not abbcc c a b c

Not in my world. Word is doing EXACTLY, PRECISELY, what I tell it do. If you are getting that result, then you told it to make that result. YOU.

I am done here. I can not help you any more.

Good luck.

premois
05-22-2008, 01:34 AM
Thank you very much! Your macro works exactly how I needed it to work. Very very helpfull. Nevertheless I'm attaching a text and wordlist so you can see what the old macro did in case you want to take a look or tell me what was wrong with the old one :). The attachment contains a document with an example text, the macro I was using, a worldlist and two screenshots, frist one of the result with your macro, second of the one with the old macro.

Same wordlist...

..your macro:
http://img297.imageshack.us/img297/3112/newmacrodq6.png

..other macro:
http://img167.imageshack.us/img167/4919/oldmacropc4.png

premois
05-22-2008, 01:57 AM
That was the "current" code... I can't see which part of the code tells word to split the strings?

Sub CompareWordList()
Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim wrdRef As Object

sCheckDoc = "c:\checklist.doc"
Set docCurrent = Selection.Document
Set docRef = Documents.Open("c:\checklist.doc")
docCurrent.Activate

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.ColorIndex = wdRed
.Replacement.Text = "^&"
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

For Each wrdRef In docRef.Words
If Asc(Left(wrdRef, 1)) > 32 Then
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdRef

docRef.Close
docCurrent.Activate
End Sub

fumei
05-22-2008, 09:50 AM
"Nevertheless I'm attaching a text and wordlist so you can see what the old macro did in case you want to take a look or tell me what was wrong with the old one . The attachment contains a document with an example text, the macro I was using, a worldlist and two screenshots, frist one of the result with your macro, second of the one with the old macro. "

1. my bold. No...it does NOT contain the macro you were using.

2. use Option Explicit!!!! Now. Go into your VBE Options and check Require Variable Declaration. Do it.

3. As your files did NOT contain the code you were using (even though you say it does), I copied the code from your post - CompareWordList().

You do not understand what "word" means. And you are, apparently, not really listening. I asked if you were stepping through your code. You never answered.

You need to learn basic debugging.

You know something is not happening correctly. Yes?
You know it is not picking up your list correctly. Yes?

So....LOOK and see what it is doing. Not what you think it is doing, but what it IS doing.



For Each wrdRef In docRef.Words
If Asc(Left(wrdRef, 1)) > 32 Then
With Selection.Find
.Wrap = wdFindContinue
.Text = wrdRef
.Execute Replace:=wdReplaceAll
End With
End If
Next wrdRef



That is your code for actioning your list. You even asked in one of your early posts about "words". Now...think....

Would it not be a good idea to actually check to see what is happening, to actually look and see what is REALLY going on?


For Each wrdRef In docRef.Words
Debug.Print wrdRef
' If Asc(Left(wrdRef, 1)) > 32 Then
' With Selection.Find
' .Wrap = wdFindContinue
' .Text = wrdRef
' .Execute Replace:=wdReplaceAll
' End With
' End If
Next wrdRef



This outputs what wrdRef IS to the Immediate window. If you want, you could send it to a messagebox, but it is better to use the Immediate window.

It becomes immediately obvious that wrdRef is NOT what you think it is. You could have seen it right away.

Word is not splitting the strings. You tell it to use Words...and it uses Words. You just did not know what Word means by "words".


For Each wrdRef In docRef.Words



Try this, just for your own knowledge. Select the first "line" in checklist.doc. It is:

vol.^p

That is, "vol" followed by a period (a dot), followed by a paragraph mark.

How many "words" do you think are there? One?

Nope. There are three (3). Select the line, and execute this wee Sub.
Sub TellMe()
MsgBox Selection.Words.Count
End Sub



The messagebox will display....3

There are three "words".

vol
the period
the paragraph mark

EACH of those will be processed by

For Each wrdRef In docRef.Words



This is why I have been trying to help/get you to think in terms of logic.

What are you, logically, trying to do?

You are trying to use the text of each "line".

For example, in the line:

at wo

are you trying to (logically) check/test for "at " independently of "wo"? In other words, test for "at " throughout the document, THEN test for "wo"?

If I understand correctly, no, you are trying to test for "at wo".

In which case, again, you are trying to test using the text of each "line".

So... logically, that is what you need to do. Get the text of each "line" - and let's be accurate here, it is each PARAGRAPH.

Which is what my code does. It grabs the text of each paragraph in the list, striping off the paragraph mark.

Voila!

"at wo" is the search string.

So, in conclusion...

1. Use Option Explicit

2. Use Range, not Selection, where ever possible

Now, I want to make sure you understand that I am not being critical of you. Many people have made the same mistake in thinking about "words". The mistake is thinking that Word considers "words" the same way as humans think of "words".

It does not.

The fault lies in this...people thinking that Word "considers" anything.

It does not. It is a computer program. It does not think of anything. It has a design, and it always does exactly what it is instructed to do.

If something results in a way that is not desired, then the fault is not with Word. IMO, even "bugs" - and there definitely are some - are never the fault of the application. It is the fault of whoever wrote the code.

So you think Word is "splitting", but it is not. It is, again, again, again, doing EXACTLY, PRECISELY, what it is told to do.