Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2016, 10:34 PM
gmayor's Avatar
gmayor gmayor is offline VBA Word -  Building Block (AutoText) - Array - Replace Text with Specific Building Blocks Windows 10 VBA Word -  Building Block (AutoText) - Array - Replace Text with Specific Building Blocks Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It would make far more sense to rename your building blocks to match the array values (or vice versa) then you can simply insert the building block that matches the name. The method you have attempted is fine if there are only two different values in the document, but if there are ten it gets rather unwieldy. You can then use code such as the following.

If you have hundreds of documents to process then you might investigate http://www.gmayor.com/document_batch_processes.htm with a custom process (or depending on what is in the building blocks, a replace from table). The following macro can easily be modified to match the format used by the add-in's custrom process option, as shown in the second panel below.

Code:
Option Explicit

Sub BuildingBlockArrayMacro()

' Replace Array Words with a Building Block - Autotext

Dim Rng As Word.Range
Dim ArrayList As Variant
Dim i As Long
    ArrayList = Array("#BB1", "#BB2")
    For i = 0 To UBound(ArrayList)
        Set Rng = ActiveDocument.Range
        With Rng.Find
            Do While .Execute(FindText:=ArrayList(i), MatchWholeWord:=True)
                Application.Templates(ActiveDocument.AttachedTemplate). _
                        BuildingBlockEntries(Rng.Text).Insert _
                        Where:=Rng, _
                        RichText:=True
                Rng.Collapse 0
            Loop
        End With
    Next i
lbl_Exit:
    Exit Sub
End Sub
Code:
Function BBReplace(oDoc As Document) As Boolean
Dim Rng As Word.Range
Dim ArrayList As Variant
Dim i As Long
    On Error GoTo err_Handler
    ArrayList = Array("#BB1", "#BB2")
    For i = 0 To UBound(ArrayList)
        Set Rng = oDoc.Range
        With Rng.Find
            .ClearFormatting
            Do While .Execute(FindText:=ArrayList(i), MatchWholeWord:=True)
                Application.Templates(oDoc.AttachedTemplate). _
                        BuildingBlockEntries(Rng.Text).Insert _
                        Where:=Rng, _
                        RichText:=True
                Rng.Collapse 0
            Loop
        End With
    Next i
    BBReplace = True
lbl_Exit:
    Exit Function
err_Handler:
    BBReplace = False
    GoTo lbl_Exit
End Function

__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
building block, vba word



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem batch adding autotext using Greg Maxey's Building Blocks Add-In Genuine Gin Word VBA 5 12-16-2015 10:20 AM
Formatting Issues When Using If Statements and Inserting Building Blocks/Autotext sanko787 Mail Merge 1 07-05-2014 09:00 PM
Building blocks show correctly in dropdown, but wrong building block populates in doc wordgirl123 Word 0 10-03-2013 08:30 AM
INSERT building blocks from Quickpart in word jasserin Word VBA 0 06-05-2013 12:55 PM
VBA Word -  Building Block (AutoText) - Array - Replace Text with Specific Building Blocks Word - Attach Building Blocks namedujour Word 0 04-04-2011 09:59 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:45 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft