MS WORD (VBA) Remove just some BB Code tag pairs in text.

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by DocAElstein »

Hello
I have asked some similar questions before, and usually I get these things eventually. But wild card type things are some strange exception with me : I must have a real psychological problem with them: When I think about them my brain seriously flips out and I can’t think of anything for the rest of the day… :(

Can anyone please help put me out of my misery.

Lets say I have some text like this in word

Code: Select all

Ahdahdjjkkjh  [COLOR=black][b]This will usually be black anyway[/b], not [color=orange]orange[/color], ([/COLOR] [color=purple]Purple[/color] [COLOR=black] is my favourite ) so I really don’t need the black color BB Code tag pairs [/COLOR] shfdlkfahfafhhfhf
, and I have highlighted text with some redundant black BB code tag pairs that I want to get rid of:
https://i.postimg.cc/mr1VXK9c/Highlight ... -pairs.jpg
Highlighted text with redundant black BB code tag pairs.JPG
I would like VBA code to get rid of just the black BB code tag pairs but not mess up any of the other BB code tags. So the result I want after running the code is like
https://i.postimg.cc/7Yd1TfF7/Redundant ... emoved.jpg
Redundant black BB code tag pairs removed.JPG

Code: Select all

Ahdahdjjkkjh  [b]This will usually be black anyway[/b], not [color=orange]orange[/color], ( [color=purple]Purple[/color]  is my favourite ) so I really don’t need the black color BB Code tag pairs  shfdlkfahfafhhfhf


Thanks
Alan

( P.S. I have German Office and my separator is a semi colon
;
instead of a comma
,
I think that sometimes chucks a spanner in the works… )

Edit: P.S. 2 I suppose if anyone knows how to do it manually with the find replace dialogue stuff, then it may be possible to get some coding from the macro recorder
You do not have the required permissions to view the files attached to this post.
Last edited by DocAElstein on 03 Oct 2022, 08:46, edited 1 time in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by StuartR »

I think you can do this by replacing
Find what wrote:\[COLOR=black\](*)\[/COLOR\]
with
Replace with wrote:\1
You do not have the required permissions to view the files attached to this post.
StuartR


User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by DocAElstein »

Thanks Stuart, Great, that did it, ( and the macro recorder got me an initial coding. )

StuartsWithMacroRecorder.JPG
Image

Thanks
I will one day sit down for a (very) long time and try to get all this wildcard syntax clear in my head. I definitely have a strange weird problem in thinking about these things, not sure why…
_._______________________________________________________________________________________

Code from doing it with macro recorder on:

Code: Select all

Sub RemoveBlackBBCodeTagPairs() ' https://eileenslounge.com/viewtopic.php?p=299529#p299529
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
    With Selection.Find
     .Text = "\[COLOR=black\](*)\[/COLOR\]"
     .Replacement.Text = "\1"
     .Forward = True
     .Wrap = wdFindAsk
     .Format = False
     .MatchCase = False
     .MatchWholeWord = False
     .MatchKashida = False
     .MatchDiacritics = False
     .MatchAlefHamza = False
     .MatchControl = False
     .MatchAllWordForms = False
     .MatchSoundsLike = False
     .MatchWildcards = True
    End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
That can almost certainly be simplified I expect, but I will leave it like that for now, as i am not too sure about WORD VBA stuff

_.______________________

Ref https://excelfox.com/forum/showthread.p ... #post16731
You do not have the required permissions to view the files attached to this post.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15587
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by ChrisGreaves »

StuartR wrote:
03 Oct 2022, 08:45
I think you can do this by replacing ...
Thank you, Stuart :clapping: :clapping: :clapping:
Would you mind if I borrowed this structure to add to the B, U, I and Q toolbar buttons on my toolbar?
I could name the new buttons in your honour (1), or else just resort to -B, -U, -I and -Q.
Thanks in keen anticipation.
Chris
(1) Which could now be Americanised with honor! C
There's nothing heavier than an empty water bottle

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by DocAElstein »

Yes, it’s very useful, as are a lot of things you can do with wild card things, but trying to get the correct syntax always sends me (even more ) mad.
I think you can do similar things with some “RegEx” pattern matching coding ideas, which are at least as puzzling to me. But they seem to fall down a bit on larger data, at least i found that with Excel VBA “RegEx” pattern matching coding ideas, whereas I expect the find replace thing is a fairly old, fundamental and so maybe fairly reliable efficient thing, ( maybe not unlike yourself, Chris :) ). Just guessing
Last edited by DocAElstein on 03 Oct 2022, 10:23, edited 1 time in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
HansV
Administrator
Posts: 78394
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by HansV »

A slightly streamlined version:

Code: Select all

Sub RemoveBlackBBCodeTagPairs() ' https://eileenslounge.com/viewtopic.php?p=299529#p299529
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "\[COLOR=black\](*)\[/COLOR\]"
        .Replacement.Text = "\1"
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub
Best wishes,
Hans

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by DocAElstein »

Thanks Hans, that’s useful to know, as I thought something like that, but lacking experience with WORD VBA I was not sure.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by StuartR »

ChrisGreaves wrote:
03 Oct 2022, 09:55
Would you mind if I borrowed this structure to add to the B, U, I and Q toolbar buttons on my toolbar?
I could name the new buttons in your honour (1), or else just resort to -B, -U, -I and -Q.
Thanks in keen anticipation.
Chris
You don't need permission to reuse anything posted here in the lounge, but it might be polite to include a credit to the lounge, as Hans did in his version of the code
StuartR


User avatar
HansV
Administrator
Posts: 78394
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by HansV »

(Actually, I copied DocAElstein's code)
Best wishes,
Hans

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by DocAElstein »

I always do Stuart, in the code ( Hans copied / modified my code version which always has the referrence in - Hans did not put that there, I did), and I mentioned you, Hans ans Eileen's Lounge a few times in the post I referrenced as soon as I got your solutions.... :)
(Also the solutions are only for me. The post I referrenced is just a convenient place ( a spare test sub forum that no one but me ever goes to) where I store stuff for myself. If I had not given that referrence no one would ever have known about it :) ( But now you will all be famous to the traveling crawling Bots, my test post there get ridiculous numbers of views by Bots, never figured out why, the word Test seems to appeal to the Bots... )

Edit: Oops, Sorry Stuart, I see you were talking to Chris, not me!
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by StuartR »

Here is an explanation of that Wildcard find/replace to help you in future...

Code: Select all

\[COLOR=black\](*)\[/COLOR\]
\[ and \] are how you inclulde [ and ] in a find string, because square brackets would otherwise have a special meaning
* means any text of any length
SO

Code: Select all

[COLOR=black]*[/COLOR]
will find the opening and closing tags, with any text in between them
putting () round something in the find box lets you refer to it in the replace box. The first () is referenced as \1 the next () is \2 etc
SO

Code: Select all

[COLOR=black](*)[/COLOR]
allows you to put \1 in the replace with box to mean all the text in between the tags
StuartR


User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by DocAElstein »

Thanks Stuart.
( I once had a strange dream to cure my strange ignorance of these things whereby I kidnapped some people that I knew understood some of these solutions and imprisoned them in one of the many deep holes in my garden until they revealed their secrets… Then I would make a very concise blog on it, monetize it, then use the money to compensate them should they have survived the very cold winter in the hole.
……As I mentioned, these wild card things seem to send me a bit mad)
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by DocAElstein »

( Of course there can be a small downside sometimes to always referencing the Lounge… I know a few people in the past followed my references here, .. and stayed. .. may not have always proved so desirable.
I try to be a bit more careful these days. The majority of links I post are to help me find stuff in my chaotic messy way of storing stuff )
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by StuartR »

:laugh:
StuartR


User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by SpeakEasy »

>they seem to fall down a bit on larger data, at least i found that with Excel VBA “RegEx” pattern matching

That may be true of the Excel built-in almost RegExp - but the full proper scripting RegExp (Microsoft VBScript Regular Expressions Library 5.5) works find with huge amounts of text. On the other hand, it is also probably even more arcane if you are not familiar with it ...

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by macropod »

DocAElstein wrote:
03 Oct 2022, 10:04
it’s very useful, as are a lot of things you can do with wild card things, but trying to get the correct syntax always sends me (even more ) mad.
I think you can do similar things with some “RegEx” pattern matching coding ideas, which are at least as puzzling to me
Unlike RegEx, though, Word's wildcard Find/Replace respects formatting (as does a regular Find/Replace) and can be used to change formatting.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by DocAElstein »

Thx SpeakEasy/Paul for the extra input/info
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(


User avatar
DocAElstein
4StarLounger
Posts: 580
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by DocAElstein »

Thx for the info Charles.
There is a lot of stuff to go through and I expect in the past I went wrong as I simply did not realise that I am going to need a lot of time to sit down and get clued up on it all, but hopefully I will find the time some day.
I ordered the Jack Lyon book. ( I found one on ebay, - amazon, as in the link you gave, does not seem to work at all well here in Germany, especially if ordering books from the states, there is always some complicated bureaucratic chaos at the local tax/custom office, or worse, the local anti terrorist squad demand an official translation, as books are so out of fashion they think it must be part of some dodgy coded communication ). I did see that some of the links you gave show that one can get that Jack Lyon book as a free download. But I occasionally still like to have something physical in the hands, for example it could suit a bit of night time reading occasionally: if I can’t sleep it will make good use of the time, and if it does do my brain in again, then that sometimes has the effect of encouraging me to fall asleep, so one way or another the book will be useful!

The first link from you is one of 3 good links I already have that Hans gave me in one of my earlier wildcard threads. Here is the other two for completeness/future reference:
https://support.office.com/en-US/articl ... 4173D3D6E4
http://www.gmayor.com/replace_using_wildcards.htm

I will also add the links to those earlier threads of mine for future reference as Paul, Hans and Stuart gave me some good explanations there. ( I am not quite sure about my postings there…. I think I was new/ late starting to computers , internet, and communication on the internet and was a bit screwy, - either that or I was on something pretty potent at the time, Lol )

http://www.eileenslounge.com/viewtopic.php?f=26&t=38744
http://www.eileenslounge.com/viewtopic. ... 44#p175744
http://www.eileenslounge.com/viewtopic.php?f=26&t=26030
http://www.eileenslounge.com/viewtopic. ... 22#p202322
http://www.eileenslounge.com/viewtopic. ... 76#p276676

_._____

( The last link of yours, Charles, is not working for me. I tried on a few computers and systems, and also tried again after registering an account at that MS office forum , but just get this: https://i.postimg.cc/MG42C9sV/Search-Got-Nothing.jpg )
Search GotNothing.JPG
Search Got Nothing.JPG

Alan
You do not have the required permissions to view the files attached to this post.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
Charles Kenyon
5StarLounger
Posts: 609
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: MS WORD (VBA) Remove just some BB Code tag pairs in text.

Post by Charles Kenyon »

You are correct as to the last one not working. It was a search of a forum for "advanced find and replace."
https://www.msofficeforums.com/word/
00 deleteme 1.png
You do not have the required permissions to view the files attached to this post.