Creating


Create a Document ".doc"

To create a Word add-in you must first create a Word document.
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 document because you cannot make any changes to the VBA code once the add-in has been created.


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.
If you are using Word 2003 then an additional command will be added to the bottom of the Tools drop-down menu to allow you to easily display the number of pages.
If you are using Word 2007 (or later) then an additional comman will appear on the Add-ins tab in the Custom ?? Group.
You will need to include some initialisation code in your add-in that will run automatically when the add-in is loaded.

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.

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.

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

Provide a Name and Description.

Unlike Excel the description of your add-in is not displayed in the "Templates and Add-ins" dialog box.
In Word 2003 filling in the (File > Properties)(Summary tab) for Word add-ins is less important.
In Word 2007 filling in the (Office > Prepare > Properties)(Summary tab) for Word add-ins is less important.
The name that appears in the "Templates and Add-ins" dialog box is just the name of the add-in so it is important to save your add-in with a meaningful name.


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) from within the Visual Basic Editor.
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.

microsoft excel docs

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.


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.

microsoft excel docs

The folder path will change automatically to the default folder path for your add-ins.
Microsoft 365 - C:\Users\"user name"\AppData\Roaming\Microsoft\Templates\
Word 2019 - C:\Users\"user name"\App Data\Microsoft\Templates\
Word 2016 - C:\Users\"user name"\App Data\Microsoft\Templates\
Word 2013 - C:\Users\"user name"\App Data\Microsoft\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".


Important

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.


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