


|
 Marvellous macros
Helen Bradley explains how to create macros in Word 97.
In
the word processing column in April '97 we published a series of handy macros for Word 6.0
and Word 95. Unfortunately, if you have upgraded to Word 97 these macros like any other
you were using in previous versions of Word will no longer work. This is because Word 97
uses a different language for its macros -- it uses Visual Basic rather than the WordBasic
language which the older versions used.
|
Creating a macro by typing the code
To use the macro code from this column, you'll need to be able to create
your own macros. To do this you will need to type the code directly into your Visual Basic
Editor. Here's how it's done:Step 1: Select
Tools, Macro, Macros and in the Macro Name text box type the name for your macro. Use only
characters and don't use any punctuation or spaces. In the Macros In list box select
Normal.dot (global template) and click Create.
|
Step 2: You'll see the
beginning and end of your procedure (macro) marked on the screen as:
Sub YourMacroName()
'
' YourMacroName Macro
' Macro created <today's date> by <your name.
'
End Sub
The first line is the procedure (macro) name, the second to
fifth lines are comments and serve no purpose in the macro except that they give you space
to document the macro. The sixth line is blank and it is here that you should type your
macro text. The last line marks the end of the procedure (macro) -- all your macro text
should appear before this line.
|
Step 3: Type the text of your
macro exactly as it appears in the text. |
Step 4: Close the Visual Basic
Editor and return to the Word editing screen, select File, Close and return to Microsoft
Word |
Step 5: Test your macro to
make sure it works as it should. To do this select Tools, Macro, Macros and select the
macro name from the list and click Run. All of
the macros in this months column can be created using this method.
|
Macro to insert a graphic and size it
This macro (called InsertImage) inserts a graphic into your document and sizes it to your
own specified size:
Sub InsertImage()
' Insert Image Macro
'move to the end of the current selection
Selection.EndKey Unit:=wdLine, Extend:=wdMove
'insert the graphic
Set myimg = ActiveDocument.Shapes.AddPicture(Anchor:=Selection.Range,
FileName:="C:\clipart\myimage.tif", LinkToFile:=False, SaveWithDocument:=True)
'size it to the following height and width in points
myimg.Height = 34.6
myimg.Width = 138.05
End Sub
Before you can use this macro you'll need to replace 'c:\
clipart\myimage.tif' with the name and location of your graphic file.
If you want an image to appear at a fixed size you'll first
need to calculate the size in points. To do this, load the image in a sample page and size
it to the size you want to use. Select Tools, Options and select the General tab and set
the Measurement Units to Points. Select the image and select Format, Picture, click the
Size tab and take a note of the Height and Width settings. Reset your units of measure
using Tools, Options. Finally, place the height and width measurements you took note of in
the last two lines of this macro.
If you want the image to appear in its original size then
simply remove the two lines of the macro which set its height and width.
You'll find that this macro is useful for inserting your
company logo, a picture of your signature or any other image that you use often.
|
Macro to insert a check box
This macro inserts a shaded check box at the current cursor position:
Sub InsertCheckbox()
'Insert a checkbox
Selection.InsertSymbol Font:="Wingdings", CharacterNumber:=113
End Sub
This is useful if you use a lot of check boxes in your
documents. If you use other symbols regularly see 'Recording a macro' in the Insider tips
box which shows you how to record your own macros and steps you through creating a macro
to insert any symbol from any symbol font into your document.
|
Macro to toggle display of a toolbar
This macro will hide a toolbar if it is visible and display it if it is not:
Sub ToggleToolbarDisplay()
'toggles the display of the named toolbar
CommandBars("WordArt").Visible = Not (CommandBars("WordArt").Visible)
End Sub
You can customise this macro to work with any built-in Word
97 toolbar or any toolbar you have created yourself. Simply replace 'WordArt' with the
toolbar's name which you can find by selecting View, Toolbars and reading the name from
the list.
|
Change the document directory
You can select your choice of document directory using this macro:
Sub FilesDirectory()
'macro to change directory
ChangeFileOpenDirectory "C:\MSOffice\Files"
End Sub
Replace the string "c:\MSOffice\Files" with the
directory you want to change to. You won't see anything on the screen but when you select
File, Open or File, Save As you'll see the directory you have specified on the screen.
If you find yourself frustrated by the My Documents folder
try creating versions of this macro for all your favourite directories and you need never
see My Documents again! Do this by giving each macro a different name (for example, change
'FilesDirectory' to another name) and change the drive and path to your choice of drive
and path.
There are numerous other possible variations of this macro.
This one allows you to change the current directory and open a file that is contained in
it:
Sub OpenTipsDocument()
'macro to change the current directory and open a named file
ChangeFileOpenDirectory "C:\MSOffice\Files\"
Documents.Open FileName:="myfile.doc"
End Sub
To use this macro, replace the string
"c:\MSOffice\Files" with the directory you want to change to and the string
"myfile.doc" with the name of the file to open.
This next macro opens a file without changing the current
document directory. To use it, replace the filename and location with the name and
location of the file you want to open:
Sub OpenMyFile()
'macro to open a named file without changing the current directory
Documents.Open FileName:="C:\MSOffice\Files\myfile.DOC"
End Sub
This last macro creates a new document based on a particular
template. Replace the template name and location with the name and location of the
template to use. Note that this macro statement is one single line but using the
underscore character (_) allows it to be broken into smaller lines.
Sub CreateNewMemo()
Documents.Add template:= _
"D:\Microsoft Office\Templates\Memos\Elegant Memo.dot", _
NewTemplate:=False
End Sub
|
Macro to create a custom footer
This macro adds a footer to the current document displaying the current filename and path
in Arial italic 9 point text:
Sub CustomFooter()
ActiveWindow.ActivePane.View.Type = wdPageView
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.Font.Name = "Arial"
Selection.Font.Size = 9
Selection.Font.Italic = True
Selection.Fields.Add Range:=Selection.Range, Text:="FILENAME \p ",
PreserveFormatting:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
|
Macro to change printer bins
This macro places a message box on the screen telling the user which printer bin the
current printer is set up to use. It offers to change that bin to a second bin if the user
selects 'yes' from the two buttons on the screen.To
use this macro, find the names of your printer bins by selecting File, Print, Options and
read the bin names from the 'Default tray' list box. Replace the two strings for trayOne
and trayTwo with the names of your two printer trays.
Sub changePrinterTray()
'macro to tell user the current printer tray
'and switch it if required
trayOne = "Upper tray"
trayTwo = "Lower tray"
'new line variable
crlf = Chr$(13)
'create variables for current and alternate printer trays
currentTray = Options.DefaultTray
If currentTray = trayOne Then
alternateTray = trayTwo
Else
alternateTray = trayOne
End If
'compose message string
Msg = "Current printer tray is:" + crlf + currentTray + crlf
Msg = Msg + "Change tray to " + alternateTray + "?"
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = "Change Printer Tray"
'tell the user the current tray and offer to change it
userResponse = MsgBox(Msg, Style, Title)
'change it if they said yes
If userResponse = vbYes Then
Options.DefaultTray = alternateTray
End If
End Sub
|
Thank you
Thank you to reader Anne Fairweather who suggested the topic for this month's column. |
|
|

|

On this
month's CD you'll find the file PCUser.dot in the \interact\macros\
folder. This is a template file containing the macros from this column. It
contains instructions for copying them to your Normal.dot template.
Adding macros to your
toolbars |
You can add a
button to your toolbar to run this or any other macro. Do this by right-clicking the
toolbar and select Customize. Select the Commands tab and from the Categories list select
Macros. From the Commands box drag the macro name icon to the toolbar. Right-click the
'button' and select Default Style and then right-click again and select either Edit Button
Image to create a custom button face or Change Button Image to select one from the list.
When you're finished select Close in the Customize dialogue to return to your file. |
Additional macros |
Word 97
includes some additional macros in the file macros8.dot. You'll find this file on your
Office 97 CD in the directory \Office\Macros. Copy this file into your
template folder (\Microsoft Office\Templates) and you can look at the
macros in it by creating a file using File, New, select the file Macros8.dot
and select OK. You'll see an explanation of the macros and you can run them from this
file. The file contains
instructions for copying these macros to your Normal dot file. |
|
|