![]() |
#1
|
|||
|
|||
![]()
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! |
#2
|
||||
|
||||
![]()
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 [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
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 |
#4
|
|||
|
|||
![]()
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. |
#5
|
||||
|
||||
![]()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
![]()
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 |
#7
|
||||
|
||||
![]()
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 [Fmr MS MVP - Word] |
#8
|
|||
|
|||
![]()
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 ![]() |
#9
|
|||
|
|||
![]()
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 |
#10
|
||||
|
||||
![]() Quote:
As for .GoTo(What:=wdGoToBookmark, Name:="\line"), the required workaround is indeed something like your function.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
||||
|
||||
![]() Quote:
ActiveDocument.Paragraphs(6).Range
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
![]()
Distant sounds of head repeatedly hitting brick wall........
|
#13
|
|||
|
|||
![]()
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 |
#14
|
|||
|
|||
![]()
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" Code:
set myRange = ActiveDocument.Paragraphs(6).Range.Text="Hello world" Range is an 'object' which is why you have to use set rather than just Code:
myRange = ActiveDocument.Paragraphs(6).Range.Text="Hello world" Code:
myInteger = 4 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. |
#15
|
|||
|
|||
![]()
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 |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
seyzna | Word | 7 | 05-22-2023 10:57 AM |
![]() |
wblock@cnu.edu | Word | 4 | 08-23-2017 06:11 PM |
![]() |
mssodium1219 | Word | 3 | 04-01-2015 04:24 AM |
![]() |
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 |