Recording

The Macro Recorder can be used to record simple macros.
Recording macros is very useful even if you are a very experienced programmer, although there are some limitations that you need to be aware of.
If you are relatively new to macros it is worth rehearsing your steps first before recording them.
This will help you to understand the code that is generated and to help make your code as efficient as possible.
When you record a macro Word is basically recording the keystrokes, menu commands exactly as they are pressed.
After you have recorded a few single steps you can run the macro to perform the required task again and again.
Once you start the Macro Recorder all your keystrokes will be recorded and converted into VBA code.
Before recording your macro check your macro security level. You will be not be able to record a macro if your security level is set to High. This should be changed to Medium.


Record your Macro

Select (Tools > Macro > Record New Macro) to display the Record Macro dialog box.
Alternatively you could press the "Record Macro" button on the Visual Basic toolbar.

alt text

The name of your macro will automatically default to "Macro1", "Macro2", etc depending on the number of macros that have been recorded in that specific workbook.
Macro names must begin with a letter and cannot resemble cell addresses (i.e. A1, B5, BT100 etc).
Macro names cannot include spaces although the underscore character can be used (e.g. "macroname_2"). You can also include numbers in your macro names.
The shortcut key and description are optional so do not worry about these at this point, however it is very important to know where you macro will be stored.


Where to store your macro

There are two places where you can store your macros:
Normal.dot - Storing your macros here will mean that they are available every time Word is open and are not reliant on any one particular document.
Document - This is the default location and is often the best place if you are relatively new to macros. A macro that has been saved into a specific document is only available when that particular document is open. The currently active document is also referred to as the current document or active document.
Once you press OK a small floating toolbar will appear. This is the Stop Recording toolbar and indicates that any keystrokes you make will be recorded.
The status bar also displays the message "Recording".
You can now perform the keystrokes that you would like to be automated.
A new code module is often inserted into the workbook automatically to contain the generated VBA code.


Stop Recording

You can stop recording by selecting (Tools > Macro > Stop Recording) or by pressing the Stop Recording button on the left hand side of the Stop Recording toolbar.

alt text

The toolbar will then disappear.
It is very important to stop recording once you have finished otherwise Word will carry on recording your keystrokes indefinitely.
Once you have finished recording you can cut and paste the code (via the clipboard) to different code module.


Viewing the Code

The only way to view the VBA code is through the Visual Basic Editor window.
This window can be displayed by selecting (Tools > Macro > Visual Basic Editor) or by pressing (Alt + F11).

alt text

You will need to locate the Project in the Project Explorer window. This should appear in the top left corner. If you cannot see it, select (View > Project Explorer).
There should be a project there called VBAProject followed by the name of the workbook in brackets. Expand this project.
Select the Modules node and expand it. There should be a module called "Module 1". Double click this module to display the code window.
Your macro will appear here and all the VBA code is contained within the "Public Sub" and "End Sub" statements.
You will often find that the Macro Recorder will generate more code than you really need although it is easy to remove any unnecessary code.


Important

The code that is generated is normally very specific and often dependent on local settings or options.
It is not possible to record a macro and have the generated code placed within an existing macro. You will have to cut and paste the lines of code manually.
The code generated by the macro recorder often contains a lot of unnecessary arguments and lines and should really be cleaned up afterwards.
Be aware that the Macro Recorder cannot create any of the following: Loops, Variables, Error Handling or Conditional statements.
The code generated by the macro recorder can often be quite inefficient and will run quite slowly compared to code that has been optimised.
In Word 97 you cannot add a description to your macros in (Tools > Customise)
You can also double click the "REC" in the status bar at the botom to quickly record a macro


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext