View Single Post
 
Old 01-05-2016, 11:36 AM
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 - Find Formatted Text Version Only - Replace From Table

Hello to all,

this Tuesday. I hope every one is doing great today!

Thank you for all the help provided these previous past few days.


I have been using Graham's fantastic Replace from Table VBA Macro.

The code below - does a fantastic job. It searches and replaces all the text in column 1 - and replaces with text found in column 2.

I was hoping to simply extend the code.

I don't know if what I am asking for is possible?

As I have been fiddling about with it and have not been able to make heads or tails with the solution, nothing happens when I add new code on to it.

No doubt - I am doing it wrong

Is it possible to find the exact formatted text in column A of the table, ignoring all other types of formatting

Here is an example

http://tinypic.com/r/258cyf6/9



Code:
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
            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
This table solution for me is such a great tool - if I could just abuse the kindness of an advanced practitioner to see if if it's possible to do the impossible (which it is to me at this moment) - that is find the exact formatted text.

I will not lie this is a complex task, what seems simple enough in theory is not when it comes to coding.


Thank you so very much for taking the time to look over this.

I really really do appreciate all the help from the great individuals here

I would be lost without the kind help

thank you

J
Reply With Quote