Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 53

Thread: Code Integration Query

  1. #1
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location

    Smile Code Integration Query

    Hi guys, I am new to vba and I am trying to learn it. I am a writer and i want to create a vba that compares two paragraphs for duplication. If duplication if found the sentence being duplicated will be highligted into different colors, example i found 5 duplicate, i want to see also five colors in the output so that i can easily count the number of duplicated sentence fragments or phrases. I found a code that do exactly what i need only that it cannot set a lenght of matching parts and it has only two colors for highlighting duplicates. I created a userform that contains 2 rich texbox, the first box is for the source and the second box is for the article to be compared in the source. I also put a command button "Compare". I also put a combo box so that i can select the minimum lenght of matching part. Below is the code i found:

    [vba]

    Sub Compare()

    Const NMin As Long = 7
    Dim R As Range, W As Range
    Dim C As Range, C2 As Range, N As Long

    Set R = ActiveDocument.Content

    For Each W In R.Words
    If W.HighlightColorIndex = wdNoHighlight Then
    N = NMin

    Do
    Set C = W.Duplicate
    C.MoveEnd wdWord, N
    If C.End = ActiveDocument.Range.End Then Exit Sub
    Set C2 = ActiveDocument.Range(C.End, ActiveDocument.Range.End)

    Select Case True
    Case Len(C.Text) > 256, _
    C.HighlightColorIndex = 9999999, _
    C2.Find.Execute(FindText:=C.Text, Wrap:=wdFindStop) = False

    If N > NMin Then DoHighLight C
    Exit Do

    Case Else
    N = N + 1

    End Select
    Loop

    End If
    Next
    End Sub

    Sub DoHighLight(C As Range)

    Dim C2 As Range

    C.MoveEnd wdWord, -1
    C.HighlightColorIndex = wdYellow
    Set C2 = ActiveDocument.Range(C.End, ActiveDocument.Range.End)
    While C2.Find.Execute(FindText:=C.Text, Wrap:=wdFindStop)
    C2.HighlightColorIndex = wdRed
    Wend

    End Sub

    Public Sub Show()
    frmMain.Show
    End Sub


    [/vba]

    Now my question is how can I integrate this code into my own modified format. Please see attached file.

    Hope you can spare time on my problem.
    Thank you so much.

    Credits to Tony Jollans for the code.
    Attached Files Attached Files
    Last edited by deedii; 02-05-2012 at 09:26 PM.

  2. #2
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    UP. Can someone please help me?

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Use Option Explicit.

    What is NMin?????

  4. #4
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    NMin is the length of matching parts. Meaning it will highlight the sentence with seven succeeding words duplicated.

    e.g
    Source - RichBox A
    The quick brown fox jumps over the lazy dog.
    Article - RichBox B
    The quick brown fox jumps over the lazy dog.

    That codes works just fine. I just need to integrate it into a form with two rich text box and compare the two like in the layout i created in the attached file.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    NMin is NOT defined...use Option Explicit. The code only works, supposedly, fine because you are not using Option Explicit.

    It is hard to test as you only have two examples in your document. Plus I am getting object errors from your code.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Plus you have TWO subroutines with the same name - Compare. This is not good. I am having a hard time getting anything to work. Could you explain how it is supposed to work?. Are you selectng text and putting it, somehow into the userform? Are you making some text highlighted and looking for similar text? It is impossible to tell from the sample document.

    You have combobox1 - a dropdown - with NO items. If you are typing text (3,4,5,etc), why is it a combobox (a dropdown). Plus if LMatch equals the value in combobox1, why are you doing a Select Case? Just make them equal.
    [vba] Select Case ComboBox1.Text
    Case "3"
    LMatch = 3
    Case "4"
    LMatch = 4
    Case "5"
    LMatch = 5
    Case "6"
    LMatch = 6
    Case "7"
    LMatch = 7
    Case "8"
    LMatch = 8
    Case "9"
    LMatch = 9
    Case "10"
    LMatch = 10
    Case Else
    End Select
    [/vba]

    [vba]LMatch = ComboBox1.Value[/vba]

    or more carefully

    LMatch = CInt(ComboBox1.Value)

    I am not trying to be critical. I am trying to figure what is going on.
    Last edited by fumei; 02-08-2012 at 09:32 PM.

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    For example, I cleared all highlights, put new highlight on the first four words (which ARE duplicated), typed 3 into the...ahem, empty dropdown...click Compare and get a messagebox "Dogs"

    HUH????

  8. #8
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    Ok sorry for the confusion in the file. Im just playing with it to see the workaround. Here's the scenario, I ' am writing an article from the source that my editorial adviser gave me, after i rewrite the article I will now compare my rewritten article from the source if there are duplications. The code i posted here is actually do the job in finding the duplicated document and highlight it, only that it works for whole document, meaning I have to put the source and the article i wrote in one document, to avoid confusion i decided to make a form which has two RichBox one is where i paste the source and the other is where i paste the article i wrote to be compared from the source. Never mind what is written in the documents coz I will just copy paste it in the form the most important is the macro in the form that will compare Source & Article for duplication.

    I don't highlight it manually the code is the one who is highlighting the duplicated sentence.

    The code is working fine, my problem is that how will i integrate that code into the form I created which will compare Rich Box A to Rich Box B.

  9. #9
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    Oh Im so sorry I made you confused. That combo box is actually to change the value of NMin so that it will be more flexible, I dont want to limit NMin as 7 so I experimented to add another variable, I am thinking to make it as inputbox instead of combox so i can really just input what is the NMin. Please dont mind the code in the form coz its actually a mess of my experimentation the real code that works and i want to integrate is in the module. I forgot to clean the mess in form when i attached it here. Sorry

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Hopefully someone else can help you because as stated the code does not work fine for me.

    Again though, I strongly recommend you start using Option Explicit.

  11. #11
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    What code fumei? Please download the attached file "DupTest" and run "compare" to see what I mean. The code actually works but only inside the document. The form I created has no code since thats my problem, integrating the code into the form. This will be the result if you will run the code in the attached document.



    If you can see the code only works inside the document itself. So the code compare the Page 1 of the document which is the SOURCE to Page 2 which is the Article.

    I want to achieve the same result but using a form so it should be like this;



    So when I click "Compare" the result should be like this (Note: That is only a screenshot of what should the result be."



    So in general I want to integrate the code inside the file I attached into the Form.

    Hope it is clear now.
    Attached Files Attached Files

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    The file DupTest is even worse. The userform module has no code at all. So clicking Compare does absolutely nothing.


    Ah, wait. You did it as a procedure right in the document. Let me see....

  13. #13
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    Oh as I posted. The reason why the userform has no code, bcoz thats actually my problem integrating the working code in a module into the userform. So all of those command button and input box has still no function at all.

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Wow. I have seen some brute force code before, but that is incredible. The small sample in the demo executes more than 4,000 instructions! Good grief.

    I am steppng through it. Can you repeat clearly what it is you want to happen?

  15. #15
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    I want to integrate the code in the module to the userform. The final result should be like below image.



    So instead of using the code in the active page I will just copy paste the data(source and article) to the userform which has two richbox(source & article) to be compared.

    (Note: That image above was just created so that i can show you what will be the result when you click "compare".)

  16. #16
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Well you are NOT going to get the colour part for the text in the userform.

    I do not see how the code will work for text IN the userform. Are you saying you have made this work? In that you copy text into each textbox and get this kind of result?

    Lastly, there is only so much text that can fit into the textboxes. Does this not limit things?

  17. #17
    VBAX Regular deedii's Avatar
    Joined
    Dec 2011
    Posts
    50
    Location
    No its not a normal textbox its a RichTextBox. I didnt make it work, what i did to get the screenshot of the possible result, is just copy and paste the result in the document after i run the code in that two rich boxes into the userform just to get that screenshot. So i think RTB can handle that format coz when i pasted it there it has also a highlight as shown in the screenshot.

  18. #18
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I just restructured the code in this version, didn't actually try make the compare work. This is probably the way I'd do it, but there's lots of different ways

    Now my question is how can I integrate this code into my own modified format
    The code that the userform handles or needs -- I put in the user form module. I also added an 'Exit' CommandButton

    The macro ('Compare') that is exposed to the user is in mod_Main

    The 'helper' subs, etc. are in a Option Private Module called mod_Subs

    Like I said, it really doesn't do the compare, but the restructuring / integration might make it easier for you to incorporate.

    Any questions ... just ask, and I'm sure someone will help

    Paul
    Attached Files Attached Files

  19. #19
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Also ..

    It seems like a lot of work and you won't get the color high lighting

    Word 2010 has a nice document compare built in, so if 2010 is an option, you might investigate it

    This is a screen shot of all the Compare options pasted into the Compare document, and the Add/Deletes on the left

    Paul
    Attached Images Attached Images

  20. #20
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    paul, why do you code separate Load/Show instructions?[vba] Sub Compare()
    Load frmMain
    frmMain.Show
    End Sub[/vba]The Show instruction fires the Load automatically. Technically speaking, the Show instructiobn causes VBA to check if the object IS Loaded, and if it is not, Load it. Actually even more technically speaking, the Show instruction does a Load (sending an internal instruction to allocate memory), but if it already is loaded (memory is allocated) - an internal IF...THEN - continues on to execute the internal Show instructions (sending object attributes to the GUI).

Posting Permissions

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