Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 36 of 36

Thread: Mark words from wordlist in text

  1. #21
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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).

  2. #22
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  3. #23
    Quote Originally Posted by premois
    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

  4. #24
    abc
    _abc
    _abc_
    _a_b_c_
    c_ <- I had an a here it's c_a not c_

  5. #25
    I'm now going to read your post exactly so if you are here please wait before you answer.

  6. #26
    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

  7. #27
    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...

  8. #28

  9. #29
    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.

  10. #30
    I also recorded what I mean so you can see yourself...

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

  11. #31

  12. #32
    Another one, it's probably better...
    http://xxyx.dyndns.org/macro3.swf

  13. #33
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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:
    [vba]

    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

    [/vba]


    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.

  14. #34
    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:


    ..other macro:

  15. #35
    That was the "current" code... I can't see which part of the code tells word to split the strings?

    [VBA]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[/VBA]

  16. #36
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "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.

    [vba]

    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

    [/vba]

    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?

    [vba]
    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

    [/vba]

    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".
    [vba]

    For Each wrdRef In docRef.Words

    [/vba]

    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.[vba]
    Sub TellMe()
    MsgBox Selection.Words.Count
    End Sub

    [/vba]

    The messagebox will display....3

    There are three "words".

    vol
    the period
    the paragraph mark

    EACH of those will be processed by [vba]

    For Each wrdRef In docRef.Words

    [/vba]

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •