Results 1 to 20 of 27

Thread: Placing Building Blocks at Bookmarks

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    Jumping in late here to offer my own two cents (pence) :-). I use buildingblocks (AutoText) quite a bit and employ several of the Galleries and custom categories. With that practice, it is possible to have two or more building blocks defined such that Word may or may not know the correct BB to insert using the old AutoTextEntries method. Therefore, I try to explicitly define the BuidlingBlock to define by template, gallery, catefory and name.

    I've also included my code for insert at a content control vice bookmark:

    Sub InsertDemos()
    '  'For building blocks in the default template "Normal.dotm", default Gallery "AutoText"
    '  'and default Category "General" simmply pass the bookmark and building block names as arguments e.g.,:
    '  BBtoBM "BMTarget_Header", "GKM"
    '  'For building blocks in the document attached template (other than Normal.dotm),
    '  'pass the bookmark name, building block name and ThisDocument.AttachedTemplate.Name as arguments.
    '  'If the gallery or category is not the default, pass them as well e.g.,
    '  BBtoBM "BMTarget_1", "Car", ThisDocument.AttachedTemplate.Name, 29, "Classic"
    '  'For building blocks in a named tempalte (not Normal.dotm or not the attached template), pass the template
    '  'name (without extenstion) e.g., a Table of Contents from the built in building blocks.
    '  BBtoBM "BMTarget_1", "Manual Table", "Built-in Building Blocks", 14, "Built-in"
      'Similar process using a titled content control.
      BBtoCC "CCTarget_1", "GKM"
      BBtoCC "CCTarget_Header", "Car", ThisDocument.AttachedTemplate.Name, 29, "Classic"
      BBtoCC "CCTarget_1", "Manual Table", "Built-in Building Blocks", 14, "Built-in"
      BBtoCC "CCTarget_1", "Car", ThisDocument.AttachedTemplate.Name, 29, "Classic"
    lbl_Exit:
      Exit Sub
    End Sub
    Public Sub BBtoBM(BMName As String, BBName As String, _
                      Optional Template As String = "Normal", _
                      Optional GalleryIndex As Long = 9, _
                      Optional Category As String = "General")
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/18/2016
    'To use BuildingBlocks from a Gallery other than "AutoText" pass the appropriate
    'number as the Gallery argument.
    '1 Quick Parts|2 Cover Pages|3 Equations|4 Footers|5 Headers|6 Page Numbers|7 Tables
    '8 Watermarks|9 AutoText|10 Text Boxes|11 Page Numbers (Top of Page)|12 Page Numbers (Bottom of Page)
    '13 Page Numbers (Margins)|14 Table of Contents|15 Custom Quick Parts|16 Custom Cover Pages
    '17 Custom Equations|18 Custom Footers|19 Custom Headers|20 Custom Page Numbers|21 Custom Tables
    '22 Custom Watermarks|23 Custom AutoText|24 Custom Text Boxes|25 Custom Page Numbers (Top of Page)
    '26 Custom Page Numbers (Bottom of Page)|27 Custom Page Numbers (Margins)|28 Custom Tables
    '29 Custom 1|30 Custom 2|31 Custom 3|32 Custom 4|33 Custom 5|34 Bibliographies|'35 Custom Bibliographies
    Dim oTmpLoaded As Template, oTmp As Template
      Select Case Template
        Case "Normal"
          Set oTmp = Templates("Normal.dotm")
        Case ActiveDocument.AttachedTemplate.Name
           Set oTmp = ActiveDocument.AttachedTemplate 'Templates(Template)
        Case Else
          If Template = "Built-in Building Blocks" Then Templates.LoadBuildingBlocks
          For Each oTmpLoaded In Templates
            If UCase(CreateObject("scripting.filesystemobject").GetBaseName(oTmpLoaded.Name)) = UCase(Template) Then
              Set oTmp = oTmpLoaded
              Exit For
            End If
          Next
      End Select
      If Not oTmp Is Nothing Then
        If ActiveDocument.Bookmarks.Exists(BMName) Then
          On Error Resume Next
          ActiveDocument.Bookmarks.Add Name:=BMName, _
              Range:=oTmp.BuildingBlockTypes(GalleryIndex).Categories(Category).BuildingBlocks(BBName).Insert _
              (Where:=ActiveDocument.Bookmarks(BMName).Range, RichText:=True)
          If Not Err.number = 0 Then
            MsgBox "The defined buildingblock is not available.", vbInformation + vbOKOnly, "BUILDINGBLOCK NOT FOUND"
          End If
        Else
          MsgBox "The defined bookmark is not found in the document.", vbInformation + vbOKOnly, "BOOKMARK NOT FOUND"
        End If
      Else
        MsgBox "The building block template is not available.", vbInformation + vbOKOnly, "TEMPLATE NOT LOADED"
      End If
      
    lbl_Exit:
      Set oTmpLoaded = Nothing: Set oTmp = Nothing
      Exit Sub
    End Sub
    Public Sub BBtoCC(CCTitle As String, BBName As String, _
                      Optional Template As String = "Normal", _
                      Optional GalleryIndex As Long = 9, _
                      Optional Category As String = "General")
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/18/2016
    
    'To use BuildingBlocks from a Gallery other than "AutoText" pass the appropriate
    'number as the Gallery argument.
    '1 Quick Parts|2 Cover Pages|3 Equations|4 Footers|5 Headers|6 Page Numbers|7 Tables
    '8 Watermarks|9 AutoText|10 Text Boxes|11 Page Numbers (Top of Page)|12 Page Numbers (Bottom of Page)
    '13 Page Numbers (Margins)|14 Table of Contents|15 Custom Quick Parts|16 Custom Cover Pages
    '17 Custom Equations|18 Custom Footers|19 Custom Headers|20 Custom Page Numbers|21 Custom Tables
    '22 Custom Watermarks|23 Custom AutoText|24 Custom Text Boxes|25 Custom Page Numbers (Top of Page)
    '26 Custom Page Numbers (Bottom of Page)|27 Custom Page Numbers (Margins)|28 Custom Tables
    '29 Custom 1|30 Custom 2|31 Custom 3|32 Custom 4|33 Custom 5|34 Bibliographies|'35 Custom Bibliographies
    Dim oTmpLoaded As Template, oTmp As Template
    Dim oCC As ContentControl
      Select Case Template
        Case "Normal"
          Set oTmp = Templates("Normal.dotm")
        Case ActiveDocument.AttachedTemplate.Name
           Set oTmp = ActiveDocument.AttachedTemplate
        Case Else
          If Template = "Built-in Building Blocks" Then Templates.LoadBuildingBlocks
          For Each oTmpLoaded In Templates
            If UCase(CreateObject("scripting.filesystemobject").GetBaseName(oTmpLoaded.Name)) = UCase(Template) Then
              Set oTmp = oTmpLoaded
              Exit For
            End If
          Next
      End Select
      If Not oTmp Is Nothing Then
        On Error Resume Next
        Set oCC = ActiveDocument.SelectContentControlsByTitle(CCTitle).Item(1)
        If Not oCC.Type = wdContentControlRichText Then oCC.Type = wdContentControlRichText
        If Not oCC Is Nothing Then
          oTmp.BuildingBlockTypes(GalleryIndex).Categories(Category).BuildingBlocks(BBName).Insert oCC.Range, True
          If Not Err.number = 0 Then
            MsgBox "The defined buildingblock is not available.", vbInformation + vbOKOnly, "BUILDINGBLOCK NOT FOUND"
          End If
        Else
          MsgBox "The defined content control is not found in the document.", vbInformation + vbOKOnly, "BOOKMARK NOT FOUND"
        End If
      Else
        MsgBox "The building block template is not available.", vbInformation + vbOKOnly, "TEMPLATE NOT LOADED"
      End If
      
    lbl_Exit:
      Set oTmpLoaded = Nothing: Set oTmp = Nothing: Set oCC = Nothing
      Exit Sub
    End Sub
    Last edited by gmaxey; 12-18-2017 at 02:19 AM.
    Greg

    Visit my website: http://gregmaxey.com

Tags for this Thread

Posting Permissions

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