1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Your First 'Job Worthy' Program
5 - Take It One Step At A Time!
Debug Options

Back in the 'good old days' (They were really 'bad old days'. Trust me on that!), programmers used to spend hours and hours 'bench checking' their programs before ever trying them out on a real computer. (When I was an undergraduate engineering student, I was allocated one run a week to try out my new programming skills on a real computer. You better believe I checked my program carefully first!) The primary method was to step through the program one statement at a time to make sure it did what you wanted it to do.

In the 'good old days' we had to interpret what the computer would actually do by just looking at the program statements. Today, we do basically the same thing, but like so many things, we use the computer itself to do the hard and boring part. We call this process, debugging.

 Lesson 3
Your First 'Job Worthy' Program

1 - Analysis - Plan Your Work

2 - Put Your Assistant To Work Again

3 - The Form Letter Macro

4 - Help!

5 - Take It One Step At A Time!

The debug options that are available to you can be found on the Debug menu. The first four 'Step' options are the ones you will use most often. The Into, Over and Out refer to the current running procedure. So, for example, when you press F8 (Step Into), Visual Basic starts your macro, goes to the very first statement (which is indicated by the arrow and the highlighting), and then stops and waits for more instructions. The other 'Step' options are simply alternative ways to tell VB where to stop and wait. And there are more ways, too. To read more about this, start your VB Help system and search for Trace Code Execution.

Stepping Through The Code

The main reason for using this technique is to see whether VB is executing loops, conditional statements (IF statments, for example) and subroutine calls when you expect it to. Up to this point, we haven't seen any because the heavy lifting has all been done by objects. So to illustrate program stepping, let's code basically the same thing that the Macro Recorder did for us ... but do it with conditional logic and loops.

The FormLetter macro searches for a particular sequence of characters - (1) and (2) - and replaces them with a different text string. Here's some code that will do that for (1). (Again, a lot of continuation characters have been used to keep the lines short for the web page.)

Set myDoc = _
Dim NewDoc As String
Dim PFlag As Integer
PFlag = 0
For Each Searchword In myDoc
    If Searchword = "(" _
        Then PFlag = 1
    If Searchword = "1" _
        And PFlag = 1 _
        Then PFlag = 2
    If Left$(Searchword, 1) = ")" _
        And PFlag = 2 Then
        NewDoc = _
        Left$(NewDoc, Len(NewDoc) - 2)
        NewDoc = NewDoc & "Mr. Publisher Dude"
        NewDoc = NewDoc & Searchword
    End If
Next Searchword

This does approximately the same thing as this Macro Recorder code:

With Selection.Find
    .Text = "(1)"
    .Replacement.Text = _
        "Mr. Publisher Dude"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
With Selection
    If .Find.Forward = True Then
        .Collapse Direction:=wdCollapseStart
        .Collapse Direction:=wdCollapseEnd
    End If
    .Find.Execute Replace:=wdReplaceOne
    If .Find.Forward = True Then
        .Collapse Direction:=wdCollapseEnd
        .Collapse Direction:=wdCollapseStart
    End If
End With

In defense of the Macro Recorder code ... which may look more difficult:

First ... It's conservative because it takes into account a lot of possible complications that just don't happen normally. Some of it could be deleted and it would work about the same way.

Second ... Because it mainly executes just once to do it's job (rather than looping), it will run faster and more efficiently in nearly all cases and especially in larger text documents. Remember, the programmers who wrote the objects that are really doing the work were really great programmers and they did a really great job. The objects let you take advantage of their expertise.

Third ... The looping code requires some confusing objects too! You just can't get away from 'em so you might as well learn to use 'em!

Immediate and Watch

You can see for yourself the process of stepping through a program by doing a 'copy and paste' of the looping code into your own macro. Step through it to see what it does.

To really understand what the program does, however, you also have to know what information is stored in all of the variables. To examine variables, you need the Immediate and Watch windows. You can find menu commands to show those windows under the View menu item.

Here's the Immediate window showing the value of PFlag inside one of the loops. Check out the Debug object and the Watch and Immediate windows in the VB Help system for more.

Immediate Window

Although a lot of things could improve the FormLetter macro, probably the most important missing element is that in order to change ANYTHING, you have to change the macro source code! In particular, you have to change the macro source to change the Name which replaces (1) or the 'Great Work Description' which replaces (2). This is just not satisfactory! What the program needs more than anything else is a way to specify that information before it runs.

Lesson 4 ... which will be in your email next ... will go into how things like this are added to a VB program in detail. In Lesson 4, we go well beyond the Macro Recorder. If any of you have worked with the full VB 6 development system, you'll find the techniques in Lesson 4 are more what you have seen before.

©2016 About.com. All rights reserved.