Results 1 to 4 of 4

Thread: Search single line in Word Document using VBA

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2015
    Posts
    16

    Search single line in Word Document using VBA

    I need to know how to use selection to either search for a string in a specified line on a specified page in a word document, or i have my code set up currently to select the entire line if i could search for a specific variable in the selection i could use that too.

    The purpose being that i have to check to see if a piece of text was added to the string on the last line of each page the text reaches.

    So far i keep getting stuck at this one part when trying to figure it all out.

    Suffice to say i need to check if "????Z" exists on the last line of every page that has text on the last line of it


    ive spent multiple hours on google and toying around with code to try to figure this out... i cant please help.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Search single line in Word Document using VBA

    if the lines are separated by carriage returns then it is quite easy, but i assume your lines are just a wrapped paragraph

    try like this to select the last line
    Code:
    ThisDocument.Range.Select
    With Selection
        .Start = .End
        .HomeKey wdLine, wdExtend
    End With
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2015
    Posts
    16

    Re: Search single line in Word Document using VBA

    Quote Originally Posted by westconn1 View Post
    if the lines are separated by carriage returns then it is quite easy, but i assume your lines are just a wrapped paragraph

    try like this to select the last line
    Code:
    ThisDocument.Range.Select
    With Selection
        .Start = .End
        .HomeKey wdLine, wdExtend
    End With
    Dude, you the man. and as luck would have it every single line is seperated by carriage returns thank you so much, now this code looks like it will search the last line of the document, do you know of any way to search the last line of each page in the document ? preferrably only if it has text in it ? Thank you again this is an awesome starting point.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Search single line in Word Document using VBA

    as luck would have it every single line is seperated by carriage returns
    the above would work on wrapped paragraphs anyway

    do you know of any way to search the last line of each page in the document
    here is something you can test
    Code:
    Dim r As Range, firstpage As Boolean
    ThisDocument.Range.Select
    With Selection
        .Start = .End
        Do
            .HomeKey wdLine, wdExtend
            
            Do While Len(.Text) = 1
                .Move wdCharacter, -1  ' allows for empty lines at end of document
                .HomeKey wdLine, wdExtend
            Loop
            Debug.Print .Text
            ' search selection here
            If firstpage Then Exit Do
            .GoTo wdGoToPage, wdGoToPrevious
            If .Start = 0 Then firstpage = True
            .GoTo wdGoToPage, wdGoToNext
            .Move wdCharacter, -1
            .HomeKey wdLine, wdExtend
        Loop
        
    End With
    i am not sure what will happen if a page is empty of text, it may loop into the previous page, causing it to be skipped
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width