#1
|
|||
|
|||
Is the following too complex for find/replace macro?
Is the following possible in a VBA macro or is it going to be simply too complex? And if so, is there a kind of shortcut so I can change the 'term' (I have 1,000s) each time in an instant? Basically, I'm looking for a custom, advanced 'Find and Replace'.
I'm not asking anyone to write me the code for free, though I know many contributors on here are generous with their time. I simply want to know if it can be done - I know people in far off places who say they can do it, but I don't want to part with my money only to find they can't. Many thanks, all Bertie Find and replace with highlight: [not preceded by any capital letter or initial upper-case word, except those in a given list, eg 'The', 'Each' etc] [Term in plural or singular form] [not followed by a capital letter] so I can highlight, eg: Note, Notes, [The] Notes, [Each] Note, etc but ignore, eg: Class A Notes, Noteholder, Note Event of Default, etc |
#2
|
||||
|
||||
For what you're describing, you could use a two-stage process:
1. Highlight all possible candidates 2. Remove the highlights from the words in the exclusion list.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks, but I should have said: I have no idea of the terms in my exclusion list. All I know is, I want to highlight the stand-alone terms 'Note'/'Notes', but I don't want to highlight any term (which could be anywhere in a long document) with 'Note'/'Notes' in it (ie any term with an initial upper-case word preceding or following 'Note'/'Notes' except, if preceding, for specified words commonly appearing at the beginning of a sentence (The, Each, An, etc)).
|
#4
|
||||
|
||||
You could use something like:
Code:
Sub ConditionalHighlight() Application.ScreenUpdating = False Dim vFindText As Variant, StrWrds As String, i As Long vFindText = "Note,Notes" vFindText = Split(vFindText, ",") StrWrds = ",The,Each," For i = LBound(vFindText) To UBound(vFindText) With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Forward = True .Format = False .MatchWildcards = True .Wrap = wdFindStop .Text = "<[! ]{1,}> " & vFindText(i) & ">" .Replacement.Text = "" .Execute End With Do While .Find.Found If InStr(StrWrds, "," & Trim(.Duplicate.Words.First.Text) & ",") > 0 Then .Duplicate.HighlightColorIndex = wdBrightGreen End If .Collapse wdCollapseEnd .Find.Execute Loop End With Next i Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Quote:
So highlight only: The / Each [and other initial u/c words added to list] Note/Notes [not followed by a capital letter] [any initial l/c word] Note/Notes [not followed by a capital letter] Is it possible to tweak the code for this to work? |
#6
|
||||
|
||||
Perhaps:
Code:
Sub ConditionalHighlight() Application.ScreenUpdating = False Dim vFindText As Variant, StrWrds As String, i As Long vFindText = "Note,Notes" vFindText = Split(vFindText, ",") StrWrds = ",The,Each," For i = LBound(vFindText) To UBound(vFindText) With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Forward = True .Format = False .MatchWildcards = False .MatchAllWordForms = False .MatchWholeWord = True .Wrap = wdFindStop .Text = vFindText(i) .Replacement.Text = "" .Execute End With Do While .Find.Found With .Duplicate.Words.First If LCase(.Next.Words.First.Characters.First) = .Next.Words.First.Characters.First Then .MoveEnd wdWord, 1 If .Characters.First.Previous <> vbCr Then If InStr(StrWrds, "," & Trim(.Previous.Words.First.Text) & ",") > 0 Then .MoveStart wdWord, -1 .Duplicate.HighlightColorIndex = wdBrightGreen ElseIf LCase(.Previous.Words.First.Characters.First) = .Previous.Words.First.Characters.First Then If .Previous.Words.First.Characters.First.Previous = vbCr Then .MoveStart wdWord, -1 .Duplicate.HighlightColorIndex = wdBrightGreen End If End If End If End If End With .Collapse wdCollapseEnd .Find.Execute Loop End With Next i Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Thanks, macropod. I've run this code and it's almost there. Two slight tweaks - if possible - and I've got exactly what I need:
Additional highlight: [not preceded by any words][term, ie term at beginning of sentence)][not followed by any capital letter] Also, is it possible for only the term to be highlighted and not the surrounding text? Anyway, if those 2 things can be sorted, it would be great. If not, just to say, you've been very helpful, just as you were last time I posted a year or so ago to ask about wildcard searches. |
#8
|
||||
|
||||
Try:
Code:
Sub ConditionalHighlight() Application.ScreenUpdating = False Dim vFindText As Variant, StrWrds As String, i As Long, Rng As Range vFindText = "Note,Notes" vFindText = Split(vFindText, ",") StrWrds = ",The,Each," For i = LBound(vFindText) To UBound(vFindText) With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Forward = True .Format = False .MatchWildcards = False .MatchAllWordForms = False .MatchWholeWord = True .Wrap = wdFindStop .Text = vFindText(i) .Replacement.Text = "" .Execute End With Do While .Find.Found Set Rng = .Duplicate.Words.First With Rng If LCase(.Next.Words.First.Characters.First) = .Next.Words.First.Characters.First Then If .Sentences.First.Start = .Start Then Rng.Duplicate.HighlightColorIndex = wdBrightGreen Else With .Previous.Words.First If InStr(StrWrds, "," & Trim(.Text) & ",") > 0 Then Rng.Duplicate.HighlightColorIndex = wdBrightGreen ElseIf LCase(.Characters.First) = .Characters.First Then Rng.Duplicate.HighlightColorIndex = wdBrightGreen End If End With End If End If End With .Collapse wdCollapseEnd .Find.Execute Loop End With Next i Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
This is absolutely brilliant! I won't ask you - because you've been more than generous with your time already - but if I can:
- highlight any numbered headings -autopopulate entries in the FindText array with 'any bold, non-highlighted strings of words in the document', ie definitions (but not headings since highlighted under the 1st step above) - run your code - generate a list of number of instances each term appears. I will know in an instant which definitions are not used. My whole job revolves around definitions so this is amazing progress! |
#10
|
||||
|
||||
It would have been helpful if you gave the full scope of the project at the outset. What you're now asking for would require a complete re-write.
Re: Quote:
Re: Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
My apologies, Paul. I just provided individual stages of my project, because I considered each stage a separate thread - I never stopped to think they'd be interconnected - and also that if I detailed my project requirements in full, it would be an extremely long thread and too much to expect anyone to read, let alone suggest any code.
Thanks for the further links. I'm going to work with the codes you've provided so far, and see what needs further refining. Then I will produce a full project proposal. I appreciate your help so far, and I don't want to push my luck asking for more code for free, so I'll put my project up for bid when done. |
#12
|
|||
|
|||
Must say bertietheblue you are graciously appreciative of what Paul has done, AND understanding of what he is saying. This is a classic example of project creep, where requirements get bigger and bigger. It really does help if people express (as clearly as possible) what they want, but of course it is often the case they do not really know.
Me, I am still trying to parse out what the heck Paul has posted... |
#13
|
||||
|
||||
Yeah, I didn't spend too much time on commenting this one ... but you're clever enough to work it out!
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Find - Replace Macro using a table list | mdw | Word | 0 | 08-01-2013 04:36 PM |
Find and Replace Format macro issue | Jack | Word VBA | 2 | 12-12-2012 09:24 PM |
macro or find/replace | JamesVenhaus | Word | 2 | 02-27-2012 03:34 PM |
Complex Find and Replace | paulkaye | Word | 1 | 11-13-2011 04:23 AM |
Find and Replace Macro - A Better Way | Tribos | Word VBA | 0 | 10-08-2008 03:22 AM |