Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lectures_SSD2_Yermakova / Lectures_SSD2 Yermakova.doc
Скачиваний:
226
Добавлен:
25.02.2016
Размер:
3.16 Mб
Скачать

4.2.2 Lab: Macros

If you have used applications such as word processors or spreadsheets, you may have noticed that there are certain operations that you do quite often when working with documents within these applications. For example, if you are creating reports using a word processor, you may need to create a header with your name and today's date and footer with the page number. This type of operation requires you to select certain functions or make choices within the word processor's menus, as well as typing some information from the keyboard. If you are using an application that supports macros, you can have the application "record" your actions with the keyboard and mouse, and refer to them later for "playback." The recorded sequence is called a macro. The application will use the macro to simulate your inputs and perform the desired actions when you ask it to.

The advantages of a macro are:

  • It can reduce the amount of time required to create a document.

  • It can reduce the chance of entering erroneous data.

  • It can simplify a complicated set of interactions, so that other people can perform the operation without understanding all of the details involved with the application interface. You can even place the name of the macro on the application's menu or toolbar, as if you have created a function that was not provided by the application's developers.

Macros do not necessarily record your inputs directly into a data file. Instead, a brief computer program is created that interacts with the application to perform the desired actions. (Sometimes this is done in an application-specific macro language, but Microsoft applications use Visual Basic to implement macros.) When you "execute" a macro, you are in fact running this program from within the application. Unlike other programs that you run on your computer, a macro program only has meaning while running the application used to create it.

A consequence of macros actually being small programs is that documents that you typically think of as containing only data can actually contain both data and embedded programs. If someone gives you a document containing a macro, and you open the document and execute the macro, the macro could turn out to be a virus designed to harm the data on your computer. The topic of macro viruses is discussed in more detail in Unit 6.

The steps typically involved in creating a macro are:

  • Activate the "create macro" function of the application.

  • Give the macro a name so that you can refer to it later.

  • Record the steps that you want the macro to execute whenever you run it by performing the keyboard and mouse interactions for the operation to be recorded.

  • Stop the recording when you are done.

  • Run the macro.

The only step required to execute a macro is that of invoking it. This is done either by selecting it via a menu using the mouse or via a key sequence known as a "shortcut." Shortcuts, if available, are normally established at the time the macro is created. If you are using the menu selection method, click the name of the macro that you want to execute.

What about actions you perform frequently through the operating system's user interface, such as copying all word processing and spreadsheet documents from different folders to a CD disk? Is there a way to automate these actions so that you do not have to repeatedly point and click with your mouse on various menus and dialog boxes? These actions can be simulated on the operating system by using something called a "batch file" or a "script." Unlike with macros, there is typically no way to record a series of actions into a batch file; instead, you must create the batch file with a text editor. Batch files are actually small programs that use the operating system's command-line interface. They will be discussed later in 4.3.2 Batch File Commands.

Learning Exercise:

Macro facilities are typically found in more sophisticated application software. For example, Microsoft Word supports macros, while the text editor Notepad does not. This learning exercise leads you through the steps involved in creating and executing a macro using Word. If you do not have Word on your computer, try to find some other application that supports macros and perform similar actions. To start recording the macro,

If you are using Microsoft Word 2007, you will need to ensure that the Developer tab is enabled on the ribbon at the top of the page.

You can find directons to enable the Developer tab and to create macros in Word 2007 by clicking on the icon for Help (the question mark in the upper right hand corner of the monitor screen), typing 'record and run macro' in the search text box and then clicking on the 'Search' button. Click on the option to 'Record or run a macro' then click 'Show Developer Tab'.

Here are the steps to enable the Developer Tab in the ribbon at the top of the monitor screen,

 Click on the Microsoft Office Button (the round button in the upper left corner of the monitor screen).

 Click Word Options.

 Select Popular.

 Select the checkbox for 'Show Developer Tab in Ribbon'.

 Now, you will see the Developer Tab Displayed in the ribbon at the very top of the screen monitor.

To start recording the macro,

 Click on the Developer Tab in the ribbon.

 In the Code Group, select Record Macro.

 The Record Macro dialogue box will appear. In its Macro name box, type "SSD2macro". However, do not type the quotation marks. (Note that the Record Macro dialog box allows you to assign a keystroke shortcut to the macro or to assign the macro to a menu, but these actions are optional. It also allows you to specify whether the macro is just for the document you are creating or for any document. If the macro is made to be shared by other documents, it would be placed in a library of macros available for use by other documents. However, we would not be using these features as part of this learning exercise.) Click OK.

Suppose we want to create a macro that consists of your name, the class name, and today's date. You might want to include this information at the beginning of each of your exercises.

 In the Word document, type your name, press ENTER, type "SSD2 Exercise", and then press ENTER. Click theInsert menu in the ribbon at the top of the monitor screen. In the Text Group, click Date and Time..., and then select an appropriate format from the Available formats box.

 Select the Developers Tab in the ribbon.

 In the Code Group, select Stop Recording to stop recording the macro steps. Your macro is now stored in a library for use with any Word document.

 To run the macro in a new document,

 Open a new document.

 Click the Developers Tab in the ribbon.

 Within the Code Group, click on Macros.

 In the dialog box that appears, click on the macro named SSD2macro.

 Click on the Run Button.

 You will see that the macro has inserted your name, 'SSD2' and the date in your document at the place where your arrow was positioned when you ran the macro.

If you want to view the program (that is, the macro) just created during the recording process,

 click the Developers Tab.

 in the Code Group, select Macros.

 Select SSDmacro.

 Click on the Edit Button.

 You will have to close the Microsoft Visual Basic window to proceed. To delete the macro that you just created,

 click the Developers Tab.

 in the Code Group, select Macros.

 Select SSDmacro.

 Click on the Delete Button.

Remember to close Word and do not save the documents that you created as part of this exercise.

If you are using a version of Microsoft Word other than Word 2007,

 On the Tools menu, click Macro and then click Record New Macro.

 The Record Macro dialogue box will appear. In its Macro name box, type "SSD2macro". However, do not type the quotation marks. (Note that the Record Macro dialog box allows you to assign a keystroke shortcut to the macro or to assign the macro to a menu, but these actions are optional. It also allows you to specify whether the macro is just for the document you are creating or for any document. If the macro is made to be shared by other documents, it would be placed in a library of macros available for use by other documents. However, we would not be using these features as part of this learning exercise.) If you are using a version of Word other than Word 2007, Click OK. You will see the small Stop Recording window with buttons for pausing and stopping the recording process appear on the desktop or on the Word toolbar. If necessary, this window can be moved to a different location on the desktop. 

 Suppose we want to create a macro that consists of your name, the class name, and today's date. You might want to include this information at the beginning of each of your exercises. Type your name, press ENTER, type "SSD2 Exercise", and then press ENTER. On the Insert menu, click Date and Time..., and then select an appropriate format from the Available formats box.

 Click the square button of the Stop Recording window to stop recording the macro. Create a new document by clicking New on the File menu or by using the New icon on the Word toolbar.

 To execute the macro in this new document, on the Tools menu, click Macro and then click Macros.... Select "SSD2macro" from the list of macros; then click Run. You should see the result of the macro appear in the new document.

 If you want to view the program (that is, the macro) just created during the recording process, on the Tools menu, click Macro, and then click Macros.... Select "SSD2macro" from the list and click Edit. The Microsoft Visual Basicwindow will then appear displaying a sequence of Visual Basic programming language statements, which is the macro you just created. You will have to close the Microsoft Visual Basic window to proceed.

 To delete the macro you just created, on Tools menu, click Macro, and then click Macros.... Select "SSD2macro" from the list of macros, and then click Delete. Click Yes when Word queries you about deleting the macro, and then close the Macros box. Finally, close Word, and do not save any of the documents you have created as part of this exercise.