Results 1 to 13 of 13
  1. #1
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Test for End of Document (Word 97)

    In a macro or global search how do you test for the end of the document?

    If this subject is in the archives please give me words or phrases to search on. End of document generated hits but nothing I could use.

    Thanks

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Test for End of Document (Word 97)

    There are several methods, but you need to describe the scenario in more detail.

    Suppose, for example, that you want to repeat an action over and over but stop when your insertion point is in front of the last paragraph mark in the body. You know you're there when the insertion point position is one less than the last position in the text:

    While Selection.End < ActiveDocument.Content.End - 1
    'Do Stuff
    Wend

    But... if you are using Find, using these settings should stop at the end of the document

    With Selection.Find
    ...
    .Forward = True
    .Wrap = wdFindStop

    Does this help?

  3. #3
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Test for End of Document (Word 97)

    Yes. Thanks

  4. #4
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Test for End of Document (Word 97)

    How would I handle the following:

    Do
    Selection.MoveDown Unit:=wdLine, Count:=1

    If (Test for end of document) Then
    Exit Do

    Else

    End If

    Selection.Delete Unit:=wdCharacter, Count:=1
    Loop

    Thanks

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Test for End of Document (Word 97)

    In other words, you want to know if your insertion point is on the last line of a document, anywhere in the line? Mmmm... not immediately obvious.

    Why are using using wdLine? Do you really have to? If you were to use wdParagraph, then the test I posted before should work. Hopefully it's not too lazy of me to suggest this approach. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  6. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Test for End of Document (Word 97)

    What about modifying Jefferson's code sample along the lines of
    While Selection.Paragraphs(1).End < ActiveDocument.Content.End - 1
    'Do Stuff
    Wend
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Test for End of Document (Word 97)

    Or even, just in case the selection covers more than one paragraph...

    While Selection.Paragraphs(Selection.Paragraphs.Count).E nd < ActiveDocument.Content.End - 1

    StuartR

  8. #8
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Test for End of Document (Word 97)

    Yes, I do want to know if the insertion point is on the last line of a document.

    I normally program in Access 97 and import a text file into a table. Normally the file is single-spaced, but for some unknown reason once every three months the file comes over double-spaced.

    I used the macro builder in Word 97 to move down one line and delete the extra line. That is where wdLine came from. I really can't answer questions as to why I would use one command over another and I may not be using the correct terminology. I defer to you and others on this list with more experience to point me in the right direction.

    Thanks

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Test for End of Document (Word 97)

    If double spacing is your problem - why don't you use a search and replace function that finds two consecutive returns (^p^p) and replaces them with one (^p). Its a whole lot quicker than the code you are trying to create.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Test for End of Document (Word 97)

    One way of learning what those different "Unit" codes mean is to turn on the macro recorder and navigate around. MoveDown wdLine is roughly the same as DownArrow, while MoveDown wdParagraph is roughly the same as Ctrl+DownArrow. The first works well in structured data, but the second is more reliable if you want to go to the beginning of the next line (where the current line ends with a paragraph mark).

    Anyway, for your purposes, I completely agree with the Find and Replace approach. Use the More button to reveal the special characters pop-up and you'll see the incredible flexibility available to you.

  11. #11
    2 Star Lounger
    Join Date
    Jun 2002
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Test for End of Document (Word 97)

    I know this is going to sound strange but global replace produces inconsistent results. For some reason it leaves an occasional (^p^p^p), but moving down one line and deleting the (^P) works.

    Thanks for your time.

  12. #12
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Test for End of Document (Word 97)

    When the global replace has failed for me it is usually due to
    1. Some other character in between (space, tab, floating graphic)
    2. Revision tracking
    3. The return is actually a ^13 character
    4. More than two consecutive returns - solved by running the replace multiple times or using wildcards to search for two or more returns.

    So it doesn't surprise me that the global replace is not fully consistent for you but you may find your code will stumble over the same issues.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Test for End of Document (Word 97)

    Andrew's #3 is common in text that was not created in Word for Windows, as other programs and platforms often have a different view on how to end a line. Since Word will find a ^p when you search for a ^13 (note: thirteen, not L3) and not the other way around, it should be equally effective to:

    Find: ^13^13

    Replace: ^p

    in your scenario.

Posting Permissions

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