Fix Word VBA Building Blocks Programmatic Access Returning Wrong Style
🔍 WiseChecker

Fix Word VBA Building Blocks Programmatic Access Returning Wrong Style

When you write VBA macros to insert building blocks such as headers, footers, or cover pages into a Word document, the programmatic access may return a different style than the one you specified. This causes the inserted content to appear with incorrect formatting, font, or color. The issue occurs because the building block’s underlying style name in the template does not match the style applied by the macro at runtime, often due to template naming conflicts or style priority settings. This article explains why the building block API returns the wrong style and provides a step-by-step method to fix the VBA code so that the correct style is always applied.

Key Takeaways: Fixing VBA Building Block Style Mismatch

  • BuildingBlock.Insert method with Range parameter: Ensures the building block is inserted at a specific location and inherits the style of the target paragraph.
  • Application.CustomizationContext = ActiveDocument.AttachedTemplate: Forces VBA to use the style names from the template attached to the active document, not the Normal template.
  • BuildingBlock.Type = wdTypeCustomAutoText: Use this type for custom building blocks to avoid conflicts with built-in gallery styles.

ADVERTISEMENT

Why VBA Building Block Access Returns the Wrong Style

The root cause of the wrong style appearing when you use VBA to programmatically access building blocks lies in how Word resolves style names during insertion. When you call the BuildingBlock.Insert method, Word looks up the style defined in the building block’s source template. If the style name in the template is the same as a style in the current document but with different formatting, Word applies the document’s style definition instead of the template’s original definition. This happens most frequently when the active document is based on a different template than the one containing the building block.

Another scenario involves building blocks stored in the Building Blocks.dotx template. If that template has a style named “Custom Header” with specific formatting, and the active document also contains a style named “Custom Header” but with different settings, Word merges the style from the document. The VBA code does not force the building block to bring its own style definition; it relies on the style name alone.

A third cause is the use of the wdTypeBuildingBlock gallery type without specifying a Range parameter. When you omit the Range, Word inserts the building block at the cursor position and applies the style of the current paragraph. If that paragraph has a style that overrides the building block’s intended style, the result is wrong.

Steps to Correct VBA Building Block Style Access

To fix the wrong style issue, you must modify the VBA code to explicitly set the template context, specify a target range, and verify the style name. Follow these steps.

  1. Open the VBA editor and locate the macro
    Press Alt + F11 in Word to open the Visual Basic Editor. In the Project Explorer, find the module that contains your building block insertion code.
  2. Set the customization context to the attached template
    Add the following line before the building block insertion code: Application.CustomizationContext = ActiveDocument.AttachedTemplate. This tells VBA to resolve style names from the template attached to the active document, not from the Normal template or any other template.
  3. Define a target range for insertion
    Create a Range object that points to where you want the building block to be inserted. For example: Set rngTarget = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range. Using a range ensures the building block inherits the style from that specific location.
  4. Use the BuildingBlock.Insert method with the range
    Call the method with the range parameter: objBuildingBlock.Insert rngTarget, True. The True parameter specifies that the building block should be inserted as rich text, preserving its original formatting as much as possible.
  5. Verify the building block type and category
    When retrieving the building block, use BuildingBlock.Type = wdTypeCustomAutoText instead of a built-in gallery type. This reduces the chance of style conflicts because custom auto text entries are less likely to have style name collisions with document styles.
  6. Test the macro with a fresh document
    Create a new document based on the same template that contains the building block. Run the macro and check the style of the inserted content. If the style is still wrong, examine the style name in the building block’s source template and ensure it is unique.

ADVERTISEMENT

If the Style Still Appears Incorrect

The building block style name conflicts with a built-in style

If your building block uses a style name that matches a built-in Word style such as “Normal,” “Heading 1,” or “Title,” Word always applies the built-in style’s definition. To fix this, rename the style in the building block’s source template to something unique, like “CustomHeader1” or “SpecialFooter.” Then update the VBA code to use the new style name when inserting the building block.

The attached template is different from the building block’s template

If the active document is attached to a different template than the one containing the building block, Word may not find the correct style. In the VBA code, use Application.CustomizationContext = Templates(“Building Blocks.dotx”) to force the context to the building block’s source template. Replace “Building Blocks.dotx” with the actual file name of the template that contains the building block.

The building block contains linked styles that do not travel with it

Some building blocks, especially those with images or tables, reference paragraph styles that are not embedded in the building block itself. When inserted, those styles are missing and Word substitutes the Normal style. To avoid this, save the building block with the “Store styles in the building block” option checked in the Create New Building Block dialog. In VBA, verify that the building block’s Style property is not empty before insertion.

VBA Building Block Insertion Methods: Comparison

Item Insert with Range Insert without Range
Style inheritance Inherits style from the target range’s paragraph Uses style of current cursor position
Style conflict risk Lower, because range defines context Higher, because cursor position may have conflicting style
Use case Inserting headers, footers, or page elements at a fixed location Inserting content at user’s cursor, such as auto text in body
VBA code complexity Requires Range object creation Simpler, one-line call

You can now write VBA macros that insert building blocks with the correct style every time. Start by setting the customization context to the attached template and always supply a range parameter. For advanced control, store building blocks with unique style names and use the wdTypeCustomAutoText type to avoid built-in style conflicts. As a next step, explore the BuildingBlock.Description property to log style information during debugging.

ADVERTISEMENT