Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-19-2017, 01:16 AM
slaycock slaycock is offline Find This Or That On Line Six of Word Document Windows 7 64bit Find This Or That On Line Six of Word Document Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

Oh good grief. I can see where I've gone wrong. I didn't look up wdGotoItem. I assumed the unit I wanted was wdLine and just checked that it existed. If I use wdGotoLine all works as expected with the exception that word is confused about what a line is. You can see this in action by stepping through

Code:
sub test()
    Set myRange = ActiveDocument.Content.GoTo(what:=wdGoToLine, which:=wdGoToAbsolute, Count:=6)
    myRange.Select
    myRange.Characters(1).Select
    If myRange.Information(wdFirstCharacterLineNumber) <> 6 Then
        MsgBox "Line 6 is not line 6!!!", vbOKOnly
    Else
        myRange.MoveEnd unit:=wdGoToLine
        MsgBox "Line 6 found->" & myRange.Text, vbOKOnly
    End If
    
    myRange.Select
    myRange.End = ActiveDocument.Content.GoTo(what:=wdGoToLine, which:=wdGoToAbsolute, Count:=7).End
    myRange.Select
end sub
With a document of

One
Two
Three
Four
Five
Six six six NRCS Washington FSA St Louis Kansas City six six six six six six six six six seven seven seven NRCS Washington FSA St Louis Kansas seven seven seven



In the above there is no paragraph marker at the end of line 6 so the last two lines (which may appear as a single line in your browser) are a single paragraph. This is important for the demonstration above.

So a revised version of my code which works as expected and only uses line 6 would be

Code:
Sub FindOnLineSix()

Dim myRange As range

    ' Not the use of wdGotoLine and not wdLine for the what:=
    Set myRange = ActiveDocument.Content.GoTo(what:=wdGoToLine, which:=wdGoToAbsolute, Count:=6)
    myRange.End = ActiveDocument.Content.GoTo(what:=wdGoToLine, which:=wdGoToAbsolute, Count:=7).Start
        
    ' Note the change from  elseif - elseif, like a case statement, exits after the first find (true condition)
    If InStr(myRange.Text, "NRCS") Then
        Debug.Print "I found NRC5"
    End If
    If InStr(myRange.Text, "Washington") Then
        Debug.Print "I found Washington"
    End If
    If InStr(myRange.Text, "FSA") Then
        Debug.Print "I found FSA"
    If InStr(myRange.Text, "St Louis") Then
        Debug.Print "I found St Louis"
    End If
    If InStr(myRange.Text, "Kansas City") Then
        Debug.Print "I found Kansas City"
    End If
    Else
        Debug.Print "Boo ho.  Nothing found"
    End If

End Sub
which of course points to a function for future use so the code above could be revised to

Code:
Sub FindOnLineSix()

Dim myRange As range

    
    Set myRange = GetLineRange(Line:=6)
        
    If InStr(myRange.Text, "NRCS") Then
        Debug.Print "I found NRC5"
    End If
    If InStr(myRange.Text, "Washington") Then
        Debug.Print "I found Washington"
    End If
    If InStr(myRange.Text, "FSA") Then
        Debug.Print "I found FSA"
    If InStr(myRange.Text, "St Louis") Then
        Debug.Print "I found St Louis"
    End If
    If InStr(myRange.Text, "Kansas City") Then
        Debug.Print "I found Kansas City"
    End If
    Else
        Debug.Print "Boo ho.  Nothing found"
    End If

End Sub

Function GetLineRange(Line As Long) As range
    Dim r As range
    Set r = ActiveDocument.Content.GoTo(what:=wdGoToLine, which:=wdGoToAbsolute, Count:=6)
    r.End = ActiveDocument.Content.GoTo(what:=wdGoToLine, which:=wdGoToAbsolute, Count:=7).Start
    Set GetLineRange = r
End Function
As I always say. It pays to read the word help pages carefully. I just need to follow my own advice
Reply With Quote
  #2  
Old 09-19-2017, 04:48 AM
macropod's Avatar
macropod macropod is offline Find This Or That On Line Six of Word Document Windows 7 64bit Find This Or That On Line Six of Word Document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by slaycock View Post
Oh good grief. I can see where I've gone wrong. I didn't look up wdGotoItem.
...
As I always say. It pays to read the word help pages carefully. I just need to follow my own advice
Problem is, though, the Word 'Help' file has wdLine, not wdGotoLine, as a wdGotoItem; there's even an example with it!
As for .GoTo(What:=wdGoToBookmark, Name:="\line"), the required workaround is indeed something like your function.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find This Or That On Line Six of Word Document Black line that is in word document that won't go away ! seyzna Word 7 05-22-2023 10:57 AM
Find This Or That On Line Six of Word Document Won't find Word document wblock@cnu.edu Word 4 08-23-2017 06:11 PM
Find This Or That On Line Six of Word Document Cannot find my word document - saved to usb mssodium1219 Word 3 04-01-2015 04:24 AM
Find This Or That On Line Six of Word Document Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
Word 2003 cuts the last line of my document off wordboy9317 Word 0 10-12-2009 08:44 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:48 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft