Results 1 to 9 of 9
  1. #1
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I am sure this is simple but ...

    I have small routine that needs to loop through each line of a document.
    It checks the first word of the line.
    Depending upon a word, it makes the line bold and of a fixed size.

    All this is done, but how can I check if I am at the end of the document.

    Ideally something like

    Do Until ActiveDocument.? 'At end of document

    How do I find out when I have reached the last line?
    Andrew

  2. #2
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts
    OK.

    I got round this by placing my own finish text at the end of the document and checking for that.

    It solves my problem, but does not answer the question of how to determine if I am at the end of the document.
    Or indeed how to find the number of lines in a document.

    Microsoft can give the lines statistics in document properties, but do not seem to expose it as a property.
    Andrew

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You could use code like this:

    Code:
    Dim lngOldStart As Long
    Dim lngNewStart As Long
    Selection.HomeKey Unit:=wdStory
    lngNewStart = 0
    Do
      ' Your code here
      ...
      Selection.MoveDown Unit:=wdLine
      lngOldStart = lngNewStart
      lngNewStart = Selection.Start
    Loop Until lngNewStart = lngOldStart

  4. #4
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by HansV View Post
    You could use code like this:

    Code:
    Dim lngOldStart As Long
    Dim lngNewStart As Long
    Selection.HomeKey Unit:=wdStory
    lngNewStart = 0
    Do
      ' Your code here
      ...
      Selection.MoveDown Unit:=wdLine
      lngOldStart = lngNewStart
      lngNewStart = Selection.Start
    Loop Until lngNewStart = lngOldStart
    That works for me thanks.
    Mind you, you wonder why they do not provide some facility to determine an EOF.

    Is there any way to determine the number of lines other then looping through every line?

    'I really don't like the Word Object Model'
    Andrew

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The number of lines is ActiveDocument.ComputeStatistics(wdStatisticLines) .

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by AndrewKKWalker View Post
    Mind you, you wonder why they do not provide some facility to determine an EOF.
    If you go way back with WordBasic, then in the deep recesses of your memory, you would find a predefined bookmark that lets you do this:
    Code:
    If Selection.Range = ActiveDocument.Bookmarks("\EndOfDoc").Range Then Exit Do
    That assumes an insertion point directly adjacent to the final paragraph symbol. If you have some other kind of selection, you may need to compare the .End properties of the two ranges. If you are using a Range rather than the Selection, then the left side of the comparison would be your range.

    == Edit ==

    Upon further reflection, this might be safer (using >= accounts for selecting the final paragraph):
    Code:
    If Selection.Range.End >= ActiveDocument.Bookmarks("\EndOfDoc").Range.End Then Exit Do

  7. #7
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by HansV View Post
    The number of lines is Activedocument.ComputeStatistics(wdStatisticLines) .
    Thanks Hans.


    That one is seriously squirreled away in the depths.
    I searched all over the web and found nothing.
    Andrew

  8. #8
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by jscher2000 View Post
    If you go way back with WordBasic, then in the deep recesses of your memory, you would find a predefined bookmark that lets you do this:
    Code:
    If Selection.Range = ActiveDocument.Bookmarks("\EndOfDoc").Range Then Exit Do
    That assumes an insertion point directly adjacent to the final paragraph symbol. If you have some other kind of selection, you may need to compare the .End properties of the two ranges. If you are using a Range rather than the Selection, then the left side of the comparison would be your range.

    == Edit ==

    Upon further reflection, this might be safer (using >= accounts for selecting the final paragraph):
    Code:
    If Selection.Range.End >= ActiveDocument.Bookmarks("\EndOfDoc").Range.End Then Exit Do
    Thanks
    I'll give it a go.

    I don't have to do much with Word coding, and that is the problem really.
    Andrew

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by AndrewKKWalker View Post
    I don't have to do much with Word coding, and that is the problem really.
    Judiciously applied, VBA can be really useful for overcoming Word frustrations. Then you could change that gruesome avatar into something less, uh, messy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •