View Single Post
 
Old 01-03-2016, 05:47 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
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