• Word 2010: Quick Parts footers not behaving when inserted with macro

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Word 2010: Quick Parts footers not behaving when inserted with macro

    Author
    Topic
    #485169

    I’ve been playing with Quick Parts and loving being able to create a Quick Parts building block that I assigned either to a footer or a watermark gallery.

    I’ve been using the Building Blocks Organiser to insert quick parts, and love it. When I select a building block that is assigned to the footer gallery it automatically opens the footer and inserts it and all I have to do is close the footer.

    When I select a building block that is assigned to the watermark gallery it puts the watemark in the header.

    However, when I then use VBA to insert the quick part I get different results:

    The footer quick part gets added to the header.
    The watermark quick part doesn’t get added to the header, it just appears on the current page of the document, behind text.

    This is the code I’m using. I really hope someone can point out where I’m going wrong. This code works beautifully for standard autotext entries that just have to appear at the insertion point, and who have been assigned to the autotext gallery.

    Code:
    Public strAutotext As String
    Sub InsertLAAutotext()
    Dim strMasterTemplate As Template
    Dim colAutoText As AutoTextEntries
    Dim objAutoText As AutoTextEntry
    ‘Dim arrEntries As Variant
    Dim i As Integer
    Templates.LoadBuildingBlocks
    i = 1
    For Each atemp In Templates
        If atemp = “Legal Assist Building Blocks.dotx” Then Set strMasterTemplate = Templates(i)
        i = i + 1
    Next atemp
    ReDim arrEntries(0)
    Set colAutoText = strMasterTemplate.AutoTextEntries
    For Each objAutoText In colAutoText
        Bound = UBound(arrEntries)
            If arrEntries(Bound) = “” Then
                arrEntries(Bound) = objAutoText.Name
            Else
                ReDim Preserve arrEntries(Bound + 1)
                arrEntries(Bound + 1) = objAutoText.Name
            End If
    Next objAutoText
    strMasterTemplate.BuildingBlockEntries(strAutotext). _
        Insert Where:=Selection.Range, RichText:=True
    
    End Sub
    Sub WatermarkFileCopy()
         strAutotext = “FileCopy”
        Call InsertLAAutotext
        
    End Sub
    Sub WatermarkConfidential()
       strAutotext = “Confidential”
       Call InsertLAAutotext
    End Sub
    Sub WatermarkDraft()
        strAutotext = “Draft”
        Call InsertLAAutotext
    End Sub
    Sub RemoveWatermark()
        Application.ScreenUpdating = False
        Dim RngSel As Range, Scn As Section, HdFt As HeaderFooter, iView As Long
        With ActiveDocument
            Set RngSel = Selection.Range
            iView = ActiveWindow.View.Type
            For Each Scn In .Sections
                For Each HdFt In Scn.Headers
                    HdFt.Range.Select
                    WordBasic.RemoveWatermark
                Next
    ‘            For Each HdFt In Scn.Footers
    ‘                HdFt.Range.Select
    ‘                WordBasic.RemoveWatermark
    ‘            Next
            Next
        End With
    
        RngSel.Select
        ActiveWindow.View.Type = iView
        Set RngSel = Nothing
        Application.ScreenUpdating = True
    End Sub
    
    
    Viewing 1 reply thread
    Author
    Replies
    • #1347843

      Try replacing the public declaration and first few macros with the following

      Code:
      Function InsertLAAutotext(strAutotext As String)
      
        Dim strMasterTemplate As Template, atemp As Template
        Templates.LoadBuildingBlocks
        For Each atemp In Templates
          Debug.Print atemp.Name
          If atemp = “Legal Assist Building Blocks.dotx” Then
            Set strMasterTemplate = atemp
            Exit For
          End If
        Next atemp
        strMasterTemplate.BuildingBlockEntries(strAutotext).Insert _
             Where:=Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range, RichText:=True
      End Function
      Sub WatermarkFileCopy()
        InsertLAAutotext “FileCopy”
      End Sub
      Sub WatermarkConfidential()
        InsertLAAutotext “Confidential”
      End Sub
      Sub WatermarkDraft()
        InsertLAAutotext “Draft”
      End Sub
      • #1347910

        Thank you Andrew. That works beautifully for the watermarks. Is there anyway I select whether it goes into a header or footer (or just at the insertion point). This is because we use the building blocks for other things as well – some go into footers, and some just in the document in the insertion point?

        I could copy the function and create one for each of the options, but I just wonder how I would go about making it generic for all insertions of autotext.

        • #1347912

          Do you want to decide this location based on the Building Block category or on a second parameter passed into the function?

    • #1347976

      Try this (untested) code

      Code:
      Function InsertLAAutotext(strAutotext As String, Optional sPos As String)
        Dim strMasterTemplate As Template, atemp As Template
        Templates.LoadBuildingBlocks
        For Each atemp In Templates
          Debug.Print atemp.Name
          If atemp = “Legal Assist Building Blocks.dotx” Then
            Set strMasterTemplate = atemp
            Exit For
          End If
        Next atemp
        Select Case sPos
          Case “Header”
            strMasterTemplate.BuildingBlockEntries(strAutotext).Insert _
                 Where:=Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range, RichText:=True
          Case “Footer”
            strMasterTemplate.BuildingBlockEntries(strAutotext).Insert _
                 Where:=Selection.Sections(1).Footers(wdHeaderFooterPrimary).Range, RichText:=True
          Case Else
            strMasterTemplate.BuildingBlockEntries(strAutotext).Insert _
                 Where:=Selection.Range, RichText:=True
        End Select
      End Function
      Sub WatermarkFileCopy()
        InsertLAAutotext “FileCopy”   ‘inserts at selection
        InsertLAAutotext “FileCopy”, “Header”   ‘inserts into header
        InsertLAAutotext “FileCopy”, “Footer”   ‘inserts into footer
      End Sub
      
    Viewing 1 reply thread
    Reply To: Word 2010: Quick Parts footers not behaving when inserted with macro

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

    Your information: