I've debated a little about whether to publish this post. Hopefully your practice will be so busy you wont have time to look at code (or can pay someone to do it). On the other hand, it might pay to at least understand some concepts in order to benefit from the power of automation. Bill and I need to set the stage for some simple (trust us) code that will make your life easier (really - trust us). This post is only for the most daring so if you don't like it - just don't read it.
Programs
A program is made up of a collection of commands for the computer. Think of it as a script in a play, the computer simply follows the script. For example a program that draws a circle on the screen might say:
1. Get access the screen to draw on
2. Grab a red pen
3. Draw a circle with the pen
4. Quit
Functions
We might want our program to draw many red circles all over the screen. In that case, it may be simpler to combine steps 2 and 3 into what is called a function. With a function we can say:function DrawRedCircle = 1) Grab a red pen then 2) draw a circle with the pen at a certain location. We put the steps inside the function.
Now our program might work like this:
1. get access to the screen
2. DrawRedCircle (at the top right of the screen)
3. DrawRedCircle (at the bottom left)
4. DrawRedCircle (middle)
5. Quit
Variables
Now lets say we want to write a function that averages several numbers together. Our program needs to 1) add all the numbers together and 2) divide them. Our function might look like this:
function average(numbers 50, 60,95,85)
...
SumOfTheNumbers=50+60+95+85
AverageOfTheNumbers=SumOfTheNumbers/4
...
What the expression "SumOfTheNumbers=" did is put the result of the addition away in a memory location somewhere. SumOfTheNumbers (and AverageOfTheNumbers) is called a variable. A variable is kind of like a pocket that you can put stuff in and use it later. In this case SumOfTheNumbers is storing the sum of the numbers, 290. AverageOfTheNumbers holds the average, 72.5.
Function Parameters
Sometimes a function needs to take certain inputs and do some work on them, these inputs are passed into variables called parameters. For example, our Average function could take our numbers as parameters. For example our function would look like this.
function AverageFourNumbers(FirstNumber, SecondNumber, ThirdNumber, FourthNumber)
{
SumOfTheNumbers=FirstNumberSecondNumber+ThirdNumber+FourthNumber
AverageOfTheNumbers=SumOfTheNumbers/4
}
As you can see, the function adds together the four parameters FirstNumber, SecondNumber, ThirdNumber, FourthNumber and then averages. To use our new function we code:
AverageFourNumbers(50, 60,95,85)
Function Returns
We may want our functions to spit out a value so that our program can either store it or do more processing on it. An output from a function is called a return value. For example, our average function would be far more useful if it returned an average so that we could write that to a file or something. A function that returns the average might look something like this:
function AverageFourNumbers(FirstNumber, SecondNumber, ThirdNumber, FourthNumber)
{
SumOfTheNumbers=FirstNumberSecondNumber+ThirdNumber+FourthNumber
AverageOfTheNumbers=SumOfTheNumbers/4
return AverageOfTheNumbers
}
To use this we can write:
MyGPA=AverageFourNumbers(50, 60,95,85)
Lets pretend we wrote a function that would store a GPA to a file. We'd probably want it to take the GPA and a file to save it in as parameters. Calling the function might look something like this:
StoreGPA(MyGPA, MyFile)
Objects
An object is a way to wrap related variables and functions into one package. A good example of "relatedness" is a program that models a bouncing ball. In that case, we need a number of variables to describe the ball. A bouncing ball also has a number of actions that it performs, these actions are stored in functions. When talking about objects programmers use a little different terminology. Variables that are inside the object are called properties. Functions that are inside the object are called methods.Lets "describe" a ball object:
Ball
{
Properties=
Color
Size
BounceSpeed
Type
etc.
Methods=
DoBounce
Stop
}
We can set the properties of our ball something like:
Ball.Color = red
Ball.size =15
Ball.BounceSpeed =2
We can make the ball bounce by calling the DoBounce method (assuming we/others have done the programming for the method):
Ball.DoBounce
Repitition and Evaluation
A program repeats a process by doing what is called a loop. With loops you can tell to computer to repeat a group of commands for a certain number of times or until a condition occurs.An example of looping 100 times:
For Number=1 to 100
...do some stuff
Next Number
An example of looping until a condition occurs:
Number=1
Do Until Number=100
Number=Number+1
...do some stuff
Loop
Both of the examples did the same thing but one used what is known as a For Loop which does an operation a set number of times. The other example used a conditional loop that stops when a condition occurs .Both types are useful in different situations.
Evaluation is accomplished with an if statement. An if statement allows you to test if a condition has occurred. For example:
if Number=100 then
DisplayMessage("Number is one hundred")
end if
The above example uses an if statement to test whether the variable Number is 100. If it is then the pretend DisplayMessage function is called.
Strings and Numbers
A computer stores to general types of information, words and numbers. Collections of words are called strings. Numbers can come in several types integers,floats and doubles for example. An integer is a whole number (no decimals) and can be positive or negative. A float can be a decimal. Strings are enclosed in quotations. For example:
MyString = "This is an example of a string"
Numbers don't need quotations. For example:
MyNumber = 1
Putting it all together
To tie this all together, lets look at the macro we recorded in this post. The code strips all the returns so that pastes from Acrobat files aren't all messed up. It looks like this:
Sub Macro1()
'
' Macro1 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
This function is in a language called Visual Basic (VB). VB is the language that the Microsoft Office applications use for their automation. Office allows you to do tons of cool stuff to save time with VB.
The code shows a function called Macro1. VB makes a distinction between functions that return a value (see the returns section above) and functions that do not. Functions that return a value are simply called a "function." Functions that do not return a value are (confusingly) called a "sub procedure." Hence, the "sub Macro1" and "end sub" in the code above.
After the function name line you see three lines that start with a single quote ('). In VB a single quote is the beginning of a comment. Anything after a single quote is not run by the computer, its not considered code. Comments allow you to "show your work" like in algebra. Basically, you can write notes about what each line is supposed to do etc. The 3 lines of comments above just give the name of the function.
Next we see that the function does alot with some "Selection" thing. Selection is an object (see the discussion of objects above). Inside the Selection object there are various properties and methods. The Macro1 function sets and runs those properties an methods to get the result we want. The first two lines of code are:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Remember placing a period after an object name is the way to access properties and methods within the object (its kind of like a door to the inside of the object). In this case, the function accesses the Find property. Actually Find is itself an object. The Selection object has a Find object inside. To get even more wild, the Find object has a Replacement object inside of it. ClearFormatting is a method. So whats going on? Well the Find and Replace objects represent the Find what and Replace with fields on the Find and Replace box (at right). Word allows you to find words or phrases with specific formatting, like bold/italic/etc. The ClearFormatting function does the equivalent of clearing that setting out on the Find and Replace box for both the Find what (our function's Find object) and the Replace with (our function's Replacement object).
The next line of code says:
With Selection.Find
A "With" block is just a shortcut so that we don't have to type the object name over and over. It simply means that all the properties and methods used following the "With" will belong to the Find object (Selection.Find).
The code that follows isn't all that important to understand specifically. It just sets up our find by setting the properties of the find object. These properties will be used when we do the search to determine how the search will be done. Four properties are important though.
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
The Text property is what we are searching for. Here, the paragraph character (^p). The Text property of the Replacement object is what we will replace the paragraph character with. Here, a space. Forward tells the search to look forward and wrap tells it to wrap the search around when it gets to the end of the document.
The last line of the function calls the Find object's Execute method with a parameter telling it to replace all occurances of the string we are looking for (^p).
__________
Share This Post!
0 comments:
Post a Comment