PDA

View Full Version : Scaling Text in Range



andalusia
07-30-2006, 12:42 AM
Say I have a range of text with various font sizes. Is there a simple way to scale all of the font sizes in the range up or down?

TonyJollans
07-30-2006, 08:36 AM
No.

You can reduce mixed font sizes incrementally (but not scale them) via the UI but not from VBA. I've never tried it but it might, I suppose be possible with SendKeys.

fumei
07-30-2006, 12:38 PM
I am not quite understanding...still having the effects of my stupid pill...

Selection.Font.Grow - will increment all fonts in a selection proportionally, regardless of whether they are mixed, or not. It increases the font size to the next standard size. Note: NOT +1 pt.

On the other hand, as Tony mentions (sort of):

Selection.Font.Size = Selection.Font.Size + 1

will NOT work. This is because if the Selection has mixed font size, VBA does not know which = Font.Size. So Font.Size is returned as 9999999, which is not useful.

.Grow on the other hand is a global method, and does not require a value. It applies to everything.

So, what do you mean by "scale" exactly?

fumei
07-30-2006, 12:39 PM
Oh, and of course there is a .Shrink method as well.

fumei
07-30-2006, 12:55 PM
Oh...and replace "Selection" with "Range".

TonyJollans
07-31-2006, 05:46 AM
My apologies :blush

I told you I had my own supply of Stupid Pills :)

I'd like to change my answer to: Yes.

Grow and Shrink don't exactly scale but they do do better than just increasing and decreasing by a point at a time.

fumei
07-31-2006, 01:25 PM
...these things last a long time.....

Oh, by scale do you mean....increase/decrease by percent? As in - increase size by 30%?

andalusia
08-09-2006, 12:11 AM
...these things last a long time.....

Oh, by scale do you mean....increase/decrease by percent? As in - increase size by 30%?

Yes, that is what I meant.

fumei
08-09-2006, 12:35 AM
Nope, not as a percentage applicable in both dimensions.

mdmackillop
08-09-2006, 11:21 AM
This will scale the text for the whole document, but I couldn't get it to work with a selection. It uses italic to distinguish between changed sizes, but this could be changed to bold or other format.

Sub ScaleFontSize()
Dim i As Long, Ratio As Long
For i = 1 To 100
Ratio = Int(i * 1.3)
With ActiveDocument.Content.Find
.Font.Size = i
.Font.Italic = False
.Replacement.Font.Size = Ratio
.Replacement.Font.Italic = True
.Execute Replace:=wdReplaceAll
End With
Next
ActiveDocument.Content.Font.Italic = False
End Sub

mdmackillop
08-09-2006, 11:38 AM
Here's a cludge to deal with selection, but I'm sure it can be bettered

Sub ScaleSelection()
Dim wd As Document
Set wd = ActiveDocument
Selection.Cut
Documents.Add
Selection.PasteAndFormat (wdPasteDefault)
ScaleFontSize
ActiveDocument.Content.Cut
ActiveDocument.Close False
wd.Activate
Selection.PasteAndFormat (wdPasteDefault)
End Sub

fumei
08-09-2006, 06:28 PM
Hi Malcolm, I ran your code on a document with the following text:

Text at 12 pt
Text at 10 pt
Text at 22 pt
Text at 16 pt

with the paragraphs formatted to the stated font size. I added extra lines to type in the NEW font size, as in:Sub ScaleFontSize()
Dim i As Long, Ratio As Long
Dim oPara As Word.Paragraph
Dim oldSize As Integer
For i = 1 To 100
Ratio = Int(i * 1.3)
With ActiveDocument.Content.Find
.Font.Size = i
.Font.Italic = False
.Replacement.Font.Size = Ratio
.Replacement.Font.Italic = True
.Execute Replace:=wdReplaceAll
End With
Next
ActiveDocument.Content.Font.Italic = False
' my addition
For Each oPara In ActiveDocument.Paragraphs
oPara.Range.Select
Selection.MoveEnd unit:=wdCharacter, Count:=-1
Selection.Range.InsertAfter _
" New font size = " & oPara.Range.Font.Size
Selection.Collapse direction:=wdCollapseEnd
Next
End SubThe result? With the paragraphs in the stated font size of course.

Text at 12 pt New font size = 15
Text at 10 pt New font size = 13
Text at 22 pt. New font size = 28
Text at 16 pt. New font size = 20

As you can see, while the change in font size is different ( +3, + 3, + 6, + 4), the percentage increase is pretty close.

Paragraph 1 (12 pt to 15 pt) 25% increase
Paragraph 2 (10 pt to 13 pt) 30% increase
Paragraph 3 (22 pt to 28 pt) 27% increase
Paragraph 4 (16 pt to 20 pt) 25% increase

But I am not sure if this is what was desired. BTW: what is with all those iterations?

mdmackillop
08-10-2006, 12:36 AM
But I am not sure if this is what was desired. BTW: what is with all those iterations?
Hi Gerry
I just plugged in these figures to cover any situation. Easily changed to "6 to 18" or whatever, as is the ratio which i put at 30%. Obviously a Select Case could be included, rather than use arbitrary rounding.
The problem I had with selection was applying this to a highlighted section of text. If you change Activedocument.Content to Selection, the document is modified from the selection to the end.

andalusia
08-11-2006, 05:19 PM
I'm about to try these ideas out, but I had a concern about the iteration method. Won't it overlook fractional font sizes like 11.2, 13.6 etc. ?


Also, would it be reasonable to adjust font sizes into fractional font sizes, so that the resizing could occur multiple times without becoming skewed from the roundoff? For an extreme example, consider a resize method that increases by 4%. Then from roundoff,


Initial Once Twice Thrice Four Five
10 pt 10 pt 10 pt 10 pt 10 pt 10 pt
20 pt 21 pt 22 pt 23 pt 24 pt 25 pt

So one font is growing with multiple executions while the smaller one isn't.

This is an extreme example, but I think there would be a similar effect under ordinary circumstances as well.

It's not really that important, though - I only intend to make one resize.

mdmackillop
08-12-2006, 02:14 AM
Hi Andalusia,
To be honest, I thought font sizes were integers. You learn something new every day! Please feel free to change the code to use fractional font sizes and let us know how you get on.
Regards
MD

fumei
08-12-2006, 06:11 AM
Font.Size is SINGLE. Look in object browser.

mdmackillop
08-12-2006, 06:44 AM
Thanks Gerry.