Why Word VBA Quick Parts Insertion Returns ‘Error 5398’ on Building Blocks
🔍 WiseChecker

Why Word VBA Quick Parts Insertion Returns ‘Error 5398’ on Building Blocks

When you run a VBA macro to insert a Quick Part in Word, you may receive runtime error 5398 with the message “The requested member of the collection does not exist.” This error occurs specifically when the macro tries to access a building block that is not present in the target template or document. The root cause is typically a mismatch between the building block name, gallery, or category referenced in code and what is actually stored in the Building Blocks Organizer. This article explains why error 5398 appears during VBA-driven Quick Parts insertion and provides precise steps to fix your code and environment.

Key Takeaways: Fixing VBA Error 5398 When Inserting Quick Parts

  • BuildingBlockEntries.Item method: Error 5398 means the specific building block name, gallery, or category does not exist in the template referenced by the macro.
  • Application.Templates property: The macro must target the correct template (Normal.dotm, custom .dotx, or .dotm) that contains the Quick Part; otherwise the collection is empty.
  • BuildingBlock.Insert method: After locating the correct building block, call Insert where the selection cursor sits; failure to specify a valid Range also triggers error 5398.

ADVERTISEMENT

Why VBA Error 5398 Occurs When Inserting Quick Parts

Word stores Quick Parts as building blocks inside templates. Each building block belongs to a gallery (such as wdBuiltInGalleryQuickParts) and a category (such as “General” or a custom name). When your VBA code runs BuildingBlockEntries.Item("MyQuickPart"), Word searches the building block collection of the template attached to the document or explicitly referenced in code. If the exact name, gallery, or category does not match, Word throws error 5398.

Three main conditions cause this mismatch:

  • The building block is stored in a different template than the one the macro is querying. For example, the Quick Part lives in Normal.dotm but the macro reads from the active document’s attached template.
  • The building block name in the VBA Item argument has a typo or uses a different case than the stored name. Building block names are case-sensitive.
  • The gallery constant or category string in the code does not match the gallery or category where the building block actually resides. A Quick Part stored under “Custom” category cannot be found by searching “General”.

Understanding these three causes is the first step to resolving the error. The fix involves verifying the building block’s exact location and adjusting the VBA code accordingly.

Steps to Identify and Fix the Building Block Reference

Follow these steps to locate the correct building block and modify your macro to avoid error 5398.

  1. Open the Building Blocks Organizer
    Press Alt+F11 to open the VBA editor, then return to Word. Press Alt+N, B, O (or go to Insert > Quick Parts > Building Blocks Organizer). This shows all building blocks in the current template.
  2. Identify the exact building block name, gallery, and category
    In the Building Blocks Organizer, locate the Quick Part you want to insert. Note the Name, Gallery (e.g., Quick Parts), and Category (e.g., General). Right-click the entry and choose Edit Properties to see the exact name without trailing spaces.
  3. Determine which template holds the building block
    In the Building Blocks Organizer, the template column shows the parent template (e.g., Normal.dotm or a custom template). Write down the full file name. Your macro must reference this specific template.
  4. Update your VBA code to target the correct template
    Replace any generic template reference with the specific template. For example, change:
    Set objTemplate = ActiveDocument.AttachedTemplate
    to:
    Set objTemplate = Application.Templates("Normal.dotm")
    Replace “Normal.dotm” with the exact template name from step 3.
  5. Use the correct gallery and category constants
    Verify the gallery constant. For Quick Parts, use wdBuiltInGalleryQuickParts (value 17). For the category, use the exact string from step 2. Example:
    Set objBB = objTemplate.BuildingBlockEntries.Item("MyQuickPart")
    If the category is not “General”, include the Category parameter when using the BuildingBlocks collection:
    Set objBB = objTemplate.BuildingBlockTypes(wdBuiltInGalleryQuickParts).Categories("Custom").BuildingBlocks("MyQuickPart")
  6. Insert the building block at the selection
    After obtaining the correct BuildingBlock object, call the Insert method. Example:
    objBB.Insert Selection.Range
    This places the Quick Part at the current cursor position. If you need a specific range, use ActiveDocument.Range(Start:=0, End:=0) to insert at the beginning of the document.
  7. Test the macro
    Run the updated macro. If error 5398 still appears, return to step 1 and double-check the building block name for hidden characters or spaces. Use Debug.Print objTemplate.BuildingBlockEntries.Count to confirm the template contains building blocks.

ADVERTISEMENT

If Error 5398 Persists After Correcting the Code

Even after adjusting the template, name, gallery, and category, error 5398 can still occur due to less obvious factors. The sections below cover the most frequent remaining causes.

The building block was created in a different language version of Word

Building block names and category strings are language-specific. If the Quick Part was created in a French or German Word version, the gallery name may differ. In the Building Blocks Organizer, examine the Gallery column — it may show “QuickParties” instead of “Quick Parts”. Use the exact localized string in your VBA code, or better, use the numeric constant for the gallery (e.g., 17 for Quick Parts) to avoid language dependence.

The macro runs before the template is fully loaded

If your macro runs during the Document_Open event or immediately after Word starts, the building block collection may not yet be available. Add a small delay using Application.Wait Now + TimeValue("00:00:01") or call DoEvents before accessing the template. This ensures the Building Blocks Organizer is fully initialized.

The building block is stored in a global template add-in

Quick Parts can reside in global templates loaded via File > Options > Add-ins. If the macro only checks the active document’s attached template, it will miss building blocks stored in a global template. Loop through Application.Templates and check each template’s BuildingBlockEntries collection until the desired block is found.

VBA Code Patterns: Direct Reference vs. Loop Through All Templates

Item Direct Template Reference Loop Through All Templates
Code complexity Simple one-line assignment Requires For Each loop and error handling
Performance Fast — single template query Slower — checks every loaded template
Error 5398 risk High if template name is wrong Low — finds block regardless of location
Recommended for Known template with guaranteed block Shared documents or unknown template origins

Conclusion

Error 5398 in Word VBA when inserting Quick Parts is caused by a mismatch between the building block reference in code and the actual stored building block. By verifying the template, name, gallery, and category through the Building Blocks Organizer, you can correct your macro and eliminate the error. For robust code, loop through all loaded templates or use numeric gallery constants to avoid language issues. After fixing the reference, the BuildingBlock.Insert method will place the Quick Part exactly where needed.

ADVERTISEMENT