Results 1 to 7 of 7
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Need help with QuickPart in BuildingBlock template

    Our Building Blocks template has two entries with the same name. One is an AutoText; the other a QuickPart. The AutoText entry is just text; the QuickPart entry is a text box with the same text in it.


    The following code sets the BuildingBlock template as oTemplate. The Insert method inserts the AutoText entry.

    Code:
    Dim oTemplate As Template
    Set oTemplate = Application.Templates("C:\...\Building Blocks.dotx")
    
    oTemplate.BuildingBlockEntries("ABC").Insert Where:=Selection.Range, _
            RichText:=True
    What must I do to insert the QuickPart (not AutoText) that has the name "ABC"?

    Thanks.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    You can be very specific if you know the type/category/name

    Code:
    oTemplate.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("ABC").Insert _
     Where:=Selection.Range, RichText:=True
    Last edited by Andrew Lockton; 2015-01-06 at 20:46.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Thank you. Interesting that the macro recording spills out the same code whether I chose QuickParts or AutoText. I'm really not a fan of using Building Blocks programmatically, but your info has helped.

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    I created a test template to holding custom Building Blocks. I named it "Test for BB.dotx" and placed it in C:\Program Files (x86)\Microsoft Office\Office12\Document Parts\1033.

    Building Block Organizer appears to see that template, and all of the QuickParts in the template are available to be inserted from the Organizer.

    If I record a macro to insert a QP, the macro reads as follows:

    Code:
    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bibliography"). _
            Insert Where:=Selection.Range, RichText:=True
        ActiveDocument.AttachedTemplate.BuildingBlockEntries("Conforming").Insert _
            Where:=Selection.Range, RichText:=True
        ActiveDocument.AttachedTemplate.BuildingBlockEntries("Conforming").Insert _
            Where:=Selection.Range, RichText:=True
    I know that's not correct, because I inserted only one BB entry. Running only the last line of code

    Code:
        ActiveDocument.AttachedTemplate.BuildingBlockEntries("Conforming").Insert _
            Where:=Selection.Range, RichText:=True
    results in an error: Requested member of the collection does not exist.

    The code I used previously

    Code:
    Dim oTemplate As Template
    Set oTemplate = Application.Templates("C:\Program Files (x86)\Microsoft Office\Office12\Document Parts\1033\test for bb.dotx")
    
    
        oTemplate.BuildingBlockTypes(wdTypeQuickParts).Categories("Test").BuildingBlocks("Conforming").Insert _
                Where:=Selection.Range, RichText:=True
    results in the same error. I understand that, because Test for BB.dotx is not a template in the (loaded) templates collection.

    Question: Why is it that the BB Organizer is able to see a BB template that is not loaded, and what must I do to get VBA code to insert an entry from that non-loaded template. Or must I put my BB template in the Startup folder?

    Thanks.

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I don't have time to investigate this at the moment but apparently the building blocks need to be 'loaded' first. This happens when you display the BB Organiser but you can do it in code too
    wordApplication.Templates.LoadBuildingBlocks()

    refer to http://msdn.microsoft.com/en-us/libr...code-snippet-1
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Thank you. The key is that Building Blocks don't load until the Organizer is displayed.

  7. #7
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    Technically, the building blocks will be loaded whenever you do something in the UI that requires a building block -- click Insert > Quick Part, Insert > Cover Page, open the Header or Footer or AutoText gallery, etc. -- or execute the Templates.LoadBuildingBlocks() statement. You'll notice a delay the first time you do one of the UI actions, but not after that in the same session.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •