Leading the way in Microsoft Office Development
 Home|Excel|Word|PowerPoint|Consultancy|Feedback|Contact 
 Microsoft Word > Add-ins > Creating a Word Add-in< Previous | Next > 

 

Step 1 - Create a Document ".doc"

 
 

To create a Word add-in you must first create a Word document as normal.

 
 

It is highly recommended that you test your code thoroughly before creating the add-in.

 
 

There may not always be an active document, so any references to the "ActiveDocument" will cause an error if there is no document open.

 
 

Make sure you keep a copy of the original presentation because you cannot make any changes to the VBA code once the add-in has been created.

 
 

In fact you will not even see the VBA Project in the Visual Basic Editor.

 

 

Step 2 - Write some VBA Code

 
 

The code below is a very simple add-in that displays the number of pages and the number of sections in the active document.

 
 

The additional command will be added to the bottom of the Tools drop-down menu to allow you to easily display the number of pages.

 
 

You will need to include some initialisation code in your add-in that will run automatically when the add-in is loaded.

 
 
1
2
3
4
5
6
7
8
Public Sub AutoExec()
Dim objcommandbutton As CommandBarButton
   Call MsgBox("AutoExec")
   Set objcommandbutton = Application.CommandBars("Tools").Controls.Add _
                                      (Type:=msoControlButton, Before:=1)
   objcommandbutton.Caption = "How Many Pages?"
   objcommandbutton.OnAction = "HowManyPages"
End Sub
   
 

You should also include some clean-up code which will run when the add-in is unloaded.

 
 
9
10
11
12
13
14
15
16
17
Public Sub AutoExit()
Dim objcommandbutton As CommandBarControl
   Call MsgBox("AutoExit")
   For Each objcommandbutton In Application.CommandBars("Tools").Controls
      If objcommandbutton.Caption = "How Many Pages?" Then
         objcommandbutton.Delete
      End If
   Next objcommandbutton
End Sub
   
 

This subroutine will display the number of pages in the active document.

 
 
18
19
20
21
22
23
24
Public Sub HowManyPages()
   If Documents.Count > 0 Then
      Call MsgBox(ActiveDocument.BuiltInDocumentProperties("Number of Pages"))
   Else
      Call MsgBox("No document is currently active.")
   End If
End Sub
   

 

Step 2 - Provide a Name and Description.

 
 

Unlike Excel the description of your add-in is not displayed in the (Tools > Templates and Add-ins) dialog box.

 
 

Filling in the (File > Properties)(Summary tab) for Word add-ins is less important.

 
 

The name that appears in the (Tools > Templates and Add-ins) dialog box is just the name of the Word add-in so it is important to save your add-in with a meaningful name.

 

 

Step 3 - Protecting your Code

 
 

It is often a good idea to protect your code from being viewed and modified. This can be done by password protecting the project.

 
 

This is less important for Word add-ins since the VBA Project cannot be seen when the add-in is loaded.

 
 

This can be done by filling in the (Tools > VBAProject Properties)(Protection tab).

 
 

To prevent a project from being viewed check the "Lock project for viewing" check box and enter a password.

 
 

Try to use a password that you will remember as there is no way to access the code if you forget the password.

 
 

The contents of the Confirm Password box and the Password box must match when you press OK or you get an error.

 
 

 (Tools > VBAProject Properties)(Protection tab)

 
 

If you do not check the Lock Project for Viewing option but set a password, you will be required to enter a password the next time you open the (Tools > VBAProject Properties) dialog box.

 
 

It is probably also worth giving your VBA project a name and description. This can be done from the General tab.

 
 

Any passwords and locking that you apply to your project will not take effect until the project is closed and reopened.

 

 

Step 4 - Save as a Word Add-in (".dot")

 
 

You should always recompile your code before saving the document as an add-in.

 
 

If the code has not been pre-compiled then your add-in will take slightly longer to run the first time.

 
 

You can compile you code by selecting (Debug > Compile).

 
 

To save your document select (File > SaveAs) to display the Save As dialog box.

 
 

Select Document Template (*.dot) from the Save as Type drop-down.

 
 

 (File > SaveAs) dialog box

 
 

The folder path will change automatically to the default folder path for your add-ins.

 
 

Word 2003 - C:\Documents and Settings\"user name"\Application Data\Microsoft\Templates\

 
 

Word 2002 - C:\Documents and Settings\"user name"\Application Data\Microsoft\Templates\

 
 

Word 2000 - C:\Documents and Settings\"user name"\Application Data\Microsoft\Templates\

 
 

Windows NT 4.0 - C:\Winnt\Profiles\"user name"\Application Data\Microsoft\Templates\

 
 

Windows 95 and 98 - C:\Windows\Profiles\"user name"\Application Data\Microsoft\Templates\

 
 

Windows 95 and 98 - C:\Windows\Application Data\Microsoft\Templates\ (when user profiles are not enabled)

 
 

Word 97 - C:\Program Files\Microsoft Office\Office\Templates\

 

 

It is possible to save the add-in to a different directory.

 
 

You can also specify a different folder path using (Tools > Options)(File locations tab, User templates).

 
 

Once you are happy with the name of your add-in and the folder which it will be saved to press "OK".

 

 

Step 5 - Things to Remember

 
 
  • Any procedures in an add-in are not displayed in the (Tools > Macro > Macros) dialog box.

     
     
  • If you want Word to automatically install your add-in then save it in the default "Startup" folder.

     
     
  • Your comments are not automatically removed when the file is saved as an add-in. Removing all the comments will help to reduce the file size.

     
     
  • Word add-ins should be as small as possible. The smaller the file size the faster they load.

     
     
  • Once a Word document has been converted to an add-in the VBA Project will be unviewable.

     

     Copyright © 2004-2007 Better Solutions Limited. All Rights Reserved.< Previous | Top | Next >