Create macro-enabled templates
You can create your own macro-enabled templates to set default values and to perform merge and other important functions in WorkZone for Word.
Create a macro-enabled template
- Open a new Microsoft Word document.
- On the Developer tab in the Code group, click Visual Basic.
-Or-
Press Alt+F11.
A new Visual Basic document opens.
Example of an advanced document template
In this example, we are creating a macro-enabled template for a standard letter that prompts the user to select a case for a document and then recipients. Finally, it merges all the information into the document, after which the user can write the content of the letter.
Public WithEvents App As Word.Application
Public IsScriptExecuted As Boolean
Private Sub App_DocumentChange()
If Not IsScriptExecuted Then
Dim addIn As COMAddIn
Set addIn = Application.COMAddIns("Scanjour.Office.WordAddIn")
IsScriptExecuted = True
If Not addIn.Object.IsMergedDocument Then
' <- Place your code here (START)
addIn.Object.SetDocumentMetadata "title", "Hello"
addIn.Object.SetDocumentMetadata "record_type", "NT"
addIn.Object.SetDocumentMetadata "record_grp", "INF"
addIn.Object.RegistrationPaneVisible = True
addIn.Object.ShowDocumentRecipientsDialog
addIn.Object.Merge
' <- Place your code here (END)
End If
End If
End Sub
By default, the Developer tab is not displayed in the ribbon. To display it, you need to perform the following steps:
- Start Microsoft Word.
- On the File tab, select Options. The Word Options dialog box displays.
- In the categories pane, select Customize Ribbon.
- In the Main Tabs list, select the Developer check box.
- Click OK.
- On the Developer tab that has just appeared, click Visual Basic.
- In the Project pane, right-click the Microsoft Word Objects folder, and select Insert > Class Module. A new Class1 class is added to the project tree.
- Rename the Class1 module to ThisApplication.
- Select the Class1 module in the project tree. A Properties pane is displayed under the project tree.
- In the (Name) field, set ThisApplication.
- In the Project pane, double-click ThisApplication in the project tree. A new ThisApplication document opens.
- Enter a code in the ThisApplication document.
- Right-click your code project. Then click the Insert menu, and click Module.
- In the Properties pane (lower left corner of screen), click the Name field, and type AppEventHandler.
- In the Code pane (right side of screen), type the following:
Dim wordApp As New ThisApplication.
Sub AutoNew()
Set wordApp.App = Word.Application
wordApp.IsScriptExecuted = False
End Sub
- Save the document template in .dotm format.
- To use your template, just copy it to a target machine, and open it. As a result, a new document will be created based on your template (that is, with your template script executed).
Tip:
- To modify your template, right-click it from Windows Explorer, and select Edit from a context menu.
- You can have more than one macro template, however only one template will be used at the same time.