![]() |
|
#16
|
||||
|
||||
|
Perhaps you'd find it easier if we just captured para 6's content without point a range variable to it:
Code:
Sub Demo() Dim StrTxt As String StrTxt = ActiveDocument.Paragraphs(6).Range.Text If InStr(StrTxt, "NRCS") > 0 Then Call NRCS ElseIf InStr(StrTxt, "Washington") > 0 Then Call Washington ElseIf InStr(StrTxt, "FSA") > 0 Then Call FSA ElseIf InStr(StrTxt, "St Louis") > 0 Then Call St_Louis ElseIf InStr(StrTxt, "Kansas City") > 0 Then Call Kansas_City Else Call Default End If End Sub Sub NRCS() End Sub Sub Washington() End Sub Sub FSA() End Sub Sub St_Louis() End Sub Sub Kansas_City() End Sub Sub Default() End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#17
|
|||
|
|||
|
Macropod, Many thanks.
I will go look in my reference book. I know that once I get it going and understand it, I can branch out in my understanding later. I suspect you diagnose correctly that my weakness is with " point a range variable " so I know I must also look that up too. I get it that the test is "If InStr(StrTxt, "NRCS") > 0" -Thanks. And I get it that StrTxt is the variable declared as String. And I get it that StrTxt is declared to be all of paragraph6 in the Active Document. But I will have to go study to see why the test is "larger than zero" In COBOL we would have declared names for every possible position in the line/paragraph, then just said "If x equals "Washington" then perform Washington. |
|
#18
|
||||
|
||||
|
You should study the Instr function in the VBA help file. A result of 0 means the text doesn't exist.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#19
|
|||
|
|||
|
Another part of the issue is that of dealing with objects. Quite often when dealing with assignment with object we are just providing a shortcut to th eobject rather than actually doing anything with the object itself.
The following are equivalent 1 StrText=ActiveDocument.Pargraphs(6).range.text 2. set myRange = ActiveDocument.Paragraphs(6).range StrTxt= myRange.Text 3 With ActiveDocument.Paragraphs(6).range StrTxt=.text In VBA variables come in two basic sorts Value variables and Reference variables. Value variables are your traditional ones like strings, integers, single where the value is unique to the variable. Reference variables, otherwise known as variables objects, are different in that the value held by the variable is actually a pointer to the actual object and not the object itself. If your previous programming experience is in COBOL then you have my sympathies. I was recently doing some reading and found out that COBOL was designed by a committee that didn't include any computer scientists !!! |
|
#20
|
|||
|
|||
|
Macropod, I have located https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/instr-function
Here is part of it: comparison Dim SearchString, SearchChar, MyPos SearchString ="XXpXXpXXPXXP" ' String to search in. SearchChar = "P" ' Search for "P". ' A textual starting at position 4. Returns 6. MyPos = Instr(4, SearchString, SearchChar, 1) ' A binary comparison starting at position 1. Returns 9. MyPos = Instr(1, SearchString, SearchChar, 0) ' Comparison is binary by default (last argument is omitted). MyPos = Instr(SearchString, SearchChar) ' Returns 9. MyPos = Instr(1, SearchString, "W") ' Returns 0. Thank you very much. I have seen other help files like this. Returns a Variant ( Long ) The trouble is, I am not sufficiently advanced to be able to understand much of it. For example, In many help narratives, I see the word "Returns" -as in, "Returns a value of 6" Where the devil does it return to? What variable has 6 in it? How can I get-a-hold of it? So I can do some test like: If x = 6 then do something. And in the example in the above URL, the string that is searched is a literal. How can I search for "Washington" in a Word document? Like the following? Sub Demo () Dim StrTxt As String StrTxt = ActiveDocument.Paragraphs (6) . Range.Text Comparison Dim SearchString, SearchChar SearchString =StrTxt ‘String to search in. SearchChar = “Washington” If StrTxt, “Washington” > 0 Then Call Washington Endsub |
|
#21
|
|||
|
|||
|
Slaycock, I am certain that you are right. I am gathering that this "Object Orientation" is such a different concept - so I just don't get it.
I must work on this to understand what this means. In the mean time, thanks very much for your patience and help. |
|
#22
|
|||
|
|||
|
Another confusion is that the Office object models are very tightly bound to VBA and consequently the boundary between a VBA activity and a Word activity can get confused. A case in point is
Code:
StrTxt = ActiveDocument.Paragraphs(6).Range.Text InStr is a VBA function which searches with a VBA string variable. We can do the same operation as Instr within word itself using the .Find method of the range object. Code:
Sub test()
With ActiveDocument.Paragraphs(6).range.Find
.Text = "Kansas"
.Execute
If .Found Then
do something
End If
End Sub
Properties of objects can be treated as variables in that you can assign and retrieve information from a property. Methods are basically functions in that they return a value (or maybe just go off and do something) The reason they are called methods and properties rather than variables and functions is that within the object there may be a large amount of code that processes the input/output. So what is happening in Code:
StrTxt = ActiveDocument.Paragraphs(6).Range.Text Paragraphs is a method (because we can't assign to paragraphs) which returns a collection of Paragraph objects. You will see the word 'collection' a lot when looking at help pages for the Word object model and is refers to a group of Name, value pairs) The (6) is a shorthand version of saying give me .item(6) of the collection of paragraph objects (yes - .paragraphs(6) is equivalent to .paragraphs.item(6) because the .items() method is usually declared as the default method and so can be omitted) Range is a property which returns the location of the object in 'Word space'. The code behind the range object raises an exception (error) if you try to assign a value to a range that is part of an active document. Text is a property of the range object. |
|
#23
|
|||
|
|||
|
slaycock, thanks very much. I am at work right now, so I will read it at an opportune time. All your writing is worthy of very close and thorough reading. I will read it, put it down, then read it again, several times like this. Then after a few days, it will start to soak in, I know.
|
|
#24
|
||||
|
||||
|
Not really. Instr will tell you where in a string a substring begins, but that's all. Moreover, in a document with graphics, tables, etc., if you moved a range to an offset returned by Instr you'll often end up with a different range from what you'd get via Find.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#25
|
||||
|
||||
|
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#26
|
|||
|
|||
|
Macropod, thank you for your explanation of "Returns" I am studying it.
And thanks to slaycock for this discussion and your efforts. Your words are being put to good use. I think I know enough now to do the test, thanks to you. Now I have to compose the rest of the logic using the called Sub and maybe a "go to" whatever seems to be easy and will be easy to maintain, because these conditions will change. By the way, concerning Cobol, the old college textbooks encouraged programmers not to use "go to" -Instead, the recommendation was to perform paragraphs, similar to the Function and calling a Sub. Back in about 1982, at our large shop at the USDA, I was the first programmer to write a program that had no "go to" in it. The older programmers did not understand my code and I had a hard time selling this method to them. Style is habit forming and other styles are difficult to learn. Now I am the old guy that is having a difficult time learning something. |
|
#27
|
||||
|
||||
|
In VBA, GoTo is rarely needed; most of what you might need can be done via loops, Select Case & If tests. A check of the code provided in other threads in this forum should give you a few ideas.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#28
|
|||
|
|||
|
macropod, slaycock, I have decided not to use GoTo. It is mostly because of the style I am used to. Personally, I consider GoTo to be low-down and Neanderthal. Ha!
I am writing the Macros right now using what you have suggested. But I want to pass variables so I am studying how to use Functions. Actually, there are several lines/paragraphs that I am testing to see if the line/paragraph contains something. So I put a number in, here the fourth line is tested. Here is a rough draft, and this seems to work except now I realize I want to pass data back and forth between the calling and called macro/function. I know this must seem very simple to you, but this is a big milestone in learning for me: Dim StrTxt4 As String StrTxt4 = ActiveDocument.Paragraphs(4).Range.Text ‘This part clears the clipboard Dim clipboard As MSForms.DataObject Set clipboard = New MSForms.DataObject clipboard.SetText "" clipboard.PutInClipboard 'This part is the first IF & puts it in the clipboard If InStr(StrTxt4, "ASOC") > 0 Then clipboard.SetText "@asoc.usda.gov" clipboard.PutInClipboard Else If InStr(StrTxt4, "FSA") > 0 Then Call FSA Selection.HomeKey Unit:=wdStory End If End If |
|
#29
|
|||
|
|||
|
VBA subroutines (macros as you might refer to them) are either 'Sub' of 'Function' definitions
Sub FSA(<argument list> your code End sub Function FSA(<arguments>) <return value> 'return the value by assigning to FSA FSA = <return value> End Function arguments are defined as <ByRef|ByVal> <name> as <Type> or <ByRef|ByVal> Optional <Name> as <Type> or <ByRef|ByVal> Optional <Name> as <Type> = <DefaultValue> The <return value is just as <type> (because the function name acts as the variable) ByRef means pass by reference so any changes you make to the variable inside the Sub/FUnction will be seen outside the function. ByRef is the default so does not need to be included but ByVal must always be stated if that is what you want. ByVal passes a copy to the Sub or Function which is destroyed unless specifically returned by a function. It is considered good practise to only use functions and to retrieve data from the function by virtue of declaring parameters ByRef. The result returned by the function should indicate success or failure of the operation by returning a True/False or the new data item created by the function (e.g. the value of nothing for a fail and <value> is success> or if new data is not to be returned then a boolean True/False to indicate success or fail of the function. |
|
#30
|
|||
|
|||
|
Slaycock, Yes! Thank you! I am beginning to see some light at the end of the tunnel. I am looking at https://msdn.microsoft.com/en-us/lib...(v=vs.60).aspx and other things too.
And thank you for your good bits of information and mention of best practices. I try to follow best practices in life. I don't smoke, drink or chew or go out with girls who do.
|
|
|
|
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 |