Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Running Commands When Inserting Building Blocks into Word 2007 Documents

Office 2007
Office Visual How To

Applies to: 2007 Microsoft Office System, Microsoft Office Word 2007

Joel Krist, Akona Systems

May 2007

Overview

Microsoft Office Word 2007 introduces building blocks to help with the process of adding frequently used content to documents. You can select building blocks from a predefined gallery of cover pages, pull quotes, headers, and footers to make documents look more professional. You can also create building blocks to simplify adding custom text, such as legal disclaimer text or other frequently used materials. The Word 2007 object model provides the Document.BuildingBlockInsert event, which is used to watch for a user inserting a new building block into a document. This how-to topic demonstrates how to use the Document.BuildingBlockInsert event to run a command in response to the insertion of a building block by a user.

Code It

To demonstrate how to run a command when a building block is inserted into a document, this how-to topic walks through two key steps: adding a BuildingBlockInsert document event procedure, and adding code to the event procedure.

To add a BuildingBlockInsert event procedure

  1. Launch the Visual Basic Editor from within Word 2007. To do this, press Alt+F11 or select the Developer tab on the ribbon and click the Visual Basic button in the Code group. If the Developer tab is not visible, make it visible by selecting the Office Button, then select the Word Options button. In the Word Options dialog box, select the Popular category and then select the Show Developer tab in the Ribbon option so that it is checked. Click OK to save the change.

  2. Select the View menu, then select Project Explorer. The Project Explorer window is displayed.

  3. If the BuildingBlockInsert event procedure should be applied to all documents that use the Normal template, then in the Project Explorer window, expand the Normal project folder. If the BuildingBlockInsert event procedure should be applied to just the current document, then expand the document project folder.

  4. Under the Normal project or the document project folder, expand the Microsoft Word Objects folder and double-click ThisDocument. This will display the code window.

  5. On the Object list, select Document. An empty subroutine for the New event is added to the class module.

  6. On the Procedure list, select the BuildingBlockInsert event. An empty subroutine for the BuildingBlockInsert event is added to the class module.

Add Code to the BuildingBlockInsert Event Procedure

Modify the Document_BuildingBlockInsert event procedure created earlier so that it contains code that provides the desired functionality. The following sample code displays a message box that shows the properties of the building block that was just inserted.

Private Sub Document_BuildingBlockInsert(ByVal Range As Range, _
    ByVal Name As String, ByVal Category As String, _
    ByVal BlockType As String, ByVal Template As String)

    MsgBox "A building block was inserted:" & Chr(10) & Chr(10) & _
           "Name: " & Name & Chr(10) & _
           "Category: " & Category & Chr(10) & _
           "Type: " & BlockType, , "Custom Command"

End Sub
Read It

This article explores how to run a command in response to a user inserting a building block into a Word 2007 document using the Document.BuildingBlockInsert event. The key steps include:

  1. Adding a BuildingBlockInsert document event procedure.

  2. Adding code to the event procedure.

See It

Running Commands when Inserting Building Blocks

Watch the Video

Video Length: 00:02:34

File Size: 1.67 MB WMV

Explore It

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.