• Test for End of Document (Word 97)

    Author
    Topic
    #385795

    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

    Viewing 0 reply threads
    Author
    Replies
    • #666905

      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?

      • #667029

        Yes. Thanks

      • #667216

        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

        • #667314

          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. grin

          • #667409

            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

            • #667562

              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.

            • #667773

              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.

            • #667827

              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.

            • #667867

              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.

            • #667589

              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.

        • #667331

          What about modifying Jefferson’s code sample along the lines of
          While Selection.Paragraphs(1).End < ActiveDocument.Content.End – 1
          'Do Stuff
          Wend

          • #667342

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

            While Selection.Paragraphs(Selection.Paragraphs.Count).End < ActiveDocument.Content.End – 1

            StuartR

    Viewing 0 reply threads
    Reply To: Test for End of Document (Word 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: