PDA

View Full Version : Solved: Coding a new macro for Un-highlighting



shekhu
03-16-2007, 02:59 AM
Can anyone help in coding a new macro (without using "find and replace" feature) for erasing the highlights of words or sentences in the document.
Right now I am using the following macro through "find and replace", which causes difficulty in using find feature to find something else, as with previous macro use special charecters (that are in find and replace feature) are saved there. When I do a find for something else I have to delete those characters,e.g., "^?" and "^&".

The macro I am using is this:

Sub Eraser()
'
' Macro1 Macro
' Macro recorded 01/28/2007 by Accolade
'
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = False
With Selection.Find
.Text = "^?"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

mdmackillop
03-16-2007, 09:51 AM
How about

Sub RemoveHighlights()
ActiveDocument.Range.HighlightColorIndex = wdNoHighlight
End Sub

fumei
03-17-2007, 05:58 AM
Good one! Excellent use of the Object Model.

mdmackillop
03-17-2007, 06:04 AM
Thanks Gerry,
I'm still trying to get the hang of this whole "range" thing. Do you know of any good reading/tutorials?

fumei
03-17-2007, 05:11 PM
Ummm...no. But if you like, I can include you in the small group of reviewers I have looking at the Word VBA course I am writing. It is intended to be a five day, instructor led course on advanced Word techniques, primarily focusing on VBA. I have some stuff on Range, but really, it actually is simple.

Range is the document content between two numbers, the Start, and the End. As such, it has the properties and methods applicable to document contents. The Start and End can be any number, with one rule - the End can not be less than the Start.Dim r As Range
Set r = ActiveDocument.Range(Start:=40, End:=2)This will return a compiling error.

A range set to a paragraph includes the paragraph mark. A range set to a table cell does not. This is very important.
Dim r As Range
Set r = ActiveDocument.Paragraphs(2).Range
r.InsertAfter Text:="Help"Will put "Help" at the start of paragraph 3, NOT at the end of paragraph 2. It does exactly as it is told to do. It puts the text ("Help") after the range End, which includes the paragraph mark.

So, if you want to put text at the end of the TEXT of the range, you have to back up the Range .End, like so:Dim r As Range
Set r = ActiveDocument.Paragraphs(2).Range
r.MoveEnd Unit:=wdCharacter, Count:=-1
r.InsertAfter Text:="Help"This puts the inserted text at the end of the Range.Text, but before the paragraph mark.

Obviously if the set Range is not the whole paragraph, then it would not include the paragraph mark, and InsertAfter inserts the text....after.

It works the other way around in tables. Perhaps this may help.

Range of text.

Includes paragraph mark in numerical values of the Range.
Does NOT include paragraph mark in Range.Text

"This is paragraph 2."

Set r = ActiveDocument.Paragraphs(2).Range

r.Text returns "This is paragraph 2."
r.InsertAfter puts text after the paragraph mark - ie. start of paragraph 3.

Range in table:

Does NOT include paragraph mark in numerical values of the Range.
Includes paragraph mark in Range.Text

Text in Cell(2,1) is "Cell(Row 2, Col 1)"

r.Text returns "Cell(Row 2, Col 1)" AND that square symbol for the paragraph mark (actually the end-of-cell marker).

r.InsertAfter will insert the text in the cell, not the next cell (next range).

Weird? Sure, but then...what object model is not?

A paragraph within a range MUST be explicitly ranged in order to use its range.

Say r (as range) has 14 paragraphs.r.Paragraphs(7) does not mean the range of paragraph 7.r.Paragraphs(7).Rangeallows the use of methods and properties for paragraph 7.

r.Paragraphs(7).Text = error
r.Paragraphs(7).Range.Text = text of paragraph 7

Of course r.Text = text of the range.

Simple.

Where things get tricky is when you use Range for fluid objects like formfields, or bookmarks. THAT can get really weird.

A very useful tool for moving a range end is MoveEndUntil. This uses Cset (character set, essentially ASCII) as a built-in search mechanism.

Set r = ActiveDocument.Paragraphs(2).Range
r.MoveEndUntil Cset:="z", Count:=wdForward

Now, you may think wdForward is an odd "count", which of course...it is. But there you go.

The r.End will move forward until it finds the first "z". It stops before the Cset value (that is, it does NOT include the "z").

You can also use an ASCII value, given as Chr.

r.MoveEndUntil Cset:=Chr(12), Count:=wdForwardmoves the end of the range forward to just before the next Page (OR Section) Break.

However, really, it all comes down to numbers. Ranges have a Start and an End. It is trying to figure out where the heck the numbers come from, or how to give it to the range.

tra-la, tra-la.....

In your post, the question was how to remove highlights from the document, and you did it in the most efficient manner possible. You made the document range (that is, the range of the document) have no highlight. Boom. Done.

mdmackillop
03-17-2007, 06:25 PM
Thanks Gerry,
Very useful. I'll reread it a couple of times and play around with it.

I was trying to make use of Range in connection with Find, as in the ZZZ posting and I'm still trying to puzzle out what goes on there with a looping search. I'll get back to you on this in due course.

shekhu
03-18-2007, 08:20 PM
Thanx all,

I am pleased to see great people around here. My purpose is solved. As I had no VBA background, I have been through various forums all this time. Finally, I got the solution here. Thanks again mdmackillop.

shekhu
03-19-2007, 09:24 PM
I am also using this macro to find gender mistakes. As this also is based on find and replace. Is it possible re-code it without using “find and replace”.

Sub Male()
'
' a Macro
' Macro recorded 10/10/05 by Accolade
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "Man's"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run MacroName:="am"
Application.Run MacroName:="bm"
Application.Run MacroName:="cm"
Application.Run MacroName:="dm"
Application.Run MacroName:="em"
Application.Run MacroName:="fm"
Application.Run MacroName:="gm"

