Introduction to macros

Macro is a function similar to programming, which allows word to implement some instructions you want to execute. Macro is a special function designed by Microsoft for its OFFICE software package, which aims to automate some tasks in user documents. Both WORD and EXEAL in OFFICE have macros.

About macros

If you perform a task repeatedly in Microsoft Word, you can use a macro to automatically perform the task. Macros are a series of Word commands and instructions, which are combined to form a single command to realize the automation of task execution.

The following are some typical applications of macros:

Accelerate daily editing and formatting.

Combines multiple commands, such as inserting a table with a specified size and border and a specified number of rows and columns.

Make the options in the dialog box easier to access.

Automate a series of complex tasks.

Word provides two ways to create macros: macro recorder and Visual Basic editor.

If you want to run a macro conveniently and quickly, you can assign it to a toolbar, menu or shortcut key. In this way, running a macro is as simple as clicking a toolbar button or menu command or pressing a shortcut key.

If the new macro has the same name as the existing built-in Word command, the new macro action will replace the existing action. For example, if you record a new macro and name it "FileClose", it will be associated with the "Close" command. When you choose the Close command, Word will perform the operation of the new record.

Recording macro

The macro recorder can help you start creating macros. Word records macros as a series of Word commands in Visual Basic for Applications. You can pause recording while recording a macro, and then resume recording from where you paused.

When recording a macro, you can click commands and options with the mouse, but the macro recorder cannot record the mouse movement in the document window. You must use the keyboard to record these actions.

Tips for recording macros

Before recording or writing a macro, please plan the steps and commands that the macro needs to execute.

If an error occurs during macro recording, the action to correct the error will also be recorded. After recording, you can edit the macro and delete the unnecessary actions recorded.

Try to predict any information that Word may display, which may stop the macro operation or cause confusion when the macro is running.

If the macro contains the Find or Replace command on the Edit menu, click the Advanced button on the Find or Replace tab, and then click the All option in the Search Scope box. If the macro only searches up or down, when the macro reaches the beginning or end of the document, Word will stop running the macro and display a prompt asking whether to continue the search.

If you want to use the macro you are recording in other documents, make sure that the macro has nothing to do with the contents of the current document.

If you use macros frequently, you can assign them to toolbar buttons, menus or shortcut keys. In this way, you can run the macro directly without opening the macro dialog box.

Create a macro from scratch

You can use the Visual Basic editor to create very flexible and powerful macros, which contain Visual Basic instructions that cannot be recorded.

When using the Visual Basic editor, you can get other help, such as reference information about objects and properties.

Save macro

You can save macros in a template or document. By default, Word saves macros in a normal template. So that all Word documents can use macros. If you need to use macros in a separate document, you can save the macros in that document. A single macro in a document is saved in a macro scheme and can be copied from this document to other documents.

Automatically perform tasks in web pages

In Word documents saved and published as web pages, use script tags and Microsoft script editors instead of macros to automatically perform tasks.

Script tags are used to mark the storage location of scripts in Word documents. Double-clicking the script tag in the Word document will start the Microsoft Script Editor, and you can start editing the script. When the Web page is displayed in a web browser, the script will run.