A couple of years back, when I first discovered macros, I kinda fell a bit in love with them. In particular I was entranced by the Visual Basic Editor packaged with Microsoft Office for the purpose of editing macros, or even writing them from scratch, bypassing the rather naff macro recorder. Long before I had a clue how to even begin to decipher the inner workings of these magical text-spells, let alone edit one myself, I was happily pillaging the internet for complete projects offered up by those sorcerers who do know how to create conjurations with this Clarkian magic. Like any denizen of Arthur C Clarke's magical kingdom, I may not know how to bind the spirits to a purpose, but when a friendly magician gives me the already-imbued Ring Of Invisibility and tells me the magic word, I can use the fruits of her labour with ease.
So, anyway, ruminations on the impenetrability of most of the technology around us aside, one of the first, and subsequently most useful spells I found was a macro for highlighting any text between two defined characters or strings thereof. (F'rinstance, if I want to change all the quotation-marks in a piece to their html entities, but the piece includes html tags, I quite obviously don't want to change the quotation-marks within those tags. So I copy the whole lot, paste it into M/S Word, and use the macro to embolden all text between < and >. Then I simply replace all non-bold quotation-marks with ". Copy it all back to the original document; job done.)
One thing about this macro that always bugged me a little bit was that, as written, you have to manually edit it in order to change its parameters. (But gift-horses, mouths, etc; please don't think I was or am in any way ungrateful to Maniac, the person who wrote the macro!) You have to change the start and end strings between which text will be highlighted and if you want to change the way the text is highlighted (make it bold, make it a particular colour, struck-through, "actual" highlighting, whatever) you have to change that too.
Once I got enough know-how to experiment, I added in my most commonly used sets of characters and formatting, with each "commented out" so that most times I used it, I merely had to add and delete a few apostrophes where appropriate. But, well, wouldn't it be nice to be able to use it from a custom button on the toolbar and input the needed variables into an input form which pops up?
Well, I managed to get it to do that, though I did it in stages. The first part—getting it to pop up forms asking for the start and end strings—was fairly easy. And, to be honest, getting it to accept formatting instructions wasn't a helluva lot harder; but it took me a while to realise that I could do that without it involving a whole series of Y/N boxes which would make the process of making choices longer and more annoying than manual editing. But I got there in the end. It's kind of intermediary, as a tick-box form would be both more polished and easier to use, not to mention that it would probably make it a lot easier to include more choices. Problem is, I've not yet figured out how to make a tick-box form add the needed info into the macro. Still an' all, I reckon this solution—which does have the advantage that unlike the putative tick-box solution, it's all contained in one macro—is worth sharing. So here it is. (Instructions on how to use it will follow.)
Sub FindTextBetweenTwoStrings() ' ' Find text between two strings ' Selection.HomeKey Unit:=wdStory Dim pFormat As String pFormat = InputBox("Format:" _ & vbCrLf & "Bold: 'B'" _ & vbCrLf & "Colour: 'R'(ed)/ 'G'(reen)/ 'Bl'(ue)/ 'P'(ink)" _ & vbCrLf & "Underline: 'U'" _ & vbCrLf & "---------------------------------" _ & vbCrLf & "Double-strikethrough: 'S'" _ & vbCrLf & "Highlight: 'H'") If pFormat = "" Then Exit Sub End If pFormat = LCase(pFormat) Dim StartString As String Dim EndString As String StartString = InputBox("Enter start-string.") If StartString = "" Then Exit Sub End If EndString = InputBox("Enter end-string.") If EndString = "" Then Exit Sub End If ActiveDocument.Range(0).Select Selection.Find.ClearFormatting While Selection.Find.Execute(StartString) StartReformat = Selection.End Selection.MoveRight Selection.Find.Execute (EndString) StopReformat = Selection.Start Selection.MoveRight With ActiveDocument.Range(StartReformat, StopReformat) If pFormat = "bru" Then .Bold = True .Font.Color = wdColorRed .Underline = True End If If pFormat = "br" Then .Bold = True .Font.Color = wdColorRed End If If pFormat = "ru" Then .Underline = True .Font.Color = wdColorRed End If If pFormat = "r" Then .Font.Color = wdColorRed End If If pFormat = "bgu" Then .Bold = True .Font.Color = wdColorGreen .Underline = True End If If pFormat = "bg" Then .Bold = True .Font.Color = wdColorGreen End If If pFormat = "gu" Then .Underline = True .Font.Color = wdColorGreen End If If pFormat = "g" Then .Font.Color = wdColorGreen End If If pFormat = "bblu" Then .Bold = True .Font.Color = wdColorBlue .Underline = True End If If pFormat = "bbl" Then .Bold = True .Font.Color = wdColorBlue End If If pFormat = "blu" Then .Font.Color = wdColorBlue .Underline = True End If If pFormat = "bl" Then .Font.Color = wdColorBlue End If If pFormat = "bpu" Then .Bold = True .Font.Color = wdColorPink .Underline = True End If If pFormat = "bp" Then .Bold = True .Font.Color = wdColorPink End If If pFormat = "pu" Then .Font.Color = wdColorPink .Underline = True End If If pFormat = "p" Then .Font.Color = wdColorPink End If If pFormat = "bu" Then .Bold = True .Underline = True End If If pFormat = "b" Then .Bold = True End If If pFormat = "u" Then .Underline = True End If If pFormat = "s" Then .Font.DoubleStrikeThrough = True End If If pFormat = "h" Then .HighlightColorIndex = wdBrightGreen End If End With Wend Selection.HomeKey Unit:=wdStory End Sub
When you run the macro, the first thing you'll be presented with is a dialog box asking for your formatting choices, like this:
(Okay, it may look a bit flashier on your screen if, unlike me, you're not stuck in the dark-ages and clinging to the "Windows Classic" theme like grim death.)
The first three, Bold, Colours and Underlined, can be combined. They must be typed in that order, so bblu will make the found text bold (b), blue (bl) and underlined (u). Or ru gets you red-underlined, bg produces bold-green, bu makes it bold-underlined, p pink, and so on. The two below the line, Double-strikethrough (s) and Highlight (h), do what they describe, but can't be combined with each other or the three above. (I'll be happy to provide tweaks for other combinations of three things, add/change colours and add single attributes if asked.)
Click OK (or hit Enter; the OK button is, fortuitously, selected by default) and that box will close, and the Start-string box will open. Type in the character(s) you want to precede the highlighted text, OK it and fill in the End-string box which follows on its heels. OK that, and the macro-proper will run, highlighting all text found between the strings you chose.
Example: br in the first box, < in the second and > in the third would make all text inside html tags bold and red.
Clicking Cancel on any of the boxes will exit you from the macro without making any changes. A letter-combination in the first, format-choice, box which the macro doesn't recognise will result in the macro running but making no changes to the document.
If anyone is unsure how to add a macro by pasting it into the Visual basic Editor, ask in comments and I'll explain there.
To make a macro run from a button in the toolbar, Microsoft have a handy guide.
And that just about wraps it up for this post. I hope, as ever, that this has been of some use to someone.
You may use these HTML tags in comments
<a href="" title=""></a> <abbr title=""></abbr>
<acronym title=""></acronym> <blockquote></blockquote> <del></del>* <strike></strike>† <em></em>* <i></i>† <strong></strong>* <b></b>†
* is generally preferred over †