none
How to measure a string width? RRS feed

  • Question

  • I want to set the ColumnWidth of my table. Therefore I want to measure the width of my tableheaders.

    How can I get the width of a word in Interop.Word?

    I tried to use the "MeasureString"-Method from the Graphics-Class but this use "System.Drawing.Font" and is not compatible with "Interop.Word.Font":

        Dim g As Graphics = Me.CreateGraphics
        For i As Integer = 0 To wordTab.Columns.Count - 1
          wordTab.Columns(i + 1).Width = g.MeasureString("TableHeader",hdFont).Width
        Next
        g.Dispose() : g = Nothing
    

     

    Monday, July 19, 2010 9:51 AM

Answers

  • Hi Gooner

    The Word object model doesn't provide anything like this. Partly, this is probably because Word lays things out differently depending on the selected printer driver...

    Which version of Word are we talking about? Most recent versions of Word have "AutoFit" capability for Word tables.

    Some can also shrink text to fit the width of a table cell.

    It should also be possible for you to use the .NET Framework method to get a value, then apply that to the table column, although there's no guarantee it will actually be an adequate width, depending on the printer driver Word's using to lay out the page. You'd just have to create a System.Drawing.Font, using the font name and size information from Word...

    another approach would be to insert the text into a paragraph in the document (outside the table), then use a parameter from Range.Information to get the start and end points, which would let you calculate the width.


    Cindy Meister, VSTO/Word MVP
    Monday, July 19, 2010 12:01 PM
    Moderator

All replies

  • Hi Gooner

    The Word object model doesn't provide anything like this. Partly, this is probably because Word lays things out differently depending on the selected printer driver...

    Which version of Word are we talking about? Most recent versions of Word have "AutoFit" capability for Word tables.

    Some can also shrink text to fit the width of a table cell.

    It should also be possible for you to use the .NET Framework method to get a value, then apply that to the table column, although there's no guarantee it will actually be an adequate width, depending on the printer driver Word's using to lay out the page. You'd just have to create a System.Drawing.Font, using the font name and size information from Word...

    another approach would be to insert the text into a paragraph in the document (outside the table), then use a parameter from Range.Information to get the start and end points, which would let you calculate the width.


    Cindy Meister, VSTO/Word MVP
    Monday, July 19, 2010 12:01 PM
    Moderator
  • another approach would be to insert the text into a paragraph in the document (outside the table), then use a parameter from Range.Information to get the start and end points, which would let you calculate the width.

    Hello

    How I can get the start and end points from Range.Information? WdInformation is not contain this information.

    Thursday, November 1, 2012 12:48 PM
  • Hi Cindy,

    The difficulty I see with the latter approach is that justification, especially with the Wordperfect option, means the raw string width outside the table may return a result that is not representative of what you could get within it. In that case, one could calculate the raw string width outside the table, then keep moving the paragraph indent inwards from there until the text wraps to a second line. The previous iteration gives the minimum text width. Add to that the cell's/table's internal margin widths and you'll have the minimum possible column width. As you say, though, changing to another printer may invalidate all the effort ...


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, November 3, 2012 3:27 AM