Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Challenge: Fit text to text box in Word


Reply
 
Thread Tools Display Modes
  #1  
Old 10-20-2005, 12:25 PM
UtahJ UtahJ is offline
Freshman
 
Join Date: Dec 2003
Posts: 40
Question Challenge: Fit text to text box in Word 2000


Hi everyone.

I need to fit a random number of lines of text vertically within a Word text
box. The height of the text box, the font, and the font size will remain the same,
but the number of lines will change.

Right now the user pastes the data and then manually fits the data to
the text box (so it fills it from top to bottom equally spaced) by adjusting the paragraph line spacing.
It is a rather tedious process manually, but I thought it might be able to be automated.

The text box height is 7.53 and the font is Arial 9.

Any ideas?

Last edited by UtahJ; 10-20-2005 at 02:10 PM.
Reply With Quote
  #2  
Old 10-20-2005, 02:37 PM
herilane's Avatar
herilane herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

Absolutely. Anything tedious should be automated.
The simplest approach I could think of was to simply try different values for line spacing, until you find one that is too large (text won't fit any more). Then set the line spacing to just below that.

Brief example:
Code:
Sub main() Const c_DesiredHeight = 80 Const c_Step = 5 Dim tb As Shape Set tb = ActiveDocument.Shapes(1) tb.TextFrame.AutoSize = True With tb.TextFrame.TextRange.ParagraphFormat .LineSpacingRule = wdLineSpaceExactly Dim i As Long 'each 1 step in i equals 1/10th of a point For i = 90 To 170 Step c_Step .LineSpacing = i / 10 Application.ScreenRefresh 'force Word to redraw the textbox If tb.Height > c_DesiredHeight Then 'we've gone one step too far - reverse that, and stop .LineSpacing = (i - c_Step) / 10 Exit For End If Next i End With tb.TextFrame.AutoSize = False tb.Height = c_DesiredHeight End Sub
Changing the Step value will change the precision - smaller steps will lead to more precise adjustments, but slower code. Everything else should be self-explanatory.
Reply With Quote
  #3  
Old 10-27-2005, 02:54 PM
UtahJ UtahJ is offline
Freshman
 
Join Date: Dec 2003
Posts: 40
Default

Quote:
Originally Posted by herilane
Absolutely. Anything tedious should be automated.
The simplest approach I could think of was to simply try different values for line spacing, until you find one that is too large (text won't fit any more). Then set the line spacing to just below that.

Brief example:
Code:
Sub main() Const c_DesiredHeight = 80 Const c_Step = 5 Dim tb As Shape Set tb = ActiveDocument.Shapes(1) tb.TextFrame.AutoSize = True With tb.TextFrame.TextRange.ParagraphFormat .LineSpacingRule = wdLineSpaceExactly Dim i As Long 'each 1 step in i equals 1/10th of a point For i = 90 To 170 Step c_Step .LineSpacing = i / 10 Application.ScreenRefresh 'force Word to redraw the textbox If tb.Height > c_DesiredHeight Then 'we've gone one step too far - reverse that, and stop .LineSpacing = (i - c_Step) / 10 Exit For End If Next i End With tb.TextFrame.AutoSize = False tb.Height = c_DesiredHeight End Sub
Changing the Step value will change the precision - smaller steps will lead to more precise adjustments, but slower code. Everything else should be self-explanatory.


Thanks for the code herilane. Unfortunately, it seems to resize the
text box to the text, rather than spacing the text to the text box.

I'll keep digging.
Reply With Quote
  #4  
Old 10-29-2005, 04:59 AM
herilane's Avatar
herilane herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

Really? That's not what it did when I tried it.

It sets the textbox autosize to true. It then tries different line spacings, letting the textbox autosize accordingly. It then compares this autosized height to the height you want, and if theyr'e the same, turns off autosize again. The text should now fit nicely in the box.
Reply With Quote
  #5  
Old 10-31-2005, 09:27 AM
UtahJ UtahJ is offline
Freshman
 
Join Date: Dec 2003
Posts: 40
Default

Quote:
Originally Posted by herilane
Really? That's not what it did when I tried it.

It sets the textbox autosize to true. It then tries different line spacings, letting the textbox autosize accordingly. It then compares this autosized height to the height you want, and if theyr'e the same, turns off autosize again. The text should now fit nicely in the box.

My mistake. There is a lot going on in this particular document.
Not only was the code running against a different text box, but someone
has added some incomplete code to the Normal.dot (for some reason)
that seems to interfere with your code.

I'm going to clean things up and try it again.

Thanks again.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->