This section provides some macro ideas and demonstrates ways you can use some of the WordBasic elements introduced in this chapter.
The macros described in this section are available on the Microsoft Word Developer's Kit disk in EXAMPLES.DOT (Windows) or MACRO EXAMPLES (Macintosh). For information about installing the files on the disk, see Chapter 1, "Introduction."
Here's a simple macro to delete the text between the insertion point, positioned within a sentence, and the beginning of the sentence. The macro capitalizes the first letter of the remaining text:
Sub MAIN SentLeft 1, 1 'Move to start of sentence; select text EditCut 'Cut selected text ChangeCase 4 'Capitalize first letter of remaining text End Sub
When you're typing in Word and you reach the end of a line, Word automatically moves the insertion point to the next line. You press Enter only when you reach the end of a paragraph. But many other sources of text include a paragraph mark at the end of every line. This text is difficult to work with in Word because Word treats each line as a separate paragraph and does not wrap the text. The solution is to remove the excess paragraph marks, leaving only the ones that end each paragraph. You can do this by hand, using the Replace command (Edit menu), but it is faster to record the process and run a macro such as the following:
Sub MAIN StartOfDocument EditReplace .Find = "^p^p", .Replace = "@#$#", \ .Direction = 0, .ReplaceAll, .Format = 0, .Wrap = 0 FileSave EditReplace .Find = "^p", .Replace = " ", .ReplaceAll, \ .Format = 0, .Wrap = 0 FileSave EditReplace .Find = "@#$#", .Replace = "^p^p", .ReplaceAll, \ .Format = 0, .Wrap = 0 End Sub
This macro assumes that two consecutive paragraph marks signify the end of a paragraph. When you remove paragraph marks from text, you usually want to preserve separate paragraphs. For that reason, this macro replaces two consecutive paragraph marks with the placeholder "@#$#". It then replaces each remaining paragraph mark with a space. Finally, it replaces the "@#$#" placeholder with two paragraph marks.
The following macro uses two WordBasic time and date functions, DateValue() and Today(), to count the number of days between today and a future date that you specify. An InputBox$() instruction prompts you to specify a future date; a MsgBox instruction displays a message box that indicates the number of days between the current date and the specified date. For a complete listing of time and date functions, see "Language Summary" in Part 2, "WordBasic Reference."
Sub MAIN enddate$ = InputBox$("Please enter future date:") serialenddate = DateValue(enddate$) numdays = serialenddate - Today() MsgBox "The number of days between now and " + enddate$ + \ " is" + Str$(numdays) + "." End Sub
The first version of this macro (see "The While¼Wend Loop" earlier in this chapter) counted the number of times the word "cool" appeared in a document. You can modify this macro so that it counts the number of times any word appears in a document:
Sub MAIN TRUE = - 1 searchtext$ = InputBox$("Please type a word to search for:") StartOfDocument EditFind .Find = searchtext$, .Direction = 0, .MatchCase = 0, \ .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, \ .FindAllWordForms = 0, .Format = 0, .Wrap = 0 count = 0 While EditFindFound() = TRUE count = count + 1 RepeatFind Wend MsgBox searchtext$ + " was found" + Str$(count) + " times." End Sub
The first line of the macro uses the InputBox$() function to prompt the user for the text to search for. The user's response is placed in the variable searchtext$.
When the macro is finished, it displays a message box indicating how many times searchtext$ was found.
You could further improve this macro by presenting different messages according to whether the text was not found, found just once, or found more than once. To do so, you can replace the MsgBox instruction in the example with the following If conditional:
If count = 0 Then MsgBox searchtext$ + " was not found." ElseIf count = 1 Then MsgBox searchtext$ + " was found once." Else MsgBox searchtext$ + " was found" + Str$(count) + " times." End If
This conditional tests for two specific conditions: count = 0 (the text was not found) and count = 1 (the text was found only once). All other cases (the text was found more than once) are handled by Else. If count = 0, the macro will display a message box similar to the following one.
You can make one more refinement to this macro by changing the MsgBox instructions so that when the word being counted is displayed, it is enclosed by quotation marks. Because quotation marks cannot be used inside a string, you need to use the Chr$() function: Chr$(34) produces a double quotation mark. In the following example, quote$ is assigned the result of Chr$(34). In the MsgBox instruction, quote$ is used in strings wherever a double quotation mark should appear:
quote$ = Chr$(34) If count = 0 Then MsgBox quote$ + searchtext$ + quote$ + " was not found." ElseIf count = 1 Then MsgBox quote$ + searchtext$ + quote$ + " was found once." Else MsgBox quote$ + searchtext$ + quote$ + " was found" + \ Str$(count) + " times." End If
This example displays a message box similar to the following one.
For more information on the Chr$() function, see Chr$() in Part 2, "WordBasic Reference."