|
Page 1 of 2 My acquaintance with Microsoft Word has begun one and a half year ago when I for the first time needed to issue enough the big text document - degree work. Now I hardly can present the workplace without a habitual icon.
In Word it is possible to do much: to type and edit the text, to draw simple pictures, even to impose books. There are many books, devoted to work with Word, however in them poorly or will not mention at all language of macros - WordBasic.
Meanwhile WordBasic - convenient and a simple language of the programming, allowing to automate routine work with test documents, to operate any Windows-programs and even to write a virus.
In the resulted examples I have tried to show the most useful opportunities WordBasic and to show opportunities of language.
1. The general remarks
Most easier to create macros with command Service->; Macros->; To begin record. All actions of the user before pressing the button Stop enter the name in macros and generating at start of it macros. Such way does not allow to organize cycles and to give out messages to the user, therefore for a writing of the high-grade program it is necessary to edit written down macros. For this purpose in Word 6.0 and 7.0 it is necessary to choose a command Service->; Macros->; To change. (Service->; Macros->; Editor VisualBasic in Word97).
The full description of commands WordBasic is delivered together with Word, but not established by default. If you cannot find this section in your help system, means it is necessary to establish it from distributive disk Word.
2. Dialogues
In any directory on programming it is written, that the good program should be interactive, that is should be able to carry on dialogue with the user. We shall consider, how it can be made by means of WordBasic.
Let's write absolutely correct program, giving out on the screen the message.
Sub Hello ()
MsgBox " Hello Word ", vbInformation, " My first message "
End Sub
The first parameter of function MsgBox sets the text of the message, the second - type of the message, i.e. a badge and buttons, and the third sets heading of a window of the message.
Now we shall try to complicate the program. Let it displays the message with an inscription " To close Word? " And buttons " Ok " and "cancel". Except for that let the program closes Word on pressing Ok.
Sub Hello ()
If MsgBox (" To close Word? ", vbOKCancel, " My first message ") = vbOK Then
Application. Quit
End If
End Sub
Here we use the value returned by function MsgBox to define on what button the user has pressed.
If function has returned vbOK, i.e. the user has chosen button OK, we cause method Quit of object Application (object Application is itself Word).
But it's not yet all. At output Word gives out warnings if changes in files are not kept. We modify the program so that these messages did not appear. For this purpose we shall establish property DisplayAlerts of object Application operating a conclusion of messages on the screen in false and we shall specify parameter wdDoNotSaveChanges (to not keep change) for method Application. Quit
Sub Hello ()
Application. DisplayAlerts = False
If MsgBox (" To close Word? ", vbOKCancel, " My first message ") = vbOK Then
Application. Quit wdDoNotSaveChanges
End If
End Sub
Macros it is ready. Wish to surprise the colleague? Copy to it this macros in pattern Normal.dot under name Autoexec (macros with such name are carried out automatically at start Word).
3. How to plan smoke breaks...
Let's assume, that employees of your department too often smoke, or, on the contrary, so take a great interest in work that forget to have dinner (it certainly an improbable case).
The second program which we shall write, will remind, that it is already possible to go to smoke, or, say, to have dinner. Method OnTime of object application allows to set time of performance macros. Syntax at this method following:
Application. OnTime (When, Name, Tolerance)
Here When specifies time of performance, Name is a name macros which is necessary for executing, Tolerance - the unessential parameter specifying a time interval during which it should be executed macros.
In our program of the message will stand out each hour. We shall name the first macros AutoExec that it was started at start Word. In our case the method onTime uses functions Now to define current time, and TimeValue to set an interval equal to hour. Macros Message gives out the message and sets a following interval of performance.
Sub AutoExec ()
Application. OnTime Now + TimeValue ("0:01:00 AM"), "Message"
End Sub
Sub Message ()
MsgBox (" Now it is possible and to smoke... ")
Application. OnTime Now + TimeValue ("0:01:00 AM"), "Message"
End Sub
Slightly having altered macros, it is possible to write even the whole schedule for the working day. The only thing what it is necessary to remember - such macros is better to keep in global pattern Normal.dot that they were always accessible.
4. How to transfer the text from the Dos-editor in Word
Probably, at your office too there is a bookkeeper, till now preferring to work under Dos. From time to time it is necessary for you to work with its text files. Often in such cases the open file borrows{occupies} only a part of page and looks{appears} as follows:
The report on sales of horns and hoofs.
For the expired period of a horn and a hoof
Have been sold by Mormons branch
For the sum of nineteen millions
Seventy thousand, Astrakhan
Branch on fifteen millions...
As you can see, the text occupies only half screen. The problem consists that text editors under Dos place symbols of the end of the paragraph in the end every line. The algorithm is widely known, allowing transforming such documents to normal kind Word.
1. To replace two successively a going symbol of the end of the paragraph with a symbol of tabulation.
2. To replace symbols of the end of the paragraph with blanks.
3. To replace symbols of tabulation with symbols of the end of the paragraph.
Tiresomely enough to spend each time such replacements manually, therefore we shall write macros by which it will do{make} for us.
Sub FormatDosText ()
Selection. WholeStory Rem the text is allocated
Selection. Find. ClearFormatting Rem It is removed the previous conditions of search
Selection.Find.Replacement.ClearFormatting Rem It is removed the previous conditions of replacement
With Selection. Find Rem we Set new conditions of search and replacement
.Text = "^p^p" Rem Replacement of two ends of the paragraph by a symbol of tabulation
.Replacement. Text = "^t"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection. Find. Execute Replace: = wdReplaceAll Rem we make replacement
With Selection. Find
.Text = "^p" Rem Replacement of the end of the paragraph by a blank
.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
With Selection. Find
.Text = "^t" Rem Replacement of a symbol of tabulation by a symbol of the end of the paragraph
.Replacement. Text = "^p"
.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
Generally speaking, for performance of any same actions it is better to create macros. Practice shows, that spent for this time with interest pays off in the further. Besides you get rid of weight of routine work.
5. How to be protected from macro virus
<< Start < Prev 1 2 Next > End >> |