Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 09-18-2017, 06:12 AM
StephenRay StephenRay is offline Windows 7 64bit Office 2010 64bit
Advanced Beginner
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 43
StephenRay is on a distinguished road
Default Find This Or That On Line Six of Word Document

My friends,
I want to write a Macro for a Word Document that will find out if the 6th line on my document contains one of these:
NRCS
Washington
FSA
St Louis
Kansas City

Maybe line six would contain one of these, maybe not. If one is found, the macro should perform a particular function or go to some label. Each different result would perform a different function. If none is found, the macro would do something else, or allow control to pass to the next statement.

I could write a “find” to search the whole document, but I want to be more precise and look only on line 6. The “find” statements seem so long and awkward.
I am guessing that the answer uses a Range.

Thanks!
Reply With Quote
  #2  
Old 09-18-2017, 01:54 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,353
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

What do you mean by 'line' six? Is that paragraph six or just what Word puts a 6 against if you turn line numbering on?
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 09-18-2017, 03:03 PM
slaycock slaycock is offline Windows 7 64bit Office 2013
Competent Performer
 
Join Date: Sep 2013
Posts: 117
slaycock is on a distinguished road
Default

A very crude solution

Code:
Sub FindOnLineSix()

Dim myRange As range

Set myRange = ActiveDocument.Content.GoTo(what:=wdLine, which:=wdGoToAbsolute, Count:=6)
    
    If InStr(myRange.Text, "NRCS") Then
        Debug.Print " I found NRC5"
    ElseIf InStr(myRange.Text, "Washington") Then
        Debug.Print "I found Washington"
    ElseIf InStr(myRange.Text, "FSA") Then
        Debug.Print "I found FSA"
    ElseIf InStr(myRange.Text, "St Louis") Then
        Debug.Print "I found St Louis"
    ElseIf InStr(myRange.Text, "Kansas City") Then
        Debug.Print "I found Kansas City"
    Else
        Debug.Print "Boo ho.  Nothing found"
    End If


End Sub
Reply With Quote
  #4  
Old 09-18-2017, 03:45 PM
slaycock slaycock is offline Windows 7 64bit Office 2013
Competent Performer
 
Join Date: Sep 2013
Posts: 117
slaycock is on a distinguished road
Default

Bum Hoist by my own petard as I posted before checking (it *IS* time for bed for me)

The code above doesn't work. I've done some fiddling and I can't get an approach using goto with wdline as the unit to work. Hint the range returned to myRange should be the start of line 6 but is actually zero.

Just goes to show that you think you are getting an understanding of word and you then fall flat on your face.
Reply With Quote
  #5  
Old 09-18-2017, 05:02 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,353
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Yes, the Word doco on this is lacking. You could use:
Code:
Sub Test()
Dim myRange As Range
Set myRange = ActiveDocument.Range.GoTo(What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=6)
Set myRange = myRange.GoTo(What:=wdGoToBookmark, Name:="\line")
myRange.Select
End Sub
but, when you see what's selected, you'll realise the range often doesn't span the entire line.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #6  
Old 09-18-2017, 11:21 PM
slaycock slaycock is offline Windows 7 64bit Office 2013
Competent Performer
 
Join Date: Sep 2013
Posts: 117
slaycock is on a distinguished road
Default

Your code seems to select line 6 OK for me.

I followed the help at

https://msdn.microsoft.com/VBA/Word-...to-method-word

The problem seems to relate to the wdLine constant as

Code:
Set myRange = ActiveDocument.StoryRanges(wdMainTextStory).Characters(1)
myRange.MoveEnd unit:=wdWord
myRange.MoveEnd unit:=wdLine
selects the first word in the document but gives a bad parameter message when the unit is wdLine.
Reply With Quote
  #7  
Old 09-18-2017, 11:34 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,353
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by slaycock View Post
The problem seems to relate to the wdLine constant
Indeed it is. Note how in post #3 you used:
Set myRange = ActiveDocument.Content.GoTo(what:=wdLine, which:=wdGoToAbsolute, Count:=6)
(as per the MS doco), whereas in post #5 I used:
Set myRange = ActiveDocument.Range.GoTo(What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=6)
That gets myRange to the start of line 6 and, according to MS:
Set myRange = myRange.GoTo(What:=wdGoToBookmark, Name:="\line")
should expand myRange to span the whole line. It's results are inconsistent, however, and often fails to span the entire line.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #8  
Old 09-19-2017, 01:16 AM
slaycock slaycock is offline Windows 7 64bit Office 2013
Competent Performer
 
Join Date: Sep 2013
Posts: 117
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
  #9  
Old 09-19-2017, 04:48 AM
StephenRay StephenRay is offline Windows 7 64bit Office 2010 64bit
Advanced Beginner
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 43
StephenRay is on a distinguished road
Default

