none
How do I calculate number of character per each line in Word header/footer from vb.net? RRS feed

  • Question

  • I need to calculate number of character I can put in a line in the header/footer. I tried to use pagesize/fontsize but the number does not seem to correct.  Any ideas?
    Monday, July 23, 2012 7:41 PM

Answers

  • It is not practical to do all the work involving font metrics that Word does to determine where the line needs to break, and all you can really do is add the text to the document and check what Word has done and adjust it to suit your needs.
     
    Unfortunately this is more complex in Headers than in the body of a document. Word really doesn't work with Lines (it is a Word Processing application, not a Page Layout one) and this is especially true in stories other than the main document story.
     
    You can't directly get line numbers in headers and need to deduce them from, for example, [Range].Information(wdVerticalPositionRelativeToPage). You can't easily move around lines without selecting the text and using the Selection and even that is tricky in the header. What you might be able to do is use Find and Replace for the "/" characters you want to break at and try to determine whether you need to do anything but it will still be complex I'm afraid.
     
    Whatever you do is going to be difficult and fairly inefficient. If your data are so varied that you can't find a simpler way - perhaps always splitting it at a certain point even if not strictly necessary - then the best way might be to create a hidden new document and do your calculating and string breaking in that before discarding it and writing the results into your real document header.
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by BLT123 Friday, August 10, 2012 1:25 PM
    • Unmarked as answer by BLT123 Friday, August 10, 2012 1:25 PM
    • Marked as answer by BLT123 Friday, August 10, 2012 1:26 PM
    Thursday, July 26, 2012 7:50 PM

All replies

  • Hi,

    Thanks for posting in the MSDN Forum.

    Based on your issue, I would suggest you a macro for counting characters of a header/footer text in Word. Please refer to the following code:

    Sub Test()
    Dim WordCountHeader As Long
    Dim WordCountFooter As Long
    Dim HeaderRange As Range
    Dim FooterRange As Range
    Set HeaderRange = ActiveDocument.Sections(1).Headers(1).Range
    Set FooterRange = ActiveDocument.Sections(1).Footers(1).Range
    'Count characters of the header
    WordCountHeader = HeaderRange.ComputeStatistics(wdStatisticCharacters)
    MsgBox "Header text has " & WordCountHeader & " characters"
    'Count characters of the footer
    WordCountFooter = FooterRange.ComputeStatistics(wdStatisticCharacters)
    MsgBox "Footer text has " & WordCountFooter & " characters"
    End Sub

    Hope this can help you.

    If anything is unclear, please do not hesitate to let me know.

    Have a good day.


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, July 24, 2012 9:05 AM
    Moderator
  • Hi Leo,

    Thanks for your suggestion. But it jsut returns 0. I may did not explain my issue very well.

    I need to calculate how many characters will be fit in each line of header before I add text in header using vb.net but any macro code still works. When I tried your code in my vb.net with new word document it returns 0. here is how I wrote the function.  Is this only counting chars already in ther header? I need to calculate maximum number of charaters that will fit in each line in header.

    Private function CalculateCharPerLine() as integer

    dim CharPerLine as integer

    dim HeaderRange as Word.Range

    HeaderRange = ActiveDocument.Sections(1).Headers(1).Range

    CharPerLine = HeaderRange.ComputeStatistics(Word.WdStatistic.wdStatisticCharacters)

    End Function

    Wednesday, July 25, 2012 3:36 PM
  • This is a really complex thing to do. You don't say why exactly you need this information but it would be far far easier to add your text and check whether it has actually fitted on one line or not.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, July 26, 2012 12:29 PM
  • You can only calculate how many characters will fit on a line if you're using a fixed-width font. Even then, if the text is justified and uses WordPerfect justification rules, the calculated result will be unreliable. For proportional fonts, you cannot do such a calculation in advance unless you know what all the characters are - and the issues re justification can invalidate the calculation.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Thursday, July 26, 2012 12:45 PM
    Thursday, July 26, 2012 12:44 PM
  • Have a play with the following. It returns the nominal width of an input string in points, based on whatever the font is at the insertion point:

    Sub CalcStringWidth()
    Application.ScreenUpdating = False
    Dim i As Long, FntName As String, FntSize As Single, xPos As Single, StrWidth As Single
    Dim MyString As String, CharWidthArray(255)
    MyString = InputBox("Please input a test string")
    With Selection
      .Collapse
      FntName = .Font.Name
      FntSize = .Font.Size
      xPos = .Information(wdHorizontalPositionRelativeToTextBoundary)
      For i = 1 To 255
        .TypeText (Chr(i))
        CharWidthArray(i) = .Information(wdHorizontalPositionRelativeToTextBoundary) - xPos
        ActiveDocument.Undo 1
      Next
    End With
    For i = 1 To Len(MyString)
      StrWidth = StrWidth + CharWidthArray(Asc(Mid(MyString, i, 1)))
    Next
    MsgBox "The String: " & """" & MyString & """" & vbCrLf & " is " & StrWidth _
      & " points wide when printed in " & FntSize & "pt " & FntName
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, July 26, 2012 12:52 PM
  • Tony,

    Thanks for your thought. In my situation I need to break the line in certain charater, for example line only break at "/", I need to decide before set text property I need to decide how many characters will fit in one line.  Could you tell me how do I check whether it actually fit in one line? Do I need to add certain number of chars and counting line numbers?

    thanks in advance,

    Thursday, July 26, 2012 12:55 PM
  • It is not practical to do all the work involving font metrics that Word does to determine where the line needs to break, and all you can really do is add the text to the document and check what Word has done and adjust it to suit your needs.
     
    Unfortunately this is more complex in Headers than in the body of a document. Word really doesn't work with Lines (it is a Word Processing application, not a Page Layout one) and this is especially true in stories other than the main document story.
     
    You can't directly get line numbers in headers and need to deduce them from, for example, [Range].Information(wdVerticalPositionRelativeToPage). You can't easily move around lines without selecting the text and using the Selection and even that is tricky in the header. What you might be able to do is use Find and Replace for the "/" characters you want to break at and try to determine whether you need to do anything but it will still be complex I'm afraid.
     
    Whatever you do is going to be difficult and fairly inefficient. If your data are so varied that you can't find a simpler way - perhaps always splitting it at a certain point even if not strictly necessary - then the best way might be to create a hidden new document and do your calculating and string breaking in that before discarding it and writing the results into your real document header.
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by BLT123 Friday, August 10, 2012 1:25 PM
    • Unmarked as answer by BLT123 Friday, August 10, 2012 1:25 PM
    • Marked as answer by BLT123 Friday, August 10, 2012 1:26 PM
    Thursday, July 26, 2012 7:50 PM