Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-27-2015, 01:07 AM
streetcat streetcat is offline Help tweak the Macro Windows 7 64bit Help tweak the Macro Office 2010 64bit
Novice
Help tweak the Macro
 
Join Date: Jan 2015
Posts: 4
streetcat is on a distinguished road
Default Help tweak the Macro

Hi All,
My requirement: Certain words should not appear in our company publications such as Vice versa, &, in-built, and ad hoc.
What I have done so far: I have created a table with just one Colum (in a word doc, of course!), and typed all the offending words. Then I executed the following macro:
Code:
Sub Highlighting()
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim rFindText As Range
Dim i As Long
Dim sFname As String
sFname = "location of the file"
Set oDoc = ActiveDocument
Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
Set oTable = oChanges.Tables(1)
For i = 1 To oTable.Rows.Count
    Set oRng = oDoc.Range
    Set rFindText = oTable.Cell(i, 1).Range
    rFindText.End = rFindText.End - 1
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        Do While .Execute(FindText:=rFindText, _
            MatchWholeWord:=True) = True
            oRng.HighlightColorIndex = wdBrightGreen
        Loop
    End With
Next i
oChanges.Close wdDoNotSaveChanges
End Sub
My Requirement: To an extent, the above macro helped me. However, I am unable to easily eliminate offending phrases such as “It is recommended”. If I use quotation marks, macro is unable to find the phrase; if I don’t use the quotes, then macro is searching for all the words (it, is, and recommended).
Can someone help me tweak this macro?
Thanks in advance,


Streetcat

Last edited by streetcat; 01-27-2015 at 03:15 AM. Reason: Code wrapped as suggested by macropod
Reply With Quote
  #2  
Old 01-27-2015, 02:53 AM
gmayor's Avatar
gmayor gmayor is offline Help tweak the Macro Windows 7 64bit Help tweak the Macro Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Change the line
Code:
Do While .Execute(FindText:=rFindText, _
            MatchWholeWord:=True) = True
to
Code:
Do While .Execute(FindText:=rFindText) = True
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-27-2015, 03:05 AM
macropod's Avatar
macropod macropod is offline Help tweak the Macro Windows 7 64bit Help tweak the 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 Highlighting()
Application.ScreenUpdating = False
Dim oChanges As Document, oDoc As Document
Dim oTable As Table, oRng As Range, i As Long
Dim rFindText As Range, sFname As String, HiLite As Long
HiLite = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
sFname = "location of the file"
Set oDoc = ActiveDocument
Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
Set oTable = oChanges.Tables(1)
With oDoc.Range.Find
  .ClearFormatting
  .Format = True
  .Forward = True
  .Wrap = wdFindContinue
  .MatchWholeWord = True
  With .Replacement
    .ClearFormatting
    .Highlight = True
    .Text = "^&"
  End With
  For i = 1 To oTable.Rows.Count
    Set oRng = oTable.Rows(i).Cells(1).Range
    With oRng
      .End = .End - 1
      rFindText = .Text
    End With
    .Text = rFindText
    .Execute Replace:=wdReplaceAll
  Next
End With
Options.DefaultHighlightColorIndex = HiLite
oChanges.Close wdDoNotSaveChanges
Application.ScreenUpdating = True
End Sub
PS: When posting code, please use the code tags, inserted via the # button on the toolbar.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 01-27-2015, 05:44 AM
streetcat streetcat is offline Help tweak the Macro Windows 7 64bit Help tweak the Macro Office 2010 64bit
Novice
Help tweak the Macro
 
Join Date: Jan 2015
Posts: 4
streetcat is on a distinguished road
Default

thanks @gmayor and @macropod. The code worked seamlessly :-)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help tweak the Macro Macro Question: Need help making a macro to highlight the first word in every sentence LadyAna Word 1 12-06-2014 10:39 PM
Macro Needed to bold specific lines and Macro to turn into CSV anewteacher Word VBA 1 05-28-2014 03:59 PM
custom icon, undo/redo for macro, permanent macro Rapier Excel 0 08-05-2013 06:30 AM
How do I assign a macro to a button when the macro is in my personal workbook? foolios Excel Programming 2 07-27-2011 02:41 PM
Help tweak the Macro New to Word 2010 and I need to tweak it for printing Bobosmite Word 1 07-01-2010 11:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:17 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