Hardware and software setup

Methods for executing macros. Creating a UI Macro

MACROS.

Most users creating and using documents Microsoft Office use standard features applications. However, Office provides ample opportunities for automating work with documents - this is programming in VBA and using macros.

VBA, the Visial Basic for Applications programming language, is a programming language built into Office applications The used to create macros.

Macro and macro recorder.

Instead of repeating the same commands in MS Office, you can create a macro that will execute this sequence of commands automatically. The macro allows you to enter solitary a command that performs the same task as when executing a group of commands.

A macro is a program written in VBA. VBA is a special version of Visual Basic (for Application).

There are 2 ways to create a macro in Office:

§ recording of user actions by a macro recorder

§ Opening a VBA window and entering VBA statements

Most often, the user uses VBA to correct a macro recorded by a recorder.

To work with macros in MS Office 2007-13, display the DEVELOPER tab on the ribbon (button OFFICE / Word OPTIONS / RIBBON CUSTOMIZATION).

The user needs to think over the starting conditions for the macro, for example, if something needs to be done with the selected fragment, then the fragment should be selected before recording or running.

Macro recording.

§ tab DEVELOPER / RECORD MACROS, give a name to the macro. The name must not contain spaces, start with a letter, and not contain special characters. the maximum name length is 80 characters. If a created macro is given the same name as a built-in macro Microsoft Word, the existing macro is replaced by the new one.

§ execute commands

§ DEVELOPER/STOP RECORDING tab

Normally, a macro is available for the Normal.dot template, it is automatically saved with it.

Running a macro.

§ tab DEVELOPER / MACROS, select the desired one and EXECUTE.



Assigning a macro to a panel button quick access and/or keyboard shortcuts.

§ or OFFICE/Word OPTIONS/SETTINGS button

§ or choose the appropriate action when assigning a macro name.

When assigning keyboard shortcuts to a macro, you should remember that many combinations are reserved in Worde and it is not easy to find a “free” combination.

Deleting a macro.

§ tab DEVELOPER / MACROS, select the desired one and DELETE.

Changing the macro text.

When recording a macro, the recorder saves a sequence of text instructions in VBA. This text description is called source code. It can be viewed and corrected:

§ tab DEVELOPER / MACROS, select the desired one and CHANGE.

To run a macro from the VBA editor: the RUN button ( )

Creation of automatically executed macros.

Word provides 5 special names for macros

VBA programs consist of a simple sequence of actions: the program accesses an object, gives it properties, performs actions with it. Every document, paragraph, ... are objects. For example, the Selection object is a selection. Word itself is an Appication object. (See appendices 2.3)



Each object has its own characteristics - properties.

Many objects perform actions called methods.

For example,

ActiveDocument.Close (object - active document, method - close)

According to the VBA syntax, the separator is a dot.

Usually: object, dot, method and/or properties (through dot)

Right mouse on the object - a list of methods, properties (List Properties / Methods)

The With ….. End With sequence allows you to specify several methods/properties for one object without repeating its name many times.

Practical task 1.

Let's write a macro called "my_first_macro", in which we will write the following commands:

§ Landscape page orientation

§ View scale – 33%

As a result, we get the following code

Sub my_first_macro()

"

" my_first_macro Macro

" Macro recorded 03.12.07

With ActiveDocument.PageSetup

LineNumbering.Active = False

Orientation = wdOrientLandscape

TopMargin = CentimetersToPoints(1.5)

BottomMargin = CentimetersToPoints(3)

LeftMargin = CentimetersToPoints(2)

.RightMargin = CentimetersToPoints(2)

Gutter = CentimetersToPoints(0)

HeaderDistance = CentimetersToPoints(1.25)

FooterDistance = CentimetersToPoints(1.25)

PageWidth = CentimetersToPoints(29.7)

PageHeight = CentimetersToPoints(21)

FirstPageTray = wdPrinterDefaultBin

OtherPagesTray = wdPrinterDefaultBin

SectionStart = wdSectionNewPage

OddAndEvenPagesHeaderFooter = False

DifferentFirstPageHeaderFooter = False

VerticalAlignment = wdAlignVerticalTop

SuppressEndnotes = False

MirrorMargins = False

TwoPagesOnOne = False

GutterPos = wdGutterPosLeft

ActiveWindow.ActivePane.View.Zoom.Percentage = 33

end sub

You can correct the text of this macro, for example,

§ change the percentage, for example 33 to 42

§ change fields

§ replace False with True in the line.LineNumbering.Active = False (there will be autoline numbering)

§ replace in line. Orientation = wdOrientLandscape on the wdOrientPortrait, the paper orientation will be portrait

After saving, this macro can be run again for any other document.

Practical task 2.

Let's create a macro that creates a message box when opening a file.

MessageBox - a message box that appears on the screen.

To display a MessageBox when opening any MS Word file, you need to create a macro called AutoOpen and call the msgbox function inside it. It will look like this:

Sub AutoOpen()

" This function called when the document is opened

"Display MessageBox

MsgBox "Hello buddy!"

The sequence of actions (one of the options):

§ DEVELOPER/MACROS tab, give any name and CREATE

§ In the VBA editor window, change the name and type the text of the program in VBA

§ Save

§ Launch

Now, when opening each document, we will receive a message.

The command to display the message box looks like this:

MsgBox (promt, button, title, help file)

promt is a required argument that specifies the text that will appear in the message box

button is an optional argument that defines the type of the message box, the presence of buttons Ok, Cancel, Stop, Skip, etc.

title - an optional argument that specifies the title of the window

helpfile is an optional argument specifying which help file will be opened if the user presses F1

Practical task 3.

Create a simple dialog box.

To create a dialog box, you need to create a visual object called a form. A form is an analogue of a window on which you can place controls (buttons, switches, checkboxes, etc.). Each form is part user interface. Each form is an object, and, in turn, contains many objects, each of which can be controlled individually.

Form creation:

§ VBA window (ALT+F11)

§ INSERT/New Form or UserForm (insert new form)

§ In the Properties window, you can set the properties of the form, for example, the Caption property allows you to give the form a name

§ Using the ToolBox panel, set the controls on the form:

Label - inscription

CommandButton - a command button to perform an action

TextBox - text field

Image - drawing

ChtckBox - checkbox

§ Customize the operation of each element. For example, 2 clicks on the CommandButton will allow you to write those VBA statements that should be executed when this button is clicked.

In order for such a dialog box to appear when running a macro, you need to add the following lines to the macro:

Load UserForm1

UserForm1.Show

Because mastering VBA requires a lot of time and knowledge of the basics of algorithmization, users usually write down Word commands using a macro recorder, then correct and/or add to the macro.


By changing the order of commands in the palette list, you can also change the order in which commands are executed. In addition, commands from one macro can be moved to others.

In order to change the order of commands, it is necessary to "capture" the command line in the list of the palette and move it to a new position. double line displays the position of the command during the move.

Changing Macro Options

When a macro is created, it receives the name and color that it displays in command button mode, as well as a keyboard shortcut. If necessary, these parameters can be changed at any time using the command Action Options(Operation Options), which displays a dialog box identical to the New Action(New operation), which is described in the next section.

Creating and Recording Macros

Creating a macro ( action) lies in the fact that during the execution of a particular sequence of commands Adobe program Illustrator writes them down in the same order, including the settings used by each specific command.

Thus, you can record any sequence of commands and use them in the future, reducing the time and, most importantly, the effort to perform routine operations, as well as creating "man-made" effects.

The development of such macro commands opens up wide opportunities for users to apply their creative powers. By purpose, the result is close to plug-in modules, but it is easier to create and does not require special knowledge.

However, there are limitations: some commands and functions can only be included in macros using the notation ( recording). To include in the operation commands that cannot be written at the time of writing, use the command Insert Menu Item(Insert menu item). See the next section of this appendix for information on this.

Advice
Another very important limitation should be noted. The success of executing a recorded macro with another document and at another time largely depends on the parameters of the other document (color model, selected object, active layer, etc.), as well as on the current settings of the program (for example, fill and stroke colors, and some others)
.

Since in order to record a sequence of commands (Fig. A1.3), they must first be executed, and erroneous actions are possible in the process of execution, it is strongly recommended to experiment with copies (especially for important documents).

Advice
It should also be borne in mind that when used as elements of a macro command Save As(Save As) or Save a Copy(Save a copy) to dialog boxes these commands don't need to type specific filenames
.

Rice. P1.3. Example of recording multiple actions in a macro


Rice. P1.4. New Action Dialog Box

So, in order to create a macro using the recording method ( recording), you need to open the edited document, display the palette Actions(Operations), select a set of macros in it and perform one of two actions:

In both cases, a dialog box will be displayed. New Action(New operation) (Fig. A1.4), in which you can assign a name to the macro (field Name(Name)) define a set of macros (list set(Set)) and a key combination to launch it (field Function Key(Function key)), select one of the seven colors to display the macro string in the palette (list color(Color)).

Then you should press the button Begin Record(Record), after which the dot on this button will turn red.

Now you can start executing the commands that you want to save in the macro. If the command has a dialog box, then after setting the required values, press the button OK. If you intend to make changes to the settings during the execution of the macro, you can leave the default values, but in this case, be sure to click the button OK. When exiting the dialog box by pressing the button Cancel(Cancel) given command is not written to a macro.

Stop recording Occurs when the button is pressed Stop Playing/Recording(Stop Execution/Record) at the bottom of the palette Actions(Operations) (first from left).

The created macro should be executed. There are several ways to run macros:

1. Through the command Macros → Macro → Tools

2. Using the key combination assigned to the macro;

3. Through the menu command assigned to this macro;

4. Through the button assigned to this macro on the toolbar.

The choice of how to run the macro is determined by the user based on their own needs and preferences. It is advisable to use the second method to run the most frequently executed macros. The third - illustrates the ability to enter your own commands into the main menu and thereby improve the menu. The last method distinguishes the visibility of the launch.

Let's consider in more detail each of specified methods running macros.

1. Team Macros→Macro→Service– a standard option for calling a macro for execution. In the Macro dialog box, select the created macro and click the [Run] button. This method also suitable for editing and deleting a macro - the [Edit], [Delete] buttons.

2. Using the key combination (Fig. 2):

Rice. 2. Dialog box. Setting the Keyboard to Assign macro

keyboard shortcuts in Microsoft Word.

2. To call a macro in a dialog box Keyboard customization tab New keyboard shortcut a combination of control keys is specified. For example, together with a combination of numeric or alphabetic keys (in Latin);

Then the [Assign] button is pressed, which makes the assignment.

3. Call by a command in the menu - carried out standard setting menu: adding a menu item associated with a macro call:

In the dialog box that appears Setting, on the tab Teams from combo box Categories team is selected Macros, followed by the name of the macro;

after clicking the left mouse button on the name of the macro, a dotted image of the button appears, which should be dragged to the menu to add a menu item. The left mouse button is then released.

4. Using the button assigned to this macro on the toolbar (Fig. 3):

In Word, when you click the [Panels] button (see Figure 1) in the dialog box Recording macro dialog box appears Setting, in which on the tab Teams from combo box Categories is chosen Macros, followed by the name of the macro;

after pressing the left mouse button on the macro name, a dotted image of the button appears, which should be dragged to the toolbar. After that, the left mouse button is released;

By clicking on the [Change selected object] button, you can select the desired icon for the button by running the command ü Select an icon for the button(Fig. 4). You can also edit the icon with the command Change button icon(window Button Editor) (Fig. 5).

Rice. 3. Dialog box Setting for macro assignment
toolbar buttons in Microsoft Word.

Rice. 4. Button icon selection dialog

Rice. 5. Button editor dialog box

Excel additionally has the ability to run a macro through a symbol assigned to it.

After choosing a call method, a small toolbar appears Stop Recording, containing a button:

- [Stop recording] – ends macro recording;

[Pause] – pauses macro recording (Fig. 6).

Fig.6. Macro Recording Panel in Microsoft Word

  1. Changing macros

Macros are saved in the template as program texts. Each template can contain an arbitrary number of macros. Because dot files (dot is a template file extension) cannot be read as documents, Word has special means, allowing you to view and edit the macro as a sequence of statements on VBA language(Fig. 7). During editing, any macro is presented as a sequence of VBA statements. The essence of editing a macro is to remove, add and replace operators, as in programming in any other language.

Fig.7. VBA editor dialog

Macro editing is performed using the command Macros. In the dialog box Macro, in field Name the name of the macro to be edited is selected. The process starts when you click the [Debug] button. You can specify the name of a non-existent macro. If you then click the [Create] button, a new macro will be created and the user will be able to write it in VBA.

The [Debug] button is used to open the macro editing window, which displays the contents of the macro in the VBA language. The macro editing window is similar to a regular document window, but differs by the absence of a coordinate ruler and the presence of a special toolbar. You can open multiple macro processing windows. The text of the macro is edited in the same way as the normal text of a VBA program. Most of the text editing commands are available, for example, the commands Copy And Cut.

The [Delete] button deletes the specified macro.

To close the macro editing window, you can use the system menu of the editing window or the command close on the menu File. Specific commands for editing and debugging a macro are executed by pressing the buttons in the delete line.

Bibliography:

1. Musin K.A " technical application to Microsoft Word"

2. Borodina A.I. "Macroprogramming"

macro refers to a set of one or more commands that perform specific, frequently used operations, such as opening forms or printing reports. Macros can be useful for automating frequently performed actions. If you record these actions as a macro, they will be performed automatically when the macro is run. For example, when the user clicks a button, you can run a macro that prints a report or displays a form. macro- the main component of a macro, an instruction that, alone or in combination with other macro commands, determines the actions performed in the macro (sometimes macro commands are simply called macro commands). The series of macros that make up a macro is executed each time it is run.

For creating a macro in the database window, select the tab Macros and press the button Create. The macro constructor window will appear (Fig. 3.37). It consists of two columns: Macro and Note. In column macro lists the commands to be executed. To in a column cell macro set the desired macro, it should be selected from the list. Column Note, contains comments for commands. When the macro is executed, it is ignored, but its completion makes the text of the macro clearer.

Rice. 3.37. Macro window view.

The execution of each macro depends on its arguments(some macros don't have arguments). Arguments are entered into specially designated fields located at the bottom of the macro window. Arguments can be entered using the keyboard, however, if possible, it is better to select them from a list to avoid incorrect values. Keep in mind that a macro command that does not specify a target object applies to the active object.

There is the following reception quick creation macro A that performs actions on a specific database object. You need to select an object in the database window and move it with the mouse to the macro cell in the macro window.

To do this, place the database window and the macro window side by side on the screen using the command Top down or From left to right from the menu Window. Then select the appropriate tab in the database window, select an object and move its icon with the mouse to the macro cell. For example, to create a macro that opens a form, move the form icon from the database window to the macro cell with the mouse. When you drag a macro icon into a macro cell, you enter a macro that starts this macro, and when you drag icons of other objects (tables, queries, forms, reports, or modules), a macro that opens this object is added to the macro.

Running a Macro can start at the user's command, when called from another macro or event procedure, or in response to an event on a form, report, or control. For example, you can assign a macro to run on a button on a form, causing the macro to run when that button is clicked. You can also create a special menu command or button on the toolbar that launches the macro; defining a keyboard shortcut to run a macro, and automatic start macro when opening the database.

To run a macro from a window macro constructor, the button must be pressed. To run a macro from the database window, select the tab Macros, activate the macro name and press the button launch in the database window (you can also double-click on the name of the macro to run).

Liked the article? Share with friends!
Was this article helpful?
Yes
Not
Thanks for your feedback!
Something went wrong and your vote was not counted.
Thanks. Your message has been sent
Did you find an error in the text?
Select it, click Ctrl+Enter and we'll fix it!