-
2 Attachment(s)
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.
Attachment 28823
http://www.vbaexpress.com/forum/imag...BJRU5ErkJggg==
-
How about
Code:
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
-
3 Attachment(s)
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:
Attachment 28825
http://www.vbaexpress.com/forum/imag...AAAElFTkSuQmCC
- Then I tried to add a Loop, but I got a different result that what expected:
http://www.vbaexpress.com/forum/imag...AAAElFTkSuQmCC
Attachment 28826
- 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):
Attachment 28827
http://www.vbaexpress.com/forum/imag...BJRU5ErkJggg==
-
Something went amiss with the copy/paste :banghead:
Let's try again
Code:
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
-
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
-
OK that would need a minor change e.g.
Code:
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
-
Thanks a lot Graham Mayor for your efforts and helping me, that's much appreciated.