Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2024, 06:37 AM
Ulodesk Ulodesk is offline Needed macro, please Windows 10 Needed macro, please Office 2021
Word 2013 Expert Cert
Needed macro, please
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default Needed macro, please

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.
Reply With Quote
  #2  
Old 07-13-2024, 09:03 AM
gmaxey gmaxey is offline Needed macro, please Windows 10 Needed macro, please Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I think you need to revisit your "after" condition as it is not as you described.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-14-2024, 03:51 PM
Ulodesk Ulodesk is offline Needed macro, please Windows 10 Needed macro, please Office 2021
Word 2013 Expert Cert
Needed macro, please
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default

Sorry, and thank you. Correction:

Lorem ipsum sit amet, [[consectetuer adipiscing]][[elit Maecenas porttitor congue massa]] usce posuere magn.

Much obliged, Greg
Reply With Quote
  #4  
Old 07-14-2024, 07:09 PM
Italophile Italophile is offline Needed macro, please Windows 11 Needed macro, please Office 2021
Expert
 
Join Date: Mar 2022
Posts: 554
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Unless there are hidden complexities that you haven’t explained, that just requires a sequence of find and replace operations.
  1. Find [ replace with [[
  2. Find ] replace with ]]
  3. Find ( replace with [[
  4. Find ) replace with ]]

You can simply record those actions to create a macro you can then edit.
Reply With Quote
  #5  
Old 07-15-2024, 04:28 AM
gmaxey gmaxey is offline Needed macro, please Windows 10 Needed macro, please Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 07-15-2024, 06:16 AM
Ulodesk Ulodesk is offline Needed macro, please Windows 10 Needed macro, please Office 2021
Word 2013 Expert Cert
Needed macro, please
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default

Splendid. Thank you very much, sir.
Reply With Quote
  #7  
Old 07-15-2024, 04:24 PM
Guessed's Avatar
Guessed Guessed is offline Needed macro, please Windows 10 Needed macro, please Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #8  
Old 07-16-2024, 09:36 AM
gmaxey gmaxey is offline Needed macro, please Windows 10 Needed macro, please Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 07-19-2024, 06:58 AM
Ulodesk Ulodesk is offline Needed macro, please Windows 10 Needed macro, please Office 2021
Word 2013 Expert Cert
Needed macro, please
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default

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.
Reply With Quote
  #10  
Old 07-20-2024, 06:35 AM
Ulodesk Ulodesk is offline Needed macro, please Windows 10 Needed macro, please Office 2021
Word 2013 Expert Cert
Needed macro, please
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default Addendum

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.
Attached Images
File Type: png Result.png (51.8 KB, 17 views)
Reply With Quote
  #11  
Old 07-25-2024, 06:38 AM
Ulodesk Ulodesk is offline Needed macro, please Windows 10 Needed macro, please Office 2021
Word 2013 Expert Cert
Needed macro, please
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default

As it is a simple matter to perform a universal hyperlink de-linking, I am marking this thread solved, with thanks to macro writers.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Needed macro, please Macro adjustment needed Ulodesk Word VBA 2 02-06-2020 06:49 AM
Needed macro, please Macro Needed with RGB rsrasc Excel Programming 2 02-12-2019 03:42 PM
Needed macro, please ***help*** macro needed 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:51 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft