none
I want to determine the width in twips a selected text from a paragraph RRS feed

  • Question

  • Hello everyone,

     

    I want to determine the width in twips, a selected text from a paragraph. I need

    to write a Word macro for that, can someone give me guidance to do that?

     

    thank you,

    rana.



    Thursday, March 24, 2011 3:01 AM

Answers

  • Hi Tony,

    When working with wrapped text, you get the wrong result if the selected range includes the last character on the line. Try:

    Sub GetStringWidth()
    Dim RngEnd As Range
    With Selection
      Set RngEnd = .Range
      With RngEnd
        .Start = .End
        'If the selection spans a whole line, the RngEnd becomes the
        'start of the next line, so we need to back up one character.
          If .Information(wdHorizontalPositionRelativeToPage) < _
            Selection.Information(wdHorizontalPositionRelativeToPage) Then
            .MoveStart wdCharacter, -1
          End If
          MsgBox "The string width in twips is: " & 20 * _
            (.Information(wdHorizontalPositionRelativeToPage) - _
            Selection.Information(wdHorizontalPositionRelativeToPage))
      End With
    End With
    Set RngEnd = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Sunday, March 27, 2011 3:05 AM

All replies

  • Hi rana123,

    Thanks for posting in the MSDN Forum.

    Would you clarify the mean of “twips”?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 25, 2011 7:53 AM
    Moderator
  • If the selected text is all one one line - and if not, although more complex, a variation using the same functionality could be developed ...
     
        LeftPoint = Selection.Information(wdHorizontalPositionRelativeToPage)
        Selection.Collapse wdCollapseEnd
        RightPoint = Selection.Information(wdHorizontalPositionRelativeToPage)
        Twips = 20 * (RightPoint - LeftPoint)

    --
    Enjoy,
    Tony
     

    Enjoy,
    Tony
    www.WordArticles.com
    Saturday, March 26, 2011 3:39 PM
  • Hi Tony,

    When working with wrapped text, you get the wrong result if the selected range includes the last character on the line. Try:

    Sub GetStringWidth()
    Dim RngEnd As Range
    With Selection
      Set RngEnd = .Range
      With RngEnd
        .Start = .End
        'If the selection spans a whole line, the RngEnd becomes the
        'start of the next line, so we need to back up one character.
          If .Information(wdHorizontalPositionRelativeToPage) < _
            Selection.Information(wdHorizontalPositionRelativeToPage) Then
            .MoveStart wdCharacter, -1
          End If
          MsgBox "The string width in twips is: " & 20 * _
            (.Information(wdHorizontalPositionRelativeToPage) - _
            Selection.Information(wdHorizontalPositionRelativeToPage))
      End With
    End With
    Set RngEnd = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Sunday, March 27, 2011 3:05 AM