Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Much Text Can Fit in a Word Table "Cell" 1

Status
Not open for further replies.

rhpen

Programmer
Nov 2, 2005
40
US
Word 2003 document with a table containing several cells. My macro reads a file and gets 4 sentences as strings. The size of each sentence will not be known until run-time and can differ each run. I want to fit as much into the fixed cell as I can, but only complete sentences. I o not want any partial sentences in the cell. Any way to do this (is there even any way to tell what can fit in a "cell"?

Thanks
 
Not really. As you likely using True Type fonts, text will be kerned.

10 "w" -
takes up the same space as

27 "i" - iiiiiiiiiiiiiiiiiiiiiiiiiii

The cell space may be fixed, but the space used for characters is not. If the text going into thye cell has a lot of narrow charcters, then more will fit in, and vice versa.

Gerry
My paintings and sculpture
 
using True Type fonts
???
You meant proportional, I guess.
 
Hi rhpen,

Actually, it's not that difficult - you can have up to 32Mb of text in a single cell, less what you have elsewhere in the document. There's no cell limit per se, just a document limit of 32Mb of text.

If, as I suspect, you want to know how much text in a given font and point size will fit into a cell of given dimensions, that's not especially hard to work out programmatically. All word processing programs do this to determine where to wrap lines. GUI-based word processors also do it dynamically, to figure out where to place characters on screen. The overhead is really quite small.

Cheers

[MS MVP - Word]
 
Thanks Macropod,

Would you have any hints for one who is fairly new at working with fonts as to how this would be done in VBA? With a proportionally spaced font, the only way I can imagine possibly doing it is by getting down into the details of how many dots or points each character of a font is made up of and knowing the rules by which the program (Word) fits all the various combinations of characters together. From all of the above I can see where one might be able to determine how much space given string of characters might occupy. It sure doesn't seem like it would be easy or have small overhead, though. I am not sure all that information is even available to the programmer. I'm certainly ready to go ahead, though, if you could point me in the right direction.

Thanks Again,
 
hi rphen,

Here's a simple macro that outputs the relative character widths in twips for the standard character set in whatever font attributes are in use at your selection point. To get the absolute widths, simply multiply the returned values by whatever point size you want to use (or change 'xPos / FntSize' to 'xPos' to get the absolute widths for the selected point). Although not necessary for this demo, I've added the metrics to an array which you could then query to build up an overall string length.
Code:
Sub GetCharWidths()
Dim i As Integer
Dim ChrWidths As String
Dim xPos As Single
Dim yPos As Single
Dim FntSize As Single
Dim CharWidthArray(2, 255)
With Selection
    .Collapse
    FntSize = .Font.Size
    For i = 1 To 255
        .TypeText (Chr(i))
        xPos = .Information(wdHorizontalPositionRelativeToTextBoundary)
        CharWidthArray(1, i) = i
        CharWidthArray(2, i) = xPos / FntSize
        ActiveDocument.Undo 1
        ChrWidths = ChrWidths & i & vbTab & Chr(CharWidthArray(1, i)) & vbTab & CharWidthArray(2, i)
        If i Mod 2 = 0 Then ChrWidths = ChrWidths & vbCrLf
        If i Mod 2 = 1 Then ChrWidths = ChrWidths & vbTab
    Next
    .TypeText ChrWidths
End With
End Sub

Note: the code will return a few oddities for ASCII values less than 32, for characters that equate with tabs, line breaks, paragraph breaks and column breaks. But these don't have meaningful widths of their own anyway.

Cheers

[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top