Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-03-2013, 04:56 PM
bertietheblue bertietheblue is offline Is the following too complex for find/replace macro? Windows 7 64bit Is the following too complex for find/replace macro? Office 2003
Advanced Beginner
Is the following too complex for find/replace macro?
 
Join Date: May 2012
Location: United Kingdom
Posts: 54
bertietheblue is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 11-03-2013, 05:00 PM
macropod's Avatar
macropod macropod is offline Is the following too complex for find/replace macro? Windows 7 32bit Is the following too complex for find/replace macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 11-03-2013, 05:12 PM
bertietheblue bertietheblue is offline Is the following too complex for find/replace macro? Windows 7 64bit Is the following too complex for find/replace macro? Office 2003
Advanced Beginner
Is the following too complex for find/replace macro?
 
Join Date: May 2012
Location: United Kingdom
Posts: 54
bertietheblue is on a distinguished road
Default

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)).
Reply With Quote
  #4  
Old 11-03-2013, 07:36 PM
macropod's Avatar
macropod macropod is offline Is the following too complex for find/replace macro? Windows 7 32bit Is the following too complex for find/replace macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
where 'StrWrds' contains the list of words that must precede the words of interest. Note that you'll need to input the singular and plural forms of each word of interest.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-04-2013, 03:43 AM
bertietheblue bertietheblue is offline Is the following too complex for find/replace macro? Windows 7 64bit Is the following too complex for find/replace macro? Office 2003
Advanced Beginner
Is the following too complex for find/replace macro?
 
Join Date: May 2012
Location: United Kingdom
Posts: 54
bertietheblue is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
where 'StrWrds' contains the list of words that must precede the words of interest. Note that you'll need to input the singular and plural forms of each word of interest.
Thanks as always, macropod. I tried this but it doesn't work: I assume it's because I need to allow for initial u/c words that may precede the words of interest rather than must, because I also need the words highlighted if they are not preceded by an initial u/c word (eg 'the Notes').

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?
Reply With Quote
  #6  
Old 11-04-2013, 04:17 AM
macropod's Avatar
macropod macropod is offline Is the following too complex for find/replace macro? Windows 7 32bit Is the following too complex for find/replace macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 11-04-2013, 04:42 AM
bertietheblue bertietheblue is offline Is the following too complex for find/replace macro? Windows 7 64bit Is the following too complex for find/replace macro? Office 2003
Advanced Beginner
Is the following too complex for find/replace macro?
 
Join Date: May 2012
Location: United Kingdom
Posts: 54
bertietheblue is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 11-04-2013, 04:56 AM
macropod's Avatar
macropod macropod is offline Is the following too complex for find/replace macro? Windows 7 32bit Is the following too complex for find/replace macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #9  
Old 11-04-2013, 05:29 AM
bertietheblue bertietheblue is offline Is the following too complex for find/replace macro? Windows 7 64bit Is the following too complex for find/replace macro? Office 2003
Advanced Beginner
Is the following too complex for find/replace macro?
 
Join Date: May 2012
Location: United Kingdom
Posts: 54
bertietheblue is on a distinguished road
Default

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!
Reply With Quote
  #10  
Old 11-04-2013, 01:27 PM
macropod's Avatar
macropod macropod is offline Is the following too complex for find/replace macro? Windows 7 32bit Is the following too complex for find/replace macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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:
highlight any numbered headings
Highlighting numbered headings can be done by changing their Style definitions (you are using Styles, aren't you?).

Re:
Quote:
autopopulate entries in the FindText array
...
generate a list of number of instances each term appears
To see what's involved for these two aspects, go to: https://www.msofficeforums.com/word/...sage-same.html. Although the code there does somewhat more than you're asking for in this case, it is similarly complex, as the code has to both build the list and generate the output.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 11-04-2013, 01:45 PM
bertietheblue bertietheblue is offline Is the following too complex for find/replace macro? Windows 7 64bit Is the following too complex for find/replace macro? Office 2003
Advanced Beginner
Is the following too complex for find/replace macro?
 
Join Date: May 2012
Location: United Kingdom
Posts: 54
bertietheblue is on a distinguished road
Default

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.

Reply With Quote
  #12  
Old 11-04-2013, 04:56 PM
fumei fumei is offline Is the following too complex for find/replace macro? Windows 7 64bit Is the following too complex for find/replace macro? Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

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...
Reply With Quote
  #13  
Old 11-04-2013, 05:35 PM
macropod's Avatar
macropod macropod is offline Is the following too complex for find/replace macro? Windows 7 32bit Is the following too complex for find/replace macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by fumei View Post
Me, I am still trying to parse out what the heck Paul has posted...
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]
Reply With Quote
Reply



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
Is the following too complex for find/replace macro? 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
Is the following too complex for find/replace macro? 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:39 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft