20
Other Features • Index and table of contents • Macros and VBA

Other Features Index and table of contents Macros and VBA

Embed Size (px)

Citation preview

Page 1: Other Features Index and table of contents Macros and VBA

Other Features

• Index and table of contents

• Macros and VBA

Page 2: Other Features Index and table of contents Macros and VBA

Index and table of contents

• In a large document, it is helpful to have a table of contents, and possibly and index as well.

• Word can help generate these automatically for you and update them as the document is modified and page numbers change.

Page 3: Other Features Index and table of contents Macros and VBA

• To generate either one of these, use the Insert => Index and Tables…dialog box.

• To generate a table of contents, select the Table of Contents tab:

Page 4: Other Features Index and table of contents Macros and VBA
Page 5: Other Features Index and table of contents Macros and VBA

• To generate an index, select the Index tab:

Page 6: Other Features Index and table of contents Macros and VBA
Page 7: Other Features Index and table of contents Macros and VBA

• To have Word create an index, you must specify which terms are to be indexed.

• You can do this from this dialog box by either:– clicking the Mark Entry… button, which

allows you enter individual terms; or– clicking the Automark… button, which allows

you to specify a file where all the relevant terms have been stored.

Page 8: Other Features Index and table of contents Macros and VBA

Macros

• Macros are little programs that you can create to automate particular tasks that you may want to execute more easily than having to specify all the steps yourself every time you want to perform that task.

• Macros can be created for use in any Microsoft Office application.

• Here we consider simple macros for Word.

Page 9: Other Features Index and table of contents Macros and VBA

• Macros are actually written in a programming language called VBA (Visual Basic for Applications).

• But simple macros can be created by just performing the operations once and letting the macro recorder in Word translate your actions into VBA.

• To create a macro this way, use the Tools => Macro => Record New Macro… dialog box:

Page 10: Other Features Index and table of contents Macros and VBA
Page 11: Other Features Index and table of contents Macros and VBA

• If you want the macro you create to be invoked by a simple keystroke shortcut, you should click the Keyboard button and fill in the resulting dialog box.

• Or, you can invoke your macro by selecting its name in the Tools => Macro => Macros… dialog box:

Page 12: Other Features Index and table of contents Macros and VBA
Page 13: Other Features Index and table of contents Macros and VBA

Ways to create macros

• You can create a macro from scratch by writing all the VBA code yourself, assuming you know VBA.

• Or you can create a macro by just using the macro recorder.

• Or you can do something in between, by first recording a macro, then making minor modifications to its VBA code to make it more powerful.

Page 14: Other Features Index and table of contents Macros and VBA

A simple macro example

• Here is the VBA code that results from recording these steps:

1. Set the font to Times New Roman and the font size to 12 points.

2. Type “Ronald J. Williams”, followed by Enter.

3. Do an Insert => Date … using a particular format and with automatic update set, followed by Enter.

4. Type “COM1105”, followed by Enter.

Page 15: Other Features Index and table of contents Macros and VBA
Page 16: Other Features Index and table of contents Macros and VBA

• And here’s what results from running this macro at the beginning of a blank document.

Page 17: Other Features Index and table of contents Macros and VBA
Page 18: Other Features Index and table of contents Macros and VBA

• Look at the VBA code again. Even when you don’t really understand VBA, it’s not too hard to infer what each line of this code does.

• It’s also easy to see how you could modify it so that it selects other font types or sizes and how it puts text into the document.

Page 19: Other Features Index and table of contents Macros and VBA

• With very little VBA knowledge, you can make slight modifications to macros like this that do much more powerful things.

• For example, suppose we want the macro to prompt the user for the course name rather than have it built in.

• This macro cannot be created solely by using the macro recorder, but we can modify just one line of the previous code to create such a macro:

Page 20: Other Features Index and table of contents Macros and VBA

The one line we’ve modified