• Need help with QuickPart in BuildingBlock template

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Need help with QuickPart in BuildingBlock template

    Author
    Topic
    #498038

    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.

    Viewing 2 reply threads
    Author
    Replies
    • #1483434

      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
      • #1483435

        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.

      • #1484420

        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 OfficeOffice12Document Parts1033.

        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 OfficeOffice12Document Parts1033test 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.

    • #1484453

      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/library/bb457184(v=office.12).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

      • #1484455

        Thank you. The key is that Building Blocks don’t load until the Organizer is displayed.

    • #1484704

      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.

    Viewing 2 reply threads
    Reply To: Reply #1484704 in Need help with QuickPart in BuildingBlock template

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel