MS Office Forum / Word / Programming / February 2006
How can I make all text between '/' and '/' bold?
|
|
Thread rating:  |
BonRouge - 10 Feb 2006 10:25 GMT I'm writing something about phonetics and pronunciation. I've been cutting and pasting all the symbols. It's too much trouble to make them bold as I go through. I'd like to find all the things like this '/s/' in a document and make them bold. I think this must be possible, but I have no idea how to do it.
I'm using MS Word 2002.
Thanks for any help.
Tiago - 10 Feb 2006 11:17 GMT In VBA, the following should do:
With Selection.Find .ClearFormatting .MatchWildcards = True .Text = "/*/" End With Do With Selection.Find .Execute(Forward:=True) If .Found Then Selection.Range.Bold = True Selection.Collapse Direction:=wdCollapseEnd Else Exit Do End If End With Loop
I have not tested it, so fix any compile errors that may occur.
Or just use the Find dialog ;)
Hope this helps
Helmut Weber - 10 Feb 2006 13:41 GMT Hi,
this looks simple and it is simple, indeed, but only if you can guarantee that slashes are never used somewhere else in you doc and that there are no typos concerning slashes.
Otherwise you will mess up all of your doc.
First count the number of slashes. Searching and replacing "/" by "/" tells you the number of occurences. The number must be even!
Under these preconditions you may format anything _between_ slashes as bold, like this:
Sub test10() Dim rDcm As Range Set rDcm = ActiveDocument.Range With rDcm.Find .Text = "/*/" .MatchWildcards = True While .Execute rDcm.Start = rDcm.Start + 1 rDcm.End = rDcm.End - 1 ' for testing in single step mode only [F8] rDcm.Select ' delete after testing rDcm.Font.Bold = True rDcm.Start = rDcm.End + 1 rDcm.End = ActiveDocument.Range.End Wend End With End Sub
If that doesn't work because of slashes used in different ways, you may restrict the search to a certain number of characters between slashes.
HTH
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
BonRouge - 11 Feb 2006 15:36 GMT Helmut, Thanks very much. That worked great. Cheers, Steve
BonRouge - 12 Feb 2006 07:20 GMT > You may restrict > the search to a certain number of characters > between slashes. Helmut, I haven't had a problem with this code as yet, but, just so that I know - how would I restrict this to a certain number of characters between slashes - for example, a maximum of 3?
Helmut Weber - 12 Feb 2006 12:09 GMT Hi BonRouge,
like this:
Sub test10a() Dim rDcm As Range Set rDcm = ActiveDocument.Range With rDcm.Find .Text = "/*/" .MatchWildcards = True While .Execute rDcm.Start = rDcm.Start + 1 rDcm.End = rDcm.End - 1 ' for testing in single step mode only [F8] rDcm.Select ' delete after testing If Len(rDcm) < 4 Then ' <<<<<<<< rDcm.Font.Bold = True End If rDcm.Start = rDcm.End + 1 rDcm.End = ActiveDocument.Range.End Wend End With End Sub
There may be a way to use wildcards to restrict the search pattern to something like "/ 1-3 characters /". Which in theory maybe faster.
But as it is so simple to check the length of the found range, and sometimes to difficult, at least for me, to built complex search patters, which sometimes despite all efforts behave not as expected, I've chosen, what I think is the simplest methode.
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
BonRouge - 12 Feb 2006 14:37 GMT Thanks again. I appreciate it.
BonRouge - 13 Feb 2006 12:31 GMT Helmut, I'm back again. I wonder if you'd know how to go a step further than this... Besides wanting individual phonemes in bold, there are also lots of words written phonetically that I want to be bold. I wonder if I could make any word with certain characters in be bold...? I could make a list of characters to search for and when one is found, the word should be bold. What do you think? Sorry if I'm asking too much here. I'd greatly appreciate the help though.
Helmut Weber - 15 Feb 2006 05:03 GMT Hi Steve,
to the best of my knowledge,
you could search for a certain character, from a certain font. One after the other, getting the characters from an array or a text-file, and then bold the first word in the found range.
In case the font is a decorative font, like symbol, then it's getting really tough, as whether you find it depends on whether the character was inserted by the insert-symbol-dialog or whether the character was formatted directly. If inserted by the insert-symbol-dialog then you'd have to search for "(", invoke the insert-symbol-dialog and check the font type, which is displayed there.
Forget about it, to set up a programmtic solution will take more time than doing it by hand, IMHO.
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
>Helmut, >I'm back again. [quoted text clipped - 6 lines] >Sorry if I'm asking too much here. >I'd greatly appreciate the help though. BonRouge - 15 Feb 2006 05:36 GMT Well, that's a shame...
I'm not using the insert symbol thing - I'm just cutting and pasting (as I don't know how to type these characters). I'm doing the whole document in the same font - CHARIS SIL. I figured it would involve some kind of array. I don't know if this info makes it any easier... It's a pity though - I wish I knew more myself. What's the best resource to find out how to program in Word? I'm not a complete novice - I can make web pages and use javascript and php, maybe I could find an answer...
Helmut Weber - 15 Feb 2006 11:41 GMT Hi Steve,
the following would search for pairs of slashes, checks whether there are 3 or fewer characters between the slashes, and if so, checks whether there is at least one of the characters included in the search pattern .Text = "[lyz]{1,}" in the found range, and if so, formats the first word there bold.
You may have to do some error checking in addition.
Sub test10b() Dim rDcm As Range Dim rTmp As Range Dim lEnd As Long ' end position Set rDcm = ActiveDocument.Range Set rTmp = ActiveDocument.Range
With rDcm.Find .Text = "/*/" .MatchWildcards = True While .Execute lEnd = rDcm.End rDcm.Start = rDcm.Start + 1 rDcm.End = rDcm.End - 1 ' for testing in single step mode only [F8] rDcm.Select ' delete after testing If Len(rDcm) < 4 Then rTmp.Start = rDcm.Start rTmp.End = rDcm.End With rTmp.Find .Text = "[lyz]{1,}" .MatchWildcards = True If .Execute Then rTmp.Words(1).Font.Bold = True End If End With End If rDcm.Start = lEnd + 1 rDcm.End = ActiveDocument.Range.End Wend End With End Sub
HTH
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
BonRouge - 15 Feb 2006 14:39 GMT Helmut, thanks for the reply, but perhaps I haven't been clear enough.
In many places in my document, I have things like this : /s/. The 's' is often a phoneme. I want all of those bold. The code you gave previously did that. Thank you very much.
Many other places in my document have words spelt out phonetically - with phonetic symbols. These words are not enclosed in slashes, but I want them bold too.
Sorry if I didn't explain clearly enough before.
I would appreciate a pointer to a good resource where I could perhaps teach myself a thing or two if possible.
Thanks again.
Steve
Helmut Weber - 15 Feb 2006 14:53 GMT Hi Steve,
http://word.mvps.org/faqs/general/UsingWildcards.htm
plus a lot more here posted by friends and me on using ranges.
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Greg - 15 Feb 2006 13:41 GMT Helmut,
Why this line? rDcm.End = ActiveDocument.Range.End
Code seems to work with or without it.
Helmut Weber - 16 Feb 2006 08:30 GMT Hi Greg,
> Why this line? > rDcm.End = ActiveDocument.Range.End > Code seems to work with or without it. Indeed. Seems to be one of that bad habits on my side which are hard to break,
or
is was required in an earlier stage of coding and just stayed there.
 Signature Greetings from Bavaria, Germany Helmut Weber, MVP WordVBA "red.sys" & chr(64) & "t-online.de" Word 2002, Windows 2000
Greg - 16 Feb 2006 13:11 GMT I can understand both possibilities. I have seen nasty continous loops without something like that before. In this case it appears redundant.
|
|
|