![]() |
|
|
|
#1
|
|||
|
|||
|
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
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
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
|
|
#2
|
||||
|
||||
|
Quote:
As for .GoTo(What:=wdGoToBookmark, Name:="\line"), the required workaround is indeed something like your function.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Black line that is in word document that won't go away !
|
seyzna | Word | 7 | 05-22-2023 10:57 AM |
Won't find Word document
|
wblock@cnu.edu | Word | 4 | 08-23-2017 06:11 PM |
Cannot find my word document - saved to usb
|
mssodium1219 | Word | 3 | 04-01-2015 04:24 AM |
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 |