![]() |
|
#1
|
|||
|
|||
|
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
- 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. |
|
#2
|
||||
|
||||
|
Do you mean LINE or PARAGRAPH? It is highly likely that the last character on any given page is either a space or a paragraph mark although the last character you can see might be a ")"
This code does what you asked for (but perhaps not what you meant). I've dealt with a trailing space but not with a trailing paragraph mark. Code:
Sub LastLiner()
Dim aRng As Range, rngPage As Range, rngLine As Range, sText As String
Set rngPage = ActiveDocument.Bookmarks("\Page").Range
Do While rngPage.End + 1 < ActiveDocument.Range.End
rngPage.MoveEnd Unit:=wdCharacter, Count:=-1
rngPage.Collapse Direction:=wdCollapseEnd
rngPage.Select
Set rngLine = ActiveDocument.Bookmarks("\Line").Range
rngLine.Select
sText = Trim(rngLine.Text)
If sText Like "BY*" Or sText Like "EXAMINATION*" Or sText Like "*)" Then
Exit Do
Else
Selection.Collapse Direction:=wdCollapseEnd 'moves selection to top of next page
Set rngPage = ActiveDocument.Bookmarks("\Page").Range
End If
Loop
End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#3
|
|||
|
|||
|
Thank you. I was indeed asking for the last line on each page, not the last paragraph on each page. When I'm done with a transcript, I need to make sure certain things don't split between more than one page. So if the end of a page has the first line of one of those things, I need to go through and push that part one line further down. This was to try and help me automate most of that check.
Very good! So it looks like it checks later from this point in the document, so I still need to place the cursor on the first page before running it. That's fine. I went through and tested it. It looks like it works perfectly for BY and EXAMINATION. I can get it to work with the parenthesis. Thank you so much! |
|
#4
|
||||
|
||||
|
I think you need to post a sample document that shows a parenthesis on the last line of a page. I suspect it is followed by a paragraph mark or perhaps a period but will need to see your actual document to verify what is really there so the code can be adapted to deal with that scenario.
I coded it to check from the current position because of how you wanted to run the code. If we keep ending the macro because we found an instance that you want to manually do something to, it would be best to restart the macro from that point rather than going back to the top to start over on pages we have already processed.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Macro to bold and center the first line of each page.
|
MDom | Word VBA | 4 | 02-21-2023 09:43 PM |
| 1st line of 4-line poem centrally aligned; how to get lines 2-4 to start at same location on page | Swarup | Word | 6 | 09-16-2022 11:07 AM |
| Section and Line Break leaving line on previous page | Stonehands99 | Word | 5 | 08-13-2019 08:59 PM |
Page no's in Footer: p. "9" is one line higher than all other page no's: How to bring it down 1 line
|
Swarup | Word | 24 | 12-30-2018 05:11 PM |
macro to add brackets to each line and add single quotes to each word in the line
|
bracketandquotes | Word VBA | 17 | 02-16-2015 03:51 PM |