![]() |
|
#1
|
|||
|
|||
|
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
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 |
|
#2
|
||||
|
||||
|
Change the line
Code:
Do While .Execute(FindText:=rFindText, _
MatchWholeWord:=True) = True
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 |
|
#3
|
||||
|
||||
|
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#4
|
|||
|
|||
|
thanks @gmayor and @macropod. The code worked seamlessly :-)
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
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 |
New to Word 2010 and I need to tweak it for printing
|
Bobosmite | Word | 1 | 07-01-2010 11:31 AM |