Investigating Students' Redrafts: The BASIC Way

  1. Step by Step Macro Creation
  2. The BASIC Code (Copy From Here):
  3. The BASIC Code Explained:
  4. Program Pointers

Step by Step Macro Creation

I was really hoping that the big block of BASIC code would not scare anyone away. But I know from experience how much trouble it is to type code. More importantly, it is so easy to type one letter wrong, and then the program may not run at all. So here are all the steps on how to get to the MSWord macro builder and a complete code listing.

Be warned: This page contains numerous graphics (mostly in the form of screen shots), which means loading time can be very slow at times. To compensate for this, I have replaced the graphics with a common placeholder. If you want to see any of the graphics, click on the button (Load This Graphic) next to the placeholder.

To use this guide, start MSWord while keeping this browser window open:

  1. Start by creating a macro one through the menu: Tools, Macro.
  2. In the Macro dialog box, name the macro choose Record and name it "Compare."
  3. When the macro begins recording, stop it by clicking on the stop button.
  4. To edit the BASIC code of the macro, again enter the macro dialog box: Tools, Macro.
  5. Open the macro window by selecting the macro you just recorded, "Compare" and then clicking Edit.
  6. The coding window will open with the "Sub Main" and "End Sub" commands already in place. These can be DELETED as they are included in the code example below. Copy the code below and then paste it into this open macro editing window.
  7. After coding is done, you can save it through the File, Save menu. To save more time, you may want to give the macro a shortcut keyboard command. This is assigned in the Tools menu under Customize.
  8. Click on the Keyboard tab so that you can assign a shortcut to the keyboard, although you could also place shortcuts on the tool bar or even in a menu. In the Categories window, scroll down to Macros and select it. Next, select the macro you want to assign the shortcut to, in this case "Compare." Click in the Press New Shortcut Key text field, and then type on your keyboard the shortcut you want to start the macro. Finally, click on the Assign button to complete the process, then Close the customizing window.

After assigning a shortcut, anytime you press those keys, the program to compare students' drafts will run.

The BASIC Code (Copy From Here):

