Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 01-05-2016, 10:12 PM
gmayor's Avatar
gmayor gmayor is offline VBA Word - Find Formatted Text Version Only  - Replace From Table Windows 10 VBA Word - Find Formatted Text Version Only  - Replace From Table 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 might be possible, provided all the text that you were looking for was formatted in the same way i.e if the text in column 1 of the table was formatted as 16 point bold Times New Roman, then that should be doable without the use of extra columns. Try the following (I have commented out the unnecessary lines):
Code:
Option Explicit

Sub ReplaceFromTableWithFormatting()


' Slightly Tweaked from Graham Mayor's TableReplace Function
' And  Doug Robbins
' Bulk Find & Replace From Table
'

Dim oChanges As Document, oDoc 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

    '==================DOCUMENT LOCATION


    sFname = "C:\Users\Desktop\TableReplace.docx"
    
    Set oDoc = ActiveDocument
    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
        Selection.HomeKey wdStory

        With oRng.Find
            ' Find only the exact formatted version as displayed in Column 1
            '.Format = True        ' This may not be correct?
            '.ClearFormatting
            '.Replacement.ClearFormatting
            'set the variety of formats to look for
            .Font.Name = rFindText.Font.Name
            .Font.Bold = rFindText.Font.Bold
            .Font.Size = rFindText.Font.Size
            .Font.Italic = rFindText.Font.Italic
            .Font.Underline = rFindText.Font.Underline
            .Font.ColorIndex = rFindText.Font.ColorIndex
            
            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
End Sub
__________________
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Word - Find Formatted Text Version Only  - Replace From Table VBA Table – Search All Tables - Find & Replace Text in Table Cell With Specific Background Color jc491 Word VBA 8 09-30-2015 06:10 AM
Macro to keep formatted form fields after mail merge or replace text with formatted form fields jer85 Word VBA 2 04-05-2015 10:00 PM
VBA Word - Find Formatted Text Version Only  - Replace From Table How can I paste text that's not formatted as a table into a new table? WaltR Word 2 10-11-2014 03:16 PM
Word VBA Find Table Text Shading Colour and replace with another QA_Compliance_Advisor Word VBA 10 09-19-2014 08:36 AM
VBA Word - Find Formatted Text Version Only  - Replace From Table Word VBA Macro to Find and Replace based on the Alt Text of an Image bennymc Word VBA 1 01-27-2014 04:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:48 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