Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2004

Tip: Looking for answers? Try searching our database.

Determining the largest/smallest font size in a selection?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andreas Baus - 04 Nov 2004 13:21 GMT
Selection.Font.Size returns wdUndefined when the Selection contains
portions of text with different font sizes (of course, this makes
sense since the range then does not have one definite font size). Does
anyone know if there is an way to determine at least for example the
largest or smallest font size used in the selected range efficiently
(i.e. a solution that does *not* involve iterating over and checking
every single character, if possible...)?
All suggestions will be highly appreciated.
Klaus Linke - 04 Nov 2004 19:34 GMT
Hi Andreas,

Recursively halving the range until you don't get wdUndefined should be
pretty fast... See macros below.
Hope I got the logic right... you should run a few tests.

Regards,
Klaus

Sub SelectionFontSize()
 ' select some text and run this macro
 Dim rng As Range
 Dim min As Single
 Dim max As Single
 ' smallest possible size:
 max = 1
 ' largest possible size:
 min = 1638
 Set rng = Selection.Range.Duplicate
 Call MinMaxFontSize(rng, min, max)
 MsgBox min & "/" & max, , "min/max font size"
End Sub

Sub MinMaxFontSize(rng As Range, min As Single, max As Single)
 Dim myFontSize As Single
 Dim rng1 As Range
 Dim rng2 As Range
 myFontSize = rng.Font.Size
 If rng.Font.Size = wdUndefined Then
   Set rng1 = rng.Duplicate
   Set rng2 = rng.Duplicate
   rng1.End = rng.Start + Int(0.5 * (rng.End - rng.Start))
   rng2.Start = rng1.End + 1
   Call MinMaxFontSize(rng1, min, max)
   Call MinMaxFontSize(rng2, min, max)
 Else
   If myFontSize < min Then
   min = myFontSize
   Else
     If myFontSize > max Then
       max = myFontSize
     End If
   End If
 End If
End Sub
Klaus Linke - 04 Nov 2004 19:53 GMT
I knew I'd blow this!
Change

   If myFontSize < min Then
       min = myFontSize
   Else
     If myFontSize > max Then
       max = myFontSize
     End If
   End If

to

   If myFontSize < min Then
     min = myFontSize
   End If
   If myFontSize > max Then
     max = myFontSize
   End If

BTW, another thing you could try is to collapse the selection to the start,
then use
Selection.SelectCurrentFont

You now can be sure that you don't get wdUndefined for the font size.
Collapse to the end, rinse and repeat, until you are at the end of the
range.

Or get the size of the first character, and use "Edit > Find" for this font
size. Then collapse to the end, rinse and repeat.

The last method should also be pretty fast.
"Find" matches one paragraph at a time whereas the recursive macro can get
the size for several paragraphs in one go.
OTOH, the recursive macro may get wdUndefined quite often, before it
determines the real font size.

Regards,
Klaus
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.