MSFN Forum: Word 2003 - How to find the longest line inside doc - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Word 2003 - How to find the longest line inside doc OR list lines exceeding certain length? [<- SOLVED] Rate Topic: -----

#1 User is offline   GrofLuigi 

  • GroupPolicy Tattoo Artist
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,275
  • Joined: 21-April 05
  • OS:none specified
  • Country: Country Flag

Posted 26 January 2010 - 09:36 AM

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.

This post has been edited by GrofLuigi: 03 February 2010 - 10:06 AM



#2 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,433
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 26 January 2010 - 11:32 AM

Some ideas:

http://technet.micro...y/ee692861.aspx
http://vbcity.com/fo...575/628427.aspx

And some real world Macro examples:
http://web.ticino.com/multilingual/word_ch..._count_tips.htm

http://gregmaxey.mvp...nes_of_Text.htm

The second example in the latter should be a good base for you.

jaclaz

#3 User is offline   GrofLuigi 

  • GroupPolicy Tattoo Artist
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,275
  • Joined: 21-April 05
  • OS:none specified
  • Country: Country Flag

Posted 26 January 2010 - 01:02 PM

Thank you, my master of links! :thumbup

I'll get right on to them...

GL

#4 User is offline   GrofLuigi 

  • GroupPolicy Tattoo Artist
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,275
  • Joined: 21-April 05
  • OS:none specified
  • Country: Country Flag

Posted 26 January 2010 - 02:23 PM

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

#5 User is offline   GrofLuigi 

  • GroupPolicy Tattoo Artist
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,275
  • Joined: 21-April 05
  • OS:none specified
  • Country: Country Flag

Posted 26 January 2010 - 02:38 PM

View Postjaclaz, on Jan 26 2010, 06:32 PM, said:

http://gregmaxey.mvp...nes_of_Text.htm

The second example in the latter should be a good base for you.

jaclaz


You're right about that. :thumbup

But still there's some work to adapt it to my needs.

GL

#6 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,433
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 26 January 2010 - 02:42 PM

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

#7 User is offline   GrofLuigi 

  • GroupPolicy Tattoo Artist
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,275
  • Joined: 21-April 05
  • OS:none specified
  • Country: Country Flag

Posted 26 January 2010 - 06:26 PM

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

#8 User is offline   GrofLuigi 

  • GroupPolicy Tattoo Artist
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,275
  • Joined: 21-April 05
  • OS:none specified
  • Country: Country Flag

Posted 03 February 2010 - 10:13 AM

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

#9 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,433
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 03 February 2010 - 02:12 PM

View PostGrofLuigi, on Feb 3 2010, 05:13 PM, said:

Thanks again jaclaz !


You are welcome. :)

Posted Image

;)

jaclaz

#10 User is offline   GrofLuigi 

  • GroupPolicy Tattoo Artist
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,275
  • Joined: 21-April 05
  • OS:none specified
  • Country: Country Flag

Posted 02 August 2012 - 02:48 AM

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




Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy