Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

Word 2003 - How to find the longest line inside doc

- - - - -

  • Please log in to reply
9 replies to this topic

#1
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,355 posts
  • OS:none specified
  • Country: Country Flag
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, 03 February 2010 - 10:06 AM.



How to remove advertisement from MSFN

#2
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,266 posts
  • OS:none specified
  • Country: Country Flag
Some ideas:

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

And some real world Macro examples:
http://web.ticino.co..._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
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,355 posts
  • OS:none specified
  • Country: Country Flag
Thank you, my master of links! :thumbup

I'll get right on to them...

GL

#4
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,355 posts
  • OS:none specified
  • Country: Country Flag
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
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,355 posts
  • OS:none specified
  • Country: Country Flag

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
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,266 posts
  • OS:none specified
  • Country: Country Flag
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
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,355 posts
  • OS:none specified
  • Country: Country Flag
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
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,355 posts
  • OS:none specified
  • Country: Country Flag
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
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,266 posts
  • OS:none specified
  • Country: Country Flag

Thanks again jaclaz !


You are welcome. :)

Posted Image

;)

jaclaz

#10
GrofLuigi

GrofLuigi

    GroupPolicy Tattoo Artist

  • Member
  • PipPipPipPipPipPip
  • 1,355 posts
  • OS:none specified
  • Country: Country Flag
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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN