Consulting

Results 1 to 7 of 7

Thread: Play with Highlited text

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location

    Play with Highlited text

    Hi Team

    I want Word macro to search for highlighted words, then after find do the below:

    1- after the highlighted found, add a space then (
    2- past the highlighted found earlier but clear its highlight
    3- add closing bracket )
    4- select all new added text which is (the space + the opening bracket + the pasted text + the closing bracket) then make them Hidden.
    5- move to the next.

    Please have a look at the below picture (or see the attached file) to give you an idea about what i need, it shows the line/text before and after the needed macro.

    Image 2021-08-08 at 9.14.05 AM.jpg
    Attached Files Attached Files
    Last edited by Paul_Hossler; 08-09-2021 at 02:28 PM.

  2. #2
    How about

    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .Highlight = True
            Do While .Execute
                With oRng
                    .InsertBefore "("
                    .InsertAfter ")"
                    .HighlightColorIndex = wdNoHighlight
                    .Collapse 0
                End With
            Set oRng = Nothing
        End With
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Thanks Graham Mayor for the code, but I see that it’s not working as expected, please let me list the issues below and forgive my as I’ve no experience with VBA macros:


    • When I run your code I received an error message and nothing happened:


    1.jpg



    • Then I tried to add a Loop, but I got a different result that what expected:



    2.jpg


    • I see that the original text find for the highlighted words was removed and only the brackets were added, while I need the original highlighted words to be still there but copy then after between brackets then make hidden as shown below (before & after):


    3.jpg

  4. #4
    Something went amiss with the copy/paste
    Let's try again
    Sub Macro1()
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .Highlight = True
            Do While .Execute
                With oRng
                    .InsertBefore "("
                    .InsertAfter ")"
                    .HighlightColorIndex = wdNoHighlight
                    .Collapse 0
                End With
            Loop
            Set oRng = Nothing
        End With
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Thanks for looking into this, it's working now with no error, but I see that the original highlighted text was removed and only the brackets were added, while I need the original highlighted words to be still there and copy them after (repeat) between brackets then mark the added text as NOT highlighted and Hidden as shown on the screenshot below (before & after):

    https://ibb.co/6FvDfn7

  6. #6
    OK that would need a minor change e.g.

    Sub Macro1()
    Dim oRng As Range
    Dim sText As String
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .Highlight = True
            Do While .Execute
                With oRng
                    sText = .Text
                    .Collapse 0
                    .Text = " (" & sText & ")"
                    .HighlightColorIndex = wdNoHighlight
                    .Collapse 0
                End With
            Loop
            Set oRng = Nothing
        End With
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Dec 2015
    Posts
    89
    Location
    Thanks a lot Graham Mayor for your efforts and helping me, that's much appreciated.

Posting Permissions

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