End Sub

Sub am()
'
' u Macro
' Macro recorded 10/10/05 by Accolade
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "Gentleman's"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub

Sub bm()
'
' u Macro
' Macro recorded 10/10/05 by Accolade
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "Mr"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub

Sub cm()
'
' u Macro
' Macro recorded 10/10/05 by Accolade
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "man"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub

Sub dm()
'
' u Macro
' Macro recorded 10/10/05 by Accolade
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "male"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub

Sub em()
'
' u Macro
' Macro recorded 10/10/05 by Accolade
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "he"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub

Sub fm()
'
' u Macro
' Macro recorded 10/10/05 by Accolade
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "his"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub

Sub gm()
'
' u Macro
' Macro recorded 10/10/05 by Accolade
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "him"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub

fumei
03-19-2007, 09:58 PM
Not clear on exactly what you are trying to do. It looks like you are trying to find those words and highlight them.

Could you explain the use of .Replacement.Text = "^&"

If that is what you are trying to do, replace those words - well not replace them, but highlight them, you can do it much easier.Dim Genders
Dim var
Genders = Array("Man's", "Gentlemen's", "Mr.", " male ", _
" man ", " he ", " his ", " him ")
With ActiveDocument.Content.Find
For var = 0 To UBound(Genders)
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Text = Genders(var)
.Execute Replace:=wdReplaceAll
Next
End WithThis will highlight all found instances of all the array words in the document. No need to do all that Selection stuff.

Please note the use of spaces before and after "he", "his" etc, in the array. Otherwise the "he" in words such as "the" would be highlighted. The search looks for the string. If the string is "he", then ALL "he" are valid.

However, I have to go back to saying I am not sure what you are trying to do. You mention finding gender "mistakes". WHAT mistakes?????

shekhu
03-19-2007, 10:27 PM
This macro is to find those words and highlight them so that I can relate the gender to the text, as I often comit mistakes in the document. You have done the right thing, and I will try this. Thanx.

shekhu
03-19-2007, 10:37 PM
Fumei, I think this code is incomplete it ends with "End With". Could you please give me the full code.

fumei
03-20-2007, 12:11 AM
Sigh..OK. Here you go, but really...all I am doing is putting in a Sub name to start, and the End Sub to end. This is something you should know.Sub WhateverBlahBlahDoDahWhatever()
Dim Genders
Dim var
Genders = Array("Man's", "Gentlemen's", "Mr.", " male ", _
" man ", " he ", " his ", " him ")
With ActiveDocument.Content.Find
For var = 0 To UBound(Genders)
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Text = Genders(var)
.Execute Replace:=wdReplaceAll
Next
End With
End Sub

shekhu
03-20-2007, 12:43 AM
I have added this to the macro and now no need to leave blanks, is it fine? though I checked it is working well.
.MatchWholeWord = True


Sub Genders()
Dim Genders
Dim var
Genders = Array("Man's", "Gentlemen's", "Mr.", "male", _
"man", "he", "his", "him")
With ActiveDocument.Content.find
For var = 0 To UBound(Genders)
.ClearFormatting
.Replacement.ClearFormatting
.MatchWholeWord = True
.Replacement.Highlight = True
.Text = Genders(var)
.Execute Replace:=wdReplaceAll
Next
End With
End Sub

fumei
03-21-2007, 05:07 AM
That is fine.

shekhu
03-21-2007, 06:13 AM
Genders = Array("Man's", "Gentlemen's", "Mr.", "male", _
"man", "he", "his", "him")
[/vba]

fumei, what does "_" dash means after male. Is it required at the end of every sentence, as I have around 30 words to be put in this macro to search and it will be a big one.

mdmackillop
03-21-2007, 12:30 PM
The "dash" (underscore), preceded by a space lets you break the code onto two or more lines. It is commonly used here to avoid having to use the scroll arrows, which makes following the code difficult.
If you need to split a long line of text, use & as well, as in
txt = "This is too long a line " & _
"of text to display in one line."

fumei
03-21-2007, 08:23 PM
Correct. You could do it as:Genders = Array( _
"Man's", _
"Gentlemen's", _
"Mr.", _
"male", _
"man", _
"he", _
"his", _
"him") Although that is a bit silly.

It is used, as Malcolm mentions, to break up long code lines. Especially here, as the code window is small for those of us without huge monitors and high resolutions.

It is also commonly used to add clarity and ease of reading.

Sub SomeProcedure (strIn As String, _
strThis As String, _
lngCounter As Long, _
strState As String, _
Optional varRegion As Variant, _
Optional varCountry As Variant = "India")These could be written all on one line, but it makes it easier to read breaking it into separate lines.

fumei
03-21-2007, 08:30 PM
I would also like to add that the space/underscore can be used before, or after, a dot.

The syntax is:

object, dot, space, underscore
property/method
ActiveDocument.Bookmarks("test). _
Range.Text
OR

object, space, underscore
dot, property/method
ActiveDocument.Bookmarks("test") _
.Range.TextThey are equivalent. I tend to use the second method, as it is similar to the structure of With statements.With Selection
.GoTo what:=wdGoToBookmark, Name:="test"
.TypeText Text:=strIn
.Collapse Direction:=wdCollapseEnd
.Font.Superscript = True
.InsertSymbol Font:="Symbol", CharacterNumber:=-3870, _
Unicode:=True
.Collapse Direction:=wdCollapseEnd
.Font.Superscript = False
.MoveStart unit:=wdCharacter, Count:=-(Len(strIn) + 1)
End WithIt makes it easier (for me anyway) to see that the dots are correctly placed.