Compare line count in multiple newspaper-style columns RRS feed

  • Question

  • Hello all Experts,

    How can I compare the line count of multiple newspaper-style columns, producing a msgbox such as "Column 1 has x lines, Column 2 has x lines".

    @ Stefan Blom suggested using Selection.Information(wdFirstCharacterLineNumber) or <RangeObject>.Information(wdFirstCharacterLineNumber).

    Unfortunately I don't know how to do it. So far this is what I've got, I don't even know if I'm on the right track. All help will be appreciated.

    Sub test()
        Dim iViewType As Integer
        Dim iLine As Integer
        Dim oRng As Word.Range
        Dim sec As Word.Section
        iViewType = ActiveWindow.View.Type: ActiveWindow.View.Type = wdPrintView
        Options.Pagination = True
        Dim i As Long
        With ActiveDocument
            For i = 1 To .Sections.Count
                With .Sections(i)
                    If .PageSetup.TextColumns.Count > 1 Then
                        oRng.SetRange Start:???, End:=???
                        iLine = .Range.Information(wdFirstCharacterLineNumber)
                        MsgBox iLine
                    End If
                End With
            Next i
        End With
    End Sub

    Friday, May 4, 2012 12:49 PM

All replies

  • I think it's easier to use the 'ComputeStatistics' method. (This is also the only alternative to how you're doing it).

    Are you comfortable getting the range for each text column? Once you do that, you can query the lines.

    Dim lineCount as Integer

    lineCount := myTextColumnRange.ComputeStatistics(wdStatisticLines)

    • Edited by JosephFox Saturday, May 5, 2012 8:21 AM improved my attempt at VB syntax
    Saturday, May 5, 2012 8:20 AM
  • Joseph thanks for the response. As you can see from the above snippet I don't know how to get the range for each text column, can you please help me with that?
    Saturday, May 5, 2012 7:52 PM
  • Only now am I realising that, for reasons best known to themselves, Microsoft haven't given their TextColumn object a range property. So, it is a pretty difficult task you have.

    The only way I can see is iterating over the document, checking points to see if columns start or end there, and defining a range for each of them.

    I'm not used to the VB language. Hopefully other readers will correct mistakes.

    Dim rangeStart As Integer Dim outOfColFlag As Integer Dim curColumn As Integer Dim range As Range Dim content As Range Dim doc As ActiveDocument Dim count As Integer Dim totalChars As Integer Dim totalTextColumns As TextColumns totalChars := doc.Content.Range.Text.Count curColumn := 1 rangeStart := 0 outOfColFlag := -1 Set totalTextColumns As doc.PageSetup.TextColumns For count = 1 To totalChars - 1 Step 1 'Move range position forward one character range.SetRange(Start:=count, End:=count) 'Get text column, if we're in a text column If range.PageSetup.TextColumns.Count > 0 Then 'If we're transferring into a text column, update flag and range If outOfColFlag < 0 Then outOfColFlag := 1 rangeStart := range.Start End If if range.PageSetup.TextColumns.Item(1) <> totalTextColumns(curColumn) Then 'We've transfered into a new text column. Increment column index and get lines curColumn := curColumn + 1 Dim lines As Integer range.Start := rangeStart rangeStart := range.Start 'This position may be the start of a new column range.End := range.End - 1 lines = range.ComputeStatistics(wdStatisticLines) End If Else If outOfColFlag > -1 Then 'We're transferring out of a text column. Increment column index and get lines for the column we've left curColumn := curColumn + 1 Dim lines As Integer range.Start := rangeStart range.End := range.End - 1 lines = range.ComputeStatistics(wdStatisticLines) 'Set flag outOfColFlag := -1 End If End If Next count

    Maybe it'll help. Maybe it won't work at all. I'm afraid I've spent too long on it already, I don't have time to test and debug it.

    This method will be very slow. You could improve things by doing a search of the document, not moving character by character. This is just an idea.

    • Edited by JosephFox Saturday, May 5, 2012 11:10 PM
    Saturday, May 5, 2012 11:04 PM
  • Joseph,

    Thank you very much. unfortunately there are several compile errors, also I wouldn't know how to use it for my intended purpose.

    Any help will be appreciated.

    Sunday, May 6, 2012 6:58 AM