Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #16  
Old 09-19-2017, 01:11 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,331
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


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
[MS MVP - Word]
Reply With Quote
  #17  
Old 09-20-2017, 05:04 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

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.
Reply With Quote
  #18  
Old 09-20-2017, 05:40 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,331
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 get it that the test is "If InStr(StrTxt, "NRCS") > 0" -Thanks.
...

But I will have to go study to see why the test is "larger than zero"
Your should study the Instr function in the VBA help file. A result of 0 means the text doesn't exist.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #19  
Old 09-20-2017, 08:03 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

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 !!!
Reply With Quote
  #20  
Old 09-20-2017, 08: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

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
Reply With Quote
  #21  
Old 09-20-2017, 08:15 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, 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.
Reply With Quote
  #22  
Old 09-20-2017, 09:45 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

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
With this line we are copying the text of the range object to a VBA string variable.

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
But as you can see it is a more complex operation because it is a much more complicated activity than just searching for a sub string within a string. ie using find we can search for text that has formatting information as well as the substring - 'Kansas' in style 'Body Text' overlaid with 'Red' and 'Bold' and 'Underline' character formatting.

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
ActiveDocument is an object

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.
Reply With Quote
  #23  
Old 09-20-2017, 10:14 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, 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.
Reply With Quote
  #24  
Old 09-20-2017, 02:07 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,331
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
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.
Not really. Instr will tell you where in a string a substring begins, bit 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
[MS MVP - Word]
Reply With Quote
  #25  
Old 09-20-2017, 02:12 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,331
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
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.
What Instr 'returns' is a number indicating where in the string the first instance of your substring occurs. A '6', for example, means it starts at the 6th character. The >0 test I used merely tells us whether the number returned by Instr is greater than 0; if it is, the substring has been found (we don't really care where); otherwise it hasn't.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #26  
Old 09-22-2017, 07:18 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

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.
Reply With Quote
  #27  
Old 09-22-2017, 03:12 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,331
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

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
[MS MVP - Word]
Reply With Quote
  #28  
Old 09-26-2017, 07: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

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
Reply With Quote
  #29  
Old 09-26-2017, 07:54 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

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.
Reply With Quote
  #30  
Old 09-26-2017, 08:44 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, 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.
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 04:38 PM.


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