I've made some minor revisions to the previous code, so both macros should now run without error.
Regarding the formatting, unless you use a 4-column table, there's really no reliable way of knowing whether the formats found in the cells are important to the Find/Replace operation. How would one tell, for example, whether the absence of a bold font means
the content to be found is not to be bolded, or that bolding is inconsequential? Separate columns to designate the applicability of font attributes for both the Find and the Replace would be required. the following macro envisages such a table, where the presence
of content in the 3rd & 4th columns dictate whether the font format is to be taken into account for the Find (column 3) or applied to the Replace (column 4); if the cell has content, the formatting there is applied to the Find and/or Replace, as applicable.
Sub ReplaceFromTableList()
Application.ScreenUpdating = False
Dim Doc As Document, Rng As Range, i As Long, Tbl As Table
Dim sFname As String, StrFnd As String, StrRep As String
sFname = "C:\Users\Win7\Desktop\macro.docx" 'The table document
Set Rng = ActiveDocument.Range
Set Doc = Documents.Open(FileName:=sFname, Visible:=False)
Set Tbl = Doc.Tables(1)
With Rng.Find
.MatchWildcards = True
.Wrap = wdFindContinue
For i = 1 To Tbl.Rows.Count
.ClearFormatting
.Replacement.ClearFormatting
StrFnd = Split(Tbl.Cell(i, 1).Range.Text, vbCr)(0)
StrRep = Split(Tbl.Cell(i, 2).Range.Text, vbCr)(0)
.Text = StrFnd
.Replacement.Text = StrRep
If Split(Tbl.Cell(i, 3).Range.Text, vbCr)(0) <> "" Then
Set FntFnd = Tbl.Cell(i, 4).Range.Characters(1).Font
.Font.Bold = FntFnd.Bold
.Font.Italic = FntFnd.Italic
.Font.Underline = FntFnd.Underline
End If
If Split(Tbl.Cell(i, 4).Range.Text, vbCr)(0) <> "" Then
Set FntRep = Tbl.Cell(i, 4).Range.Characters(1).Font
With .Replacement
.Font.Bold = FntRep.Bold
.Font.Italic = FntRep.Italic
.Font.Underline = FntRep.Underline
End With
End If
.Execute Replace:=wdReplaceAll
.Text = LCase(StrFnd)
.Replacement.Text = LCase(StrRep)
.Execute Replace:=wdReplaceAll
Next i
End With
Doc.Close wdDoNotSaveChanges
Application.ScreenUpdating = True
End Sub