Gentlemen, thank you all for this discussion. I will look at all of this this morning. It is 6:30 A.M. here in Kansas City, Missouri.
Macropod, I said line six. But I see now, more precision is necessary. I should have said paragraph 6, because after looking, yes, every line has a paragraph mark after it.
I will go digest all this today.
Many thanks!

Last edited by StephenRay; 09-19-2017 at 04:50 AM. Reason: additions
Reply With Quote
  #10  
Old 09-19-2017, 04:48 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,353
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
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
[MS MVP - Word]
Reply With Quote
  #11  
Old 09-19-2017, 04:50 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,353
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by StephenRay View Post
I said line six. But I see now, more precision is necessary. I should have said paragraph 6, because after looking, yes, every line has a paragraph mark after it.
Getting the 6th paragraph is ridiculously easy by comparison:
ActiveDocument.Paragraphs(6).Range
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #12  
Old 09-19-2017, 05:42 AM
slaycock slaycock is offline Windows 7 64bit Office 2013
Competent Performer
 
Join Date: Sep 2013
Posts: 117
slaycock is on a distinguished road
Default

Distant sounds of head repeatedly hitting brick wall........
Reply With Quote
  #13  
Old 09-19-2017, 06:17 AM
StephenRay StephenRay is offline Windows 7 64bit Office 2010 64bit
Advanced Beginner
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 43
StephenRay is on a distinguished road
Default

Slaycock, Thank you for all your hard work. I am learning from what you wrote and I know I will use it someday.

Using MacroPod's suggestion, how about the following?


Sub FindOnLineSix()

ActiveDocument.Paragraphs(6).Range
Dim myRange As range.
If InStr (myRange.Text, “NRCS”) Then
Debug.Print “I found Washington”
If InStr (myRange.Text, “Washington”) Then
Debug.Print “I found Washington”
If InStr (myRange.Text, “St Louis”) Then
Debug.Print “I found St Louis”
If InStr (myRange.Text, “Kansas City”) Then
Debug.Print “Kansas City”

End Sub
Reply With Quote
  #14  
Old 09-19-2017, 11:56 AM
slaycock slaycock is offline Windows 7 64bit Office 2013
Competent Performer
 
Join Date: Sep 2013
Posts: 117
slaycock is on a distinguished road
Default

There are a number of mistakes

i would recommend you google

vba if statement
vba declaring variables
vba set statement

ActiveDocument.Paragraphs(6).Range

Range is a property that returns a range object

it can be used in two ways.

1. to access further properties so you can set a value

e.g

Code:
ActiveDocument.Paragraphs(6).Range.Text="Hello world"
2. assign the range to another variable

Code:
set myRange = ActiveDocument.Paragraphs(6).Range.Text="Hello world"
means that myRange points to the range of paragraph(6) of the active document.

Range is an 'object' which is why you have to use set rather than just

Code:
myRange = ActiveDocument.Paragraphs(6).Range.Text="Hello world"
as in

Code:
myInteger = 4
So at the moment you code does not use the value returned by the statement

Code:
ActiveDocument.Paragraphs(6).Range

The variable myRange is declared but nothing is ever assigned to it.

The if statement exists in two forms (three if you want to b pedantic)

1. Use on a single line

If <condition> then <do something

2 Use over a number of lines

if <condition then
<do something>
ElseIf <condition> then
<do something else>
Else
< do stuff not captured by the above conditions>
End if

The else and elseif are optional statements. Else can only occur once but elseif can occur as many times as you want.

Your code misses out the mandatory Then and does not close with the End if.

The third version is called the immediate if and is a shorthand versions of

If <condition> then
do one statement
Else
do one statement
end if

and takes the form

<value> =Iif(<condition>, true statement, false statement)


Its very commonly used when you need to set a variable to one of two values depending on something that happened elsewhere.
Reply With Quote
  #15  
Old 09-19-2017, 12:55 PM
StephenRay StephenRay is offline Windows 7 64bit Office 2010 64bit
Advanced Beginner
 
Join Date: Jan 2012
Location: Overland Park, Kansas
Posts: 43
StephenRay is on a distinguished road
Default

Slaycock, I must go read about the Immediate If Sounds cool. I suspect I can understand that after reading.

The part I don't get is the condition using Range. I want it to test like this:
If Paragraph 6 has Washington then do something1
Else If Paragraph 6 has Kansas City then do something2
Else If Paragraph 6 has NRCS then do something3
Else If Paragraph 6 has FSA then do something4
Else If Pafagraph 6 has St Louis then do something6
Else do something7
Reply With Quote
Reply
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Won't find Word document wblock@cnu.edu Word 4 08-23-2017 06:11 PM
Black line that is in word document that won't go away ! seyzna Word 4 06-05-2016 01:56 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


All times are GMT -7. The time now is 02:34 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft