Jump to content

Word 2003 - How to find the longest line inside doc


GrofLuigi

Recommended Posts

It's text, formating doesn't matter, but it should be preserved (so I can't export to a text file *).

A line defined as between two newline characters (pressed ENTER). The lines are not numbered.

The requirement is no line to exceed a limit (which may vary). Let's say a number usually between 30 and 40.

There are numerous documents, typed or examined by me, so batch is undesirable (action taken when exceed detected may vary; only human interaction is possible).

If possible, a plain search string would be desirable. That would probably be the ideal solution.

Otherwise, a macro or VBA whatever can be made, but I have no experience in creating one from scratch and I don't know how one would be stored so to be readily available on some documents, but not on others, at the same time preventing interaction/interference with some custom templates and/or entire applications used (which are in *.dot format in userappdata/.../templates).

Halp? :blushing:

GL

* The macro may export, but only for working purposes and the result should be a report (line number or line content), not the entire text. I am trying to avoid manually exporting and examining texts, since there are many of them.

Edited by GrofLuigi
Link to comment
Share on other sites


Link to comment
Share on other sites

Thank you again jaclaz for the very good pointers, but still not what I need.

I guess I'll need to brush up my old BASIC skills and delve into Visual Basic...

...where I can't distinguish (from the examples) what is a variable name, and what is a command or procedure (keyword)...

...what needs to be initialized on start, how to make a loop with what I want (For each line)....

...what are the actual commands (this should be easy to find)

...how to end it all...

No need to guide me by hand, I'll research each of these problems individually and if I come up with something useful, I'll share. But it might take some time and if it keeps giving me headaches like now, when I dove in too deep, I might postpone it all... :wacko:

GL

Link to comment
Share on other sites

Well,

I can recommend doing baby steps, just copy and paste this:

Sub CountQualifiedLines()
Dim CharCount As Integer
Dim tCount As Integer
Dim LineCount As Integer
Dim Qualifier As String

Qualifier = InputBox("Enter minimum line length:", "Line Length", 1)

tCount = ActiveDocument.ComputeStatistics(wdStatisticLines)
Selection.HomeKey wdStory
Do While tCount > 0
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
CharCount = Selection.Characters.Count
tCount = tCount - 1
If CharCount > Qualifier Then
LineCount = LineCount + 1
End If
Selection.Collapse wdCollapseStart
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Loop
Selection.HomeKey wdStory
MsgBox "There are " & LineCount & " qualified lines in this document"

End Sub

In a new module in a Word Document (already containing some text).

Run the Macro.

See what it does, and study the code.

(Hint1: You don't need a FOR loop, the example does a "countdown" from the total number of lines in the document down to 0)

(Hint2: Anything following a Dim statement is a variable)

jaclaz

Link to comment
Share on other sites

Thank you, I already did that. (I'm not that dim you see, although I might be slightly variable). :blushing:

The script is good for testing as is (at first I thought it lists just the number of lines with characters equal to the input number, but it lists the number of lines exceeding that number, which is exactly what I wanted).

Now I think it's just a matter of printing the line numbers where the overflow occurs (tCount?) for a rough estimate (I think it counts backwards, so I'd need to take care of that). But this is not a high priority. I will be able to figure it myself.

Thank you again for all your help!

GL

Link to comment
Share on other sites

I'm marking the topic as SOLVED since it satisfied one of my two requirements. During practical work, it performed as expected. If I find time in the future, I'll work on displaying the actual line number and post what I come up with here, but for now it's pretty good.

Thanks again jaclaz !

GL

Link to comment
Share on other sites

  • 2 years later...

Very slightly bettered, I'm posting it here in case anyone ever needs it. Now it prints the offending line in the dialog box and puts it in the clipboard, so it will be ready for searching. Line count is tricky, and I couldn't find a solution, because I guess Word paginates, so there is no absolute line count, or nowhere to be found easily. At the end there is a weird If... Endif statement because without it, the macro bombs out if there is no offending line.

Sub CountQualifiedLines()

Dim CharCount As Integer
Dim tCount As Integer
Dim LineCount As Integer
Dim Qualifier As String
Dim DaLine As String
Dim MyData As DataObject
Set MyData = New DataObject

Qualifier = InputBox("Enter minimum line length:", "Line Length", 1)

tCount = ActiveDocument.ComputeStatistics(wdStatisticLines)
Selection.HomeKey wdStory
Do While tCount > 0
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
CharCount = Selection.Characters.Count
tCount = tCount - 1
If CharCount > Qualifier Then
LineCount = LineCount + 1
DaLine = Selection
End If
Selection.Collapse wdCollapseStart
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Loop
Selection.HomeKey wdStory
MsgBox "There are " & LineCount & " qualified lines, line " & DaLine

If DaLine = "" Then
MyData.SetText ""
MyData.PutInClipboard

Else

MyData.SetText DaLine
MyData.PutInClipboard

End If

End Sub

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...