Consulting

Results 1 to 18 of 18

Thread: Solved: Coding a new macro for Un-highlighting

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    79
    Location

    Question Solved: Coding a new macro for Un-highlighting

    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:

    [vba]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[/vba]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    How about
    [VBA]
    Sub RemoveHighlights()
    ActiveDocument.Range.HighlightColorIndex = wdNoHighlight
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Good one! Excellent use of the Object Model.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Gerry,
    I'm still trying to get the hang of this whole "range" thing. Do you know of any good reading/tutorials?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.[vba]Dim r As Range
    Set r = ActiveDocument.Range(Start:=40, End:=2)[/vba]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.
    [vba]Dim r As Range
    Set r = ActiveDocument.Paragraphs(2).Range
    r.InsertAfter Text:="Help"[/vba]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:[vba]Dim r As Range
    Set r = ActiveDocument.Paragraphs(2).Range
    r.MoveEnd Unit:=wdCharacter, Count:=-1
    r.InsertAfter Text:="Help"[/vba]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.[vba]r.Paragraphs(7)[/vba] does not mean the range of paragraph 7.[vba]r.Paragraphs(7).Range[/vba]allows 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.

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

    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.

    [vba]r.MoveEndUntil Cset:=Chr(12), Count:=wdForward[/vba]moves 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.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Aug 2006
    Posts
    79
    Location
    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.

  8. #8
    VBAX Regular
    Joined
    Aug 2006
    Posts
    79
    Location
    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”.

    [vba]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
    [/vba]

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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 [vba].Replacement.Text = "^&" [/vba]

    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.[vba]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[/vba]This 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?????

  10. #10
    VBAX Regular
    Joined
    Aug 2006
    Posts
    79
    Location
    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.

  11. #11
    VBAX Regular
    Joined
    Aug 2006
    Posts
    79
    Location
    Fumei, I think this code is incomplete it ends with "End With". Could you please give me the full code.

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.[vba]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[/vba]

  13. #13
    VBAX Regular
    Joined
    Aug 2006
    Posts
    79
    Location
    I have added this to the macro and now no need to leave blanks, is it fine? though I checked it is working well.
    [vba].MatchWholeWord = True[/vba]


    [vba]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[/vba]

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    That is fine.

  15. #15
    VBAX Regular
    Joined
    Aug 2006
    Posts
    79
    Location
    Quote Originally Posted by shekhu
    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.

  16. #16
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    [VBA]txt = "This is too long a line " & _
    "of text to display in one line."[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Correct. You could do it as:[vba]Genders = Array( _
    "Man's", _
    "Gentlemen's", _
    "Mr.", _
    "male", _
    "man", _
    "he", _
    "his", _
    "him") [/vba]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.

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

  18. #18
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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
    [vba]ActiveDocument.Bookmarks("test). _
    Range.Text[/vba]
    OR

    object, space, underscore
    dot, property/method
    [vba]ActiveDocument.Bookmarks("test") _
    .Range.Text[/vba]They are equivalent. I tend to use the second method, as it is similar to the structure of With statements.[vba]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 With[/vba]It makes it easier (for me anyway) to see that the dots are correctly placed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •