View Single Post
 
Old 02-01-2024, 11:35 AM
Donovahkiin Donovahkiin is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2023
Posts: 4
Donovahkiin is on a distinguished road
Question Macro Help - Search the last line of each page

Hello! I'm trying to write a macro that will search the last line of each page and tell me if it contains certain text. I'm having trouble. I wish the Page object would just have a .Next feature like Paragraph does...

Do you know an easy way to iterate through each page like that? My pages for transcription work are always 25 lines, so I tried moving the cursor down that many lines each loop as a workaround. But then if there was an indent earlier, it keeps that spacing on later pages instead of looking at the start of the line.

Here's what I have so far, and I can tell you what's wrong with it.

Code:
Sub WIPSearchLastLine()
    Dim aPage As Page
    On Error Resume Next
    For Each aPage In ActiveDocument.ActiveWindow.Panes(1).Pages
        Selection.MoveDown Unit:=wdLine, Count:=24
        If Selection.Range.Next.Characters(2).Text = "BY" Then
            Selection.Next.HighlightColorIndex = wdBrightGreen
        End If
        Selection.MoveDown
    Next aPage
End Sub
Problems:
- My "If" block always resolves as True. I know that's wrong, but I'm not sure why that is.
- I have to manually click my cursor to the start of the first page before running.
- It doesn't check the first characters in the last line, it's highlighting a handful of characters later relative to what indentation was on earlier pages.
- If the last line of a page is blank or short, it highlights the beginning of the line on the next page instead.

Full scope of project:
* For the last line of each page:
- If it starts with "BY" or starts with "EXAMINATION" or ENDS with a closing parenthesis ")"
- Bring it to my attention. Ideally, move the cursor there and just exit the program. So I can fix that line, then re-run the program. I just had it highlight so it's more obvious what my program's doing.

Any help I can get with this would be wonderful.
Reply With Quote