View Single Post
 
Old 01-02-2016, 02:20 PM
jc491's Avatar
jc491 jc491 is offline Windows 10 Office 2016
VBA Novice
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default VBA Word - Building Block (AutoText) - Array - Replace Text with Specific Building Blocks

Hello to all,

and happy new year!

I was hoping some of the kind experts may be able to help me on this Autotext Building block problem.

I have a number of building blocks that need to be inserted into documents.

As per usual I have been running them individually. The Problem is I have too many building blocks to insert.


For Example


Find Code | Insert Building Block

BBS1 | Signature 1
BBS2 | Signature 2
............
............
............
and on and on etc


Each document may have 10+ building blocks to find and insert. Hence what is a not once a difficult task becomes a rather manually laborious process - I have to do this to 100s of documents.


I have seen this excellent thread.

https://www.msofficeforums.com/word-...using-vba.html


I have been trying to adapt it for the past week, and am just no closer to understanding the complex nature of the programming.

My non working attempt

Code:
Sub BuildingBlockArrayMacro() 

' Replace Array Words with a Building Block - Autotext

    Dim Rng As Word.Range 
    Dim ArrayList
    Dim i As Long 
    ArrayList= Array("#BB1", "#BB2") 
    
For i = 0 To UBound(ArrayList) 
        Set Rng = ActiveDocument.Range 
        With Rng.Find 
            .ClearFormatting
            .Text = ArrayList(i) 
            .MatchWholeWord = True 


        While .Execute

' Need to insert search?

' Got stuck here

if  .text = #BB1 then

      ActiveDocument.AttachedTemplate.Builtinbuildingblocks("Signature1").Insert Where:=Rng, 

RichText:=True

ElseIf  .text = #BB2 then

  ActiveDocument.AttachedTemplate.Builtinbuildingblocks("Signature2").Insert Where:=Rng, 

RichText:=True

        End With 
    Next 
End Sub
I have referenced the following - as well

https://www.msofficeforums.com/word-...using-vba.html

http://gregmaxey.com/word_tip_pages/...d_replace.html

I do believe I am not structurally creating the code correctly - I've got VBA blindness again.



To Recap I would like to:

1. Find specific text in my document
2. Replace each text with the correct building blocks


I would be really grateful as always for helping to solve this problem.


I appreciate this may be a very advanced VBA code - as to why I am really stuck and appreciate the time and help from the individuals on the forum.


Thank you in advance for all your time and help.
J
Reply With Quote