PDA

View Full Version : Making a Find/Replace All macro repeat



Shimo.Ochiai
03-05-2009, 05:41 PM
I have made the following macro to highlight a paragraph, search for the highlighted paragraph, replace all with a different font color, and then move on to highlight the next paragraph. The point is to identify repetitions.

It works fine for the first paragraph, but I just cannot get it to move on automatically/loop with a new selection. Any ideas?

(Here I have included just the part that works. If you can coack me on how to get it to repeat, that would be fantastic.)

OS: Windows XP
Word 2003

Sub CheckAndMoveOn()

Selection.Extend
Selection.Extend
Selection.Extend
Selection.EscapeKey
Selection.Copy
Selection.Find.ClearFormatting
Selection.Find.Highlight = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Hidden = True
With Selection.Find
.Text = Selection.Text
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.HomeKey Unit:=wdLine
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Find.Highlight = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Hidden = False
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = Selection.Text
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
End Sub

fumei
03-06-2009, 11:14 AM
It is because you are using Selection. Try to avoid that. What are you trying to do? If I understand correctly, you are trying to:

1. select some text
2. search for the same text later on in the document
3. if same text is found, then color that identical text
4. continue the search

OK. here is how you do it.
Option Explicit

Sub HighlightDuplicates()
Dim r As Range
Dim j As Long
Dim strSearchFor As String

' set the search string to the current Selection
strSearchFor = Selection.Text

' set the range object to be from
' the Selection END, to the end of the document
Set r = ActiveDocument.Range( _
Start:=Selection.Range.End, _
End:=ActiveDocument.Range.End)

' start the search
With r.Find
.ClearFormatting
' while the search is found
Do While .Execute(Findtext:=strSearchFor, _
Forward:=True) = True

With r
' expand the whole paragraph
.Expand wdParagraph
' if the expanded paragraph is the same
' as the search text
If r.Text = strSearchFor Then
' then it is a duplicate, so color it
r.Font.Color = wdColorBrightGreen
End If
' collapse the range to its End
.Collapse 0
End With
' and continue on
Loop
End With
End Sub

fumei
03-06-2009, 11:28 AM
NOTE!!! It is utterly critical that the selected text - what you call highlighting - be truly what you are searching for. That means, if the selected text is a complete paragraph it makes a HUGE difference whether you selected (highlighted) the paragraph mark, or not.

A paragraph mark is symbolized by <p>...

This is sample text.<p>
This is sample text. But this paragraph has additional text.<p>
This is sample text.<p>
This is something else, but whatever. This is sample text.<p>
This is sample text.<p>

I am assuming that the result you want (in bold) is:

This is sample text.<p>
This is sample text. But this paragraph has additional text.<p>
This is sample text.<p>
This is something else, but whatever. This is sample text.<p>
This is sample text.<p>

Logic:

Paragraph1 is selected, so that is the "original"
Paragraph2 - No, because the paragraph is NOT the same as the search text
Paragraph3 - Yes, it is identical
Paragraph4 - No, it is not identical.
Paragraph5 - Yes, it is identical

So say, you select (highlight) Paragraph1 to do the search, like this (in bold):

This is sample text.<p>

so that the paragraph mark is NOT selected, then the result will be...



follow the logic people.

The result is...NOTHING. There is no identical match. The result will be:

This is sample text.<p>
This is sample text. But this paragraph has additional text.<p>
This is sample text.<p>
This is something else, but whatever. This is sample text.<p>
This is sample text.<p>


However, if you select (highlight) Paragraph1 including the paragraph mark:

This is sample text.<p>

Then the result will be:

This is sample text.<p>
This is sample text. But this paragraph has additional text.<p>
This is sample text.<p>
This is something else, but whatever. This is sample text.<p>
This is sample text.<p>

fumei
03-06-2009, 11:29 AM
Logic, logic, logic.

duluter
03-06-2009, 06:53 PM
Sometimes it's more than just "logic, logic, logic." Not everyone is familiar with the ins and outs of paragraph marks, etc. People are at all levels of experience.


Duluter

lucas
03-07-2009, 09:36 AM
Well, you just admitted that you were inexperienced and you could benifit greatly from Gerry's knowledge. He's giving you a tip that supercedes your level of experience as none of it will be efficient or reliable if you don't follow the basic premis that he puts forth. Agreed?

duluter
03-07-2009, 04:08 PM
I wasn't the OP, but I felt that the tone was unbefitting to the forum, so I commented. Recently, others have felt that his tone is inappropriate as well:

http://www.vbaexpress.com/forum/showthread.php?t=25466

This post could have been resolved politely by simply pointing out that the paragraph mark needs to be included in the selection and why, without the editorializing. My earlier post referred to the fact that repeating "logic, logic, logic" and saying "follow the logic people" is unconstructive, especially since this is less a logical error and more a unfamiliarity-with-the-ins-and-outs-of-Word error. It's off-putting language.


Duluter

lucas
03-07-2009, 04:48 PM
I read the threads twice. Your's and nelsonaddiso were the only ones that I found offensive.

We do pride ourselves on having a friendly forum and I thought Gerry went way out of his way to help you both People volunteer here, they do it for free. You are the one who is here asking for help.

I'm serious when I tell you this as a friend. I don't think this forum is for you unless you can change the way you interact, immediatly.

Your only warning.

duluter
03-07-2009, 05:13 PM
I thought that what I originally wrote in this thread was very gentle. I was saying that people have all different kinds of experience with Word. The text of the post didn't really go beyond just that.

I re-read the thread mentioned above and can see your point that Gerry's post probably didn't merit the response he got there.

I love this forum. And I love the fact that Gerry donates his time to help others. His responses are incredibly informative. Gerry, if you're reading--thank you for all that you have done here.

My only interest at this point is in reconciliation.


Duluter

lucas
03-07-2009, 05:33 PM
I'm glad you see things that way and I appreciate you saying so. We all have bad days.

I think it's got something to do with the phase of the moon or something......

fumei
03-09-2009, 02:08 PM
Look...everyone.

I am sorry that I come across so bluntly, and that people feel I bludgeon them.

This is never, EVER, my intention.

My intention re: the paragraph mark post was - sorry, but to me it IS logic - to point out that it is crucial to be precise.

I demonstrated the why and how VBA does exactly, and only, what it is told to do.

IF you select the paragraph without the paragraph mark, THIS happens (nothing).

IF you select the paragraph with the paragraph mark, THIS happens (it does what you want).

True, very true, it does have a great deal to do with your experience. True.

And true, perhaps I do not come across very well.

However, my intention was to IN FACT point out how important paragraph marks are in Word. My intention was to help people gain that understanding of its importance.

As an aside: my personal ranking of the top three things to understanding object/structure in Word is:

the Paragraph Mark
Range
Section

I know it seems I rant, but perhaps some of that is simply the volume of text that I post. I try and give full explanations, as I am trying to help people gain understanding. Not just code solutions, but being able to think deeply and fully with Word. That comes from understanding, and yes, some people do not know things. Heck, I started out not knowing things. I have never overtly expressed superiority, although it certainly has been interpreted[ that way. When I am trying to pass along what I know, that really (honestly) is my intention. I am trying to pass it on. Nothing more. I do not need - and do not - feel any ego satisfaction from any of this. I want people to get it, to understand.

I think it is time for another break from here. I am not doing this well again. My fault.

lucas
03-09-2009, 02:27 PM
I think it is time for another break from here.

Really wish you would reconsider Gerry. Don't let a little rough water spoil the sailing.