none
how to insert building blocks in headers and footers RRS feed

  • Question

  • I want to insert a building block into headers and footers. The first problem is that the code I found is not working because it can't find the templates. The code works if I first manually insert a building block.

    I found that there are 2 ways to insert building blocks, witch is the best and the most reliable 


    Jan Vereijken Wagemaker Adviesgroep B.V.

    Tuesday, May 31, 2016 4:59 AM

Answers

  • I found de solution.

    First I have to attach de building block, that is done with:

       Templates.LoadBuildingBlocks

    Then insert a building block into the header or footer

    Dim WM_BBTemplate As String
    Dim WM_BBkop As String

    WM_BBTemplate = "C:\Users\" & Environ("Username") & "\AppData\Roaming\Microsoft\Document Building Blocks\1043\16\SectionHeaderFooters.dotx"
    WM_BBkop = "Kop A4 L"
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Application.Templates( _
            WM_BBTemplate _
            ).BuildingBlockEntries(WM_BBkop).Insert Where:=Selection.Range _
            , RichText:=True


    Jan Vereijken Wagemaker Adviesgroep B.V.

    • Marked as answer by Jan Vereijken Wednesday, June 1, 2016 6:56 AM
    Wednesday, June 1, 2016 6:55 AM

All replies

  • Which two?

    Best regards, George

    Tuesday, May 31, 2016 7:02 AM
  • I used the BuildingBlockEntries, but I didn't get it to work.

    Application.Templates( _

            "C:\Users\XXX\AppData\Roaming\Microsoft\Word\STARTUP\Rapport.dotx"). _

            BuildingBlockEntries("Koptekst Wagemaker").Insert Where:=Selection.Range, _

             RichText:=True

    The XXX is for each user differtent

    I also used BuildingBlockTypes

    ActiveDocument.AttachedTemplate.BuildingBlockTypes(wdTypeCustomHeaders).Categories("Algemeen").BuildingBlocks("Kop A4 L").Insert Selection.Range


    Jan Vereijken Wagemaker Adviesgroep B.V.

    Tuesday, May 31, 2016 7:12 AM
  • "Didn't get it to work" is not helpful. What error messages do you get?

    Best regards, George

    Tuesday, May 31, 2016 7:24 AM
  • The error is:

    Run-time error '5941' The requested member of the collection does not exist.

    The source code is:
    Application.Templates( _
         "C:\Users\JFV\AppData\Roaming\Microsoft\Document Building Blocks\1043\16\SectionHeaderFooters.dotx" _
                ).BuildingBlockEntries("Voet A4 L").Insert Where:=Selection.Range _
            , RichText:=True

    The file SectionHeaderFooters.dotx exist on that location

    The "Voet A4 L" exist in the building block library.


    Jan Vereijken Wagemaker Adviesgroep B.V.

    Tuesday, May 31, 2016 11:21 AM
  • I now notice that the error above only appears as long as you don't open the buildingblock builder (or when you insert a building block). When opened once there is no more error. So the remaining question will be "how to activate the buildingblock files, so the macro will work fine".

    I also noticed that when you open Word, the buildingblock files aren't opened (there is no ~$ctionHeaderFooter.dotx). They are opened when you for example insert a building block.


    Jan Vereijken Wagemaker Adviesgroep B.V.

    Tuesday, May 31, 2016 12:56 PM
  • Hi Jan Vereijken,

    did you try like below. you can try.

    Sub demo2()
    Dim objTemplate As Template
    Dim objBB As BuildingBlock
     
    Set objTemplate = Templates(1)
     
    Set objBB = objTemplate.BuildingBlockEntries _
     .Add(Name:="New Building Block Entry", _
     Type:=wdTypeWatermarks, _
     Category:="General", _
     Range:=Selection.Range)
    
    End Sub

    For more information regarding Building Blocks please visit the links below.

    Working with Building Blocks

    BuildingBlock Object (Word)

    BuildingBlockEntries Object (Word)

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, June 1, 2016 1:01 AM
    Moderator
  • I found de solution.

    First I have to attach de building block, that is done with:

       Templates.LoadBuildingBlocks

    Then insert a building block into the header or footer

    Dim WM_BBTemplate As String
    Dim WM_BBkop As String

    WM_BBTemplate = "C:\Users\" & Environ("Username") & "\AppData\Roaming\Microsoft\Document Building Blocks\1043\16\SectionHeaderFooters.dotx"
    WM_BBkop = "Kop A4 L"
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Application.Templates( _
            WM_BBTemplate _
            ).BuildingBlockEntries(WM_BBkop).Insert Where:=Selection.Range _
            , RichText:=True


    Jan Vereijken Wagemaker Adviesgroep B.V.

    • Marked as answer by Jan Vereijken Wednesday, June 1, 2016 6:56 AM
    Wednesday, June 1, 2016 6:55 AM
  • Hi Jan Vereijken,

    its good to hear from you that you got the solution for your issue by yourself and Thank you for sharing the solution with our community so that if some other member have same issue like you can also get solution by your post.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 1, 2016 7:05 AM
    Moderator