Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2016, 02:20 PM
jc491's Avatar
jc491 jc491 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
VBA Novice
VBA Word -  Building Block (AutoText) - Array - Replace Text with Specific Building Blocks
 
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
  #2  
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: 3,618
gmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to behold
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
  #3  
Old 01-03-2016, 04:57 AM
jc491's Avatar
jc491 jc491 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
VBA Novice
VBA Word -  Building Block (AutoText) - Array - Replace Text with Specific Building Blocks
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

Hello Graham,

thank you very for much for your help and the coding.

I understand building blocks are not the most user friendly of blocks, just take a look at the organizer, its extremely difficult to find what you need and really messy.

I believe I may have one of your macros - where you can bulk replace a from a table - that would be the ideal situation bar none, except for one problem - I need the exact formatting.

When you use a table replace - it simple replaces the text -

I need the original formatting to be copied into each replacement.

Also am I able to transfer images from a table to replace in word?

If I could find a solution as such - I would transfer all my building blocks to a table.

It would be a 1000 times easier to maintain. I really hate using building blocks - the templates become bulky, and its just a real head ache.

As per original request - the reason I was trying to hard code in - is because different documents need different sets of building blocks - So I thought I would adapt the array lists.

I can't remember where the link on your site was to the table replacement but here is a similar one

https://cybertext.wordpress.com/2015...lace-routines/


Pardon me for my newbieness - in your code above where do I put the building block names is it in the function code.


Thank you for your help
J
Reply With Quote
  #4  
Old 01-03-2016, 05:47 AM
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: 3,618
gmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to behold
Default

In the macro code, if you change your building block names to match the tags in the document, the building block names go in the array.

If you want to use a table to replace the tags, then setup a two column table with no header row, the tags to find in column 1 and the formatted texts in column 2. Use manual formatting as styles may not be correctly reflected in the documents. You can then use the following macro as a custom process with the batch tool I linked to earlier:
Code:
Option Explicit

Function TableReplace(oDoc As Document) As Boolean
    On Error GoTo err_Handler
    Dim oChanges As Document
    Dim oTable As Table
    Dim oRng As Range
    Dim rFindText As Range, rReplacement As Range
    Dim i As Long
    Dim sFname As String
    Dim sAsk As String
    sFname = "C:\Path\Changes.docx" 'Change as appropriate
    Set oChanges = Documents.Open(Filename:=sFname, Visible:=False)
    Set oTable = oChanges.Tables(1)
    For i = 1 To oTable.Rows.Count
        Set oRng = oDoc.Range
        Set rFindText = oTable.Cell(i, 1).Range
        rFindText.End = rFindText.End - 1
        Set rReplacement = oTable.Cell(i, 2).Range
        rReplacement.End = rReplacement.End - 1
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            Do While .Execute(FindText:=rFindText, _
                              MatchWholeWord:=True, _
                              MatchWildcards:=False, _
                              Forward:=True, _
                              Wrap:=wdFindStop) = True
                oRng.Select
                oRng.FormattedText = rReplacement.FormattedText
                oRng.Collapse wdCollapseEnd
            Loop
        End With
    Next i
    oChanges.Close wdDoNotSaveChanges
    TableReplace = True
lbl_Exit:
    Exit Function
err_Handler:
    TableReplace = False
    Resume 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
  #5  
Old 01-03-2016, 06:04 AM
jc491's Avatar
jc491 jc491 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
VBA Novice
VBA Word -  Building Block (AutoText) - Array - Replace Text with Specific Building Blocks
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

Hi Graham,
pardon me for not reading your instructions, when code blinds me, so does my ability to read .

I wish to the high stars - Microsoft will sort out the horrendous building block organizer in the next update its long overdue.

I updated
the building block to match the search words

An error popped up here

Code:
Application.Templates(ActiveDocument.AttachedTemplate). _
                        BuildingBlockEntries(Rng.Text).Insert _
                        Where:=Rng, _
                        RichText:=True
The building blocks are in template : Building blocks - as I can see in the organiser


Test Example

Text A1 Replace Building Block - A1

Text A2 Replace Building Block - A2I will download the add in and try the second approach as well
I am indebted to you for your help

Thank you Graham

J
Reply With Quote
  #6  
Old 01-03-2016, 07:08 AM
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: 3,618
gmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to behold
Default

You will find code for inserting building blocks from other locations on my web site at http://www.gmayor.com/word_vba_examples_3.htm
__________________
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
  #7  
Old 01-03-2016, 07:22 AM
jc491's Avatar
jc491 jc491 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
VBA Novice
VBA Word -  Building Block (AutoText) - Array - Replace Text with Specific Building Blocks
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Talking

Hi Graham,


This is really mind blowing!


Words can’t describe my elation.




Programming like anything takes time and we all need to stand on the shoulders of giants.


Sometimes when you have a horrendous task as per this thread - time is what you do not have.


I used your add in and the TableReplace function you kindly wrote - to replace from a table.


It replaced images as well.


I can’t believe it!


I am so happy I asked for help - You have saved me from the drudgery of manually searching and replacing building blocks.



When a process has too many steps – there is always a bigger chance of mistakes and errors happening – as was the case with me individually running a dozen macros.



In life - all you want is SIMPLICITY. This has achieved that goal thanks to you! This is a must have in my tool box now.




Last night I just couldn’t muster the will to even begin to describe the problem, with the documents.


It is a windy rainy and extremely gray day here typical brit weather, but you have really made my day.


Thanks again for your generous generous help.


You are Solid Gold!


Thank you Graham,

J

So happy!

Have a great rest of weekend and Happy New Year!

This is Solved




I will read instructions on how to attach template
Reply With Quote
  #8  
Old 01-03-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: 3,618
gmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to behold
Default

You are welcome
__________________
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

Thread Tools
Display Modes


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 05:01 AM.


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