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