Copy this block of code by using your mouse to select it, then click on the Edit menu and Copy command of your browser (select all the code by clicking in front of the first line at the word "Sub" then scroll the browser window until you can see the last line of code; now hold down the Shift key and click just after the last word "function). Next, switch to MSWord where you have the macro editing windows open and then paste. REMEMBER to delete the already existing "sub" and "end sub" words in your macro window. These words should only appear ONCE.

Sub MAIN
REM NAME THIS MACRO: COMPARE
REM OPEN USER PATHS TO DISK
On Error Goto PROBLEM 'send to error handler if no file
Open "c:\compare.txt" For Input As #1
Read #1, ADraft$, AReDraft$
Close #1
START:
On Error Goto OVER 'Error handling is over so jump to end of sub on error
REM SEND TO FUNCTION TO INCREMENT
ADraft$ = Increment$(ADraft$)
AReDraft$ = Increment$(AReDraft$)
REM CREATE USER DIALOG BOX
ADraft$ = InputBox$("Where Is your first draft? " + "Include the whole PATH. " + " Example: C:\ENG\COMP1\25.TXT", "First Draft", ADraft$)
ARedraft$ = InputBox$("Where Is your second draft?", "Second Draft", ARedraft$)
REM SAVE USER PATHS TO DISK
Open "c:\compare.txt" For Output As #1
Write #1, ADraft$, ARedraft$
Close #1
REM MACRO RECORDED MENU ACTIONS
FileOpen .Name = AReDraft$, .ConfirmConversions = 0, .ReadOnly = 1, .AddToMru = 0, .PasswordDoc = "", .PasswordDot = "", .Revert = 0, .WritePasswordDoc = "", .WritePasswordDot = "" ToolsRevisions .MarkRevisions = 0, .ViewRevisions = 1, .PrintRevisions = 1
ToolsCompareVersions .Name = ADraft$
Goto OVER 'If there is no error, skip error handler
PROBLEM:
ADraft$ = ""
AReDraft$ = ""
Err = 0
On Error Goto OVER
Goto START
OVER:
End Sub
Function INCREMENT$(File$)
REM INCREMENT THE FILE IF IT IS A NUMBER
File$ = LTrim$(File$)
Letters = Len(File$)
FileEnd$ = Right$(File$, 4)
For i = (Letters - 4) To 0 Step - 1
If Mid$(File$, i, 1) = "\" Or Mid$(File$, i, 1) = ":" Then
FileMid$ = Mid$(File$,(i + 1),((Letters - 4) - i))
FileFront$ = Left$(File$, i)
i = 0
End If
Next i
If Val(FileMid$)<>0 Then
FileNumber = Val(FileMid$)
FileNumber = FileNumber + 1
File$ = FileFront$ + LTrim$(RTrim$(Str$(FileNumber))) + FileEnd$
End If
Increment$ = File$
End Function


The BASIC Code Explained:

Here is the same block of code, but explained in detail. The black text are the actual lines of code while the blue text explains the line of code immediately above.

Sub MAIN
This starts a sub proceedure which is terminated by "end sub" For this program we will use only one sub proceedure.
REM NAME THIS MACRO: COMPARE
REM in the front of any line creates a comment line (the program ignore the line).
REM OPEN USER PATHS TO DISK
On Error Goto PROBLEM 'send to error handler if no file
Error handler is started. This tells the program what to do if an error (any error) takes place. Here, we can use the error handler as part of the normal flow of our program. If an error message is generated by the program, the code flow will jump to the location labled PROBLEM: (which you can see below). While Goto statements are not quite good form in programming these days, it does come in handy, especially for error handling.
Open "c:\compare.txt" For Input As #1
The open command looks for the file, which we will always place at c:\compare.txt. The file will be open for input and represented as #1. It is here that the error handler can be used, because if the file does not exist yet, an error is triggered.
Read #1, ADraft$, AReDraft$
If the file compare.txt is found, information will be read from the file that recorded the last paths used to for the draft and redraft student files. Read #1 gets the data that is in the file at location #1. In the file, there are two parts to the data, separated by a comma. The first part of data will be placed in the ADraft$ variable and the second part into the AReDraft$ variable, both for use in this macro.
Close #1
This command will now close the file on disk.
START:
This is a target for a Goto statement. Here, we are using it to return from the error of having no file on disk (compare.txt), so the program picks up right after finishing with the compare.txt file.
On Error Goto OVER 'Error handling is over so jump to end of sub on error
Another error handler is started (each new error handler replaces any previous handlers--only one can exist at any time). This error handler is simply going to send the program to the end of the sub in case any unforeseen errors occur. Without this, the program may simply freeze or crash when an error is encountered.
REM SEND TO FUNCTION TO INCREMENT
Another comment line. Lots of comments make your code easy to read and help you remember what you wanted to do months or years later when modification to the code is needed.
ADraft$ = Increment$(ADraft$)
Increment$() is a function which you can see below after the end of the sub. This is like another sub program that does a specific job for the main program. In this case, the function will check if the student's draft uses a number for a name. If it is a number, the function will increase the value by one, thus saving the time of typing the next student's file name. We send the path, which includes the student's file name, to the function Increment$ by placing it in the parentheses (ADraft$). (the program now jumps to the function Increment, seen below the sub)
AReDraft$ = Increment$(AReDraft$)
The function Increment$ checks and changes the redraft path and file name also.
REM CREATE USER DIALOG BOX

ADraft$ = InputBox$("Where is your first draft? " + "Include the whole PATH. " + " Example: C:\ENG\COMP1\25.TXT", "First Draft", ADraft$)
A dialog box is drawn on the screen with the command InputBox$. The box asks the user a question, "Where is your first draft?", and the answer is placed in the variable ADraft$. When the dialog box opens, it includes the default answer ADraft$ and a title for the box, in this case: "First Draft."
ARedraft$ = InputBox$("Where Is your second draft?", "Second Draft", AReDraft$)
The same question, along with default path and file name AReDraft.
REM SAVE USER PATHS TO DISK
The new paths will be saved to disk now so that the program can use them again for the next student file.
Open "c:\compare.txt" For Output As #1
The file compare.txt is open, but this time to send output to.
Write #1, ADraft$, ARedraft$
The two variables are written to the file, separated by a comma. This write will erase any previous data in the file.
Close #1
The compare.txt file is closed and ready to be opened again.
REM MACRO RECORDED MENU ACTIONS
Through the record macro command in MSWord, the following code was picked up.
FileOpen .Name = AReDraft$, .ConfirmConversions = 0, .ReadOnly = 1, .AddToMru = 0, .PasswordDoc = "", .PasswordDot = "", .Revert = 0, .WritePasswordDoc = "", .WritePasswordDot = "" ToolsRevisions .MarkRevisions = 0, .ViewRevisions = 1, .PrintRevisions = 1
These commands tell MSWord what to do in order to open the redraft, which we use the variable AReDraft$, and then to activate the Revisions command in the Tools menu. This commands are not BASIC commands, but unique to MSWord.
ToolsCompareVersions .Name = ADraft$
Another command for MSWord which activates the comparision and tells the MSWord where the first draft is, ADraft$.
Goto OVER 'If there is no error, skip error handler
You can see why too many Goto statements are not good--just gets a bit confusing! But this is simply to skip the error handler section that was to create the paths if they did not exist in the compare.txt file.
PROBLEM:

ADraft$ = ""

AReDraft$ = ""

Err = 0
This resets the error handler so no error code exist any longer.
On Error Goto OVER
If there is any problem here, head to the end of the sub.
Goto START
This Goto sends us back to the program after the compare.txt file was opened, but before it was saved.
OVER:
Here is a target of some Goto statements that bring the program to an end.
End Sub
This stops execution of the sub.
Function INCREMENT$(File$)
Here is a short program that is run (called) from inside the sub. This function will test if the student's file name is a number. If it is, that number will be increased by one so the next student can quickly be found. Note that the variable sent from the sub is ADraft and AReDraft, either one can be sent here, but when this function begins, it will place the value in a new variable: File$.
REM INCREMENT THE FILE IF IT IS A NUMBER

File$ = LTrim$(File$)
The path name is trimmed of any empty characters.
Letters = Len(File$)
The variable Letters holds the value of how the length of the File$ string (the number of characters).
FileEnd$ = Right$(File$, 4)
The right end, including the period, of the path name are placed in the variable FileEnd$. This gives us the ending of the file name.
For i = (Letters - 4) To 0 Step - 1
Here we start a loop that will check the characters in the File$ string one at a time, starting from the right side of the period, which is the -4. The Step will reduce move the checking one character left every iteration of the loop.
If Mid$(File$, i, 1) = "\" Or Mid$(File$, i, 1) = ":" Then
Here we check for the \ symbol, which will tell us where the actual file name begins.
FileMid$ = Mid$(File$,(i + 1),((Letters - 4) - i))
The file name will be the location of the first backslash, \, from the right of the string, (the +1 moves to the right of the \ one character) to the end of the string just before the period (that is the Letters-4 position).
FileFront$ = Left$(File$, i)
From the start of the string, on the left side, to the location of the last backslash, \, gives us the front of the string (the string before the actual file name).
i = 0
Reset the loop counter and since the loop goes until i=0, this will also end the loop.
End If
Ends the If command, for when the backslash was found.
Next i
If no backslash,\, was found, and the IF command not executed, then go back and check the next character in the string (moving one to the left).
If Val(FileMid$)<>0 Then
Now we have the actual file name with no path string before it and no file extension after it (in the variable FileMid$). The Val command will check the value of the string FileMid$, but if the file name is not a number, its value will be zero. If that is the case (the value of the file name is =0), this If statement will not execute and the file name will not be changed.
FileNumber = Val(FileMid$)
If the value of the string was not =0, then we can begin to move that number up one. First, place the value of the string in a variable called FileNumber.
FileNumber = FileNumber + 1
Increase the variable by one.
File$ = FileFront$ + LTrim$(RTrim$(Str$(FileNumber))) + FileEnd$
Now put the whole path together again with: the front of the path (FileFront$), the increased file number (FileNumber) and the file's extension (FileEnd$).
End If
End the If command of being sure the file name is really a number.
Increment$ = File$
This sets the function's value equal to the path name (File$).
End Function
Function will end here and return control to the sub program.


ProgamPointers

I am sure there are better ways to write the code I have here, but the main point is that any teacher can use some of the power built into existing software to create just what he/she needs!

If you do give it a try, please come back and give your comments on my CÆLL page (FEEDBACK form). I look forward to any comments as well as any changes to the code.