![]() |
#1
|
|||
|
|||
![]()
I receive documents that have following formatting
Lorem ipsum sit amet, [consectetuer adipiscing](elit Maecenas porttitor congue massa) usce posuere magn. I need a macro that will replace whatever is within both the single brackets and the parentheses with enclosure in double brackets, so that the above would become Lorem ipsum sit amet, [[consectetuer adipiscing]][[elit Maecenas porttitor congue massa) usce posuere magn.]] Thank you. |
#2
|
|||
|
|||
![]()
I think you need to revisit your "after" condition as it is not as you described.
|
#3
|
|||
|
|||
![]()
Sorry, and thank you. Correction:
Lorem ipsum sit amet, [[consectetuer adipiscing]][[elit Maecenas porttitor congue massa]] usce posuere magn. Much obliged, Greg |
#4
|
|||
|
|||
![]()
Unless there are hidden complexities that you haven’t explained, that just requires a sequence of find and replace operations.
You can simply record those actions to create a macro you can then edit. |
#5
|
|||
|
|||
![]()
This could likely be refined, but I don't have time right now:
Code:
Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .Text = "[\[\(]*[\]\)]" .MatchWildcards = True .Wrap = wdFindStop While .Execute oRng.Characters.First.InsertBefore Chr(Asc(oRng.Characters.First)) oRng.Characters.Last.InsertAfter Chr(Asc(oRng.Characters.Last)) oRng.Collapse wdCollapseEnd Wend End With Set oRng = ActiveDocument.Range With oRng.Find .Text = "((" .Replacement.Text = "[[" .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range With oRng.Find .Text = "))" .Replacement.Text = "]]" .Execute Replace:=wdReplaceAll End With lbl_Exit: Exit Sub End Sub |
#6
|
|||
|
|||
![]()
Splendid. Thank you very much, sir.
|
#7
|
||||
|
||||
![]()
It could be a single find/replace
Code:
Sub Macro1() With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "([\(\[])(*)([\)\]])" .Replacement.Text = "[[\2]]" .MatchWildcards = True .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchKashida = False .MatchDiacritics = False .MatchAlefHamza = False .MatchControl = False .MatchAllWordForms = False .MatchSoundsLike = False .Execute Replace:=wdReplaceAll End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
![]()
Thanks Andrew,
Tweaking your code a bit, I think we can prevent unwanted growth of the dbl brackets if the code is repeated. Code:
Sub Macro1() Dim oRng As Range Set oRng = ActiveDocument.Range oRng.InsertBefore "*" With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "([!\[\(])([\[\(])([!\[\(]*)([\]\)])([!\]\)])" .Replacement.Text = "\1[[\3]]\5" .MatchWildcards = True .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchKashida = False .MatchDiacritics = False .MatchAlefHamza = False .MatchControl = False .MatchAllWordForms = False .MatchSoundsLike = False .Execute Replace:=wdReplaceAll End With ActiveDocument.Characters(1).Delete MsgBox Asc(ActiveDocument.Characters.Last.Previous) If ActiveDocument.Characters.Last.Previous = Chr(13) Then ActiveDocument.Characters.Last.Previous.Delete End If lbl_Exit: Exit Sub End Sub |
#9
|
|||
|
|||
![]()
Thank you both very much; I'm very grateful for your generous expertise.
I have two questions. 1. Will the latest version perform if any of the text is already hyperlinked? I ran Greg's previously-latest version this morning, and found that, where the URL in parentheses was already a hyperlink, the parens were not changed to double brackets. 2. There happened to be in the same article an already-bracketed, but stand-alone comment not related to a url nor followed by one in parentheses. This also got double brackets. The second issue is certainly minor and not worth bothering with, if it would make the code too fussy (technical term). The first is more the issue, if it is one in the latest version. Again, my genuine thanks. |
#10
|
|||
|
|||
![]()
I tried running the latest, "Macro1" with the above described condition, in which the URL is already hyperlinked. I get a pop-up and the document hangs. When I use escape to dismiss the pop-up, double brackets are placed around the already bracketed term; the parens remain unchanged.
Please see attachment. (The box says 46. The first time it said 13.) Can this be easily fixed in the macro? Thank you. |
#11
|
|||
|
|||
![]()
As it is a simple matter to perform a universal hyperlink de-linking, I am marking this thread solved, with thanks to macro writers.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Ulodesk | Word VBA | 2 | 02-06-2020 06:49 AM |
![]() |
rsrasc | Excel Programming | 2 | 02-12-2019 03:42 PM |
![]() |
PokerBob | Excel | 8 | 03-18-2015 02:57 PM |
Macro Needed to bold specific lines and Macro to turn into CSV | anewteacher | Word VBA | 1 | 05-28-2014 03:59 PM |
Check box macro help needed | Aflac | Word | 4 | 03-24-2012 07:11 PM |