I have set up some code to search and replace data in my word files based on excel columns. IT works ok... (although I get a small erroe at the end.. Anyway, what it does not do, is preserve the formatting in the excel spread sheet.
For example, I have colored text / italics in my excel spreadsheet, that just gets put into the word file with the formatting of the previous text (in word). Any help would be appreciated?
Here is my current code:
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim row As Integer
Dim tString As String, tRange As Word.Range
Dim findtext As Long, r As Long
Dim totalrows As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\replace\replace.doc")
Set wrdSelection = wrdApp.Selection
totalrows = ActiveSheet.UsedRange.Rows.Count
With wrdSelection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For row = 2 To totalrows Step 1
wrdSelection.Find.Text = ActiveSheet.Cells(row, 1).Value
wrdSelection.Find.Replacement.Text = ActiveSheet.Cells(row, 2).Value
wrdSelection.Find.Execute Replace:=wdReplaceAll
Next row
wrdDoc.Save
End Sub
For example, I have colored text / italics in my excel spreadsheet, that just gets put into the word file with the formatting of the previous text (in word). Any help would be appreciated?
Here is my current code:
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim row As Integer
Dim tString As String, tRange As Word.Range
Dim findtext As Long, r As Long
Dim totalrows As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\replace\replace.doc")
Set wrdSelection = wrdApp.Selection
totalrows = ActiveSheet.UsedRange.Rows.Count
With wrdSelection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For row = 2 To totalrows Step 1
wrdSelection.Find.Text = ActiveSheet.Cells(row, 1).Value
wrdSelection.Find.Replacement.Text = ActiveSheet.Cells(row, 2).Value
wrdSelection.Find.Execute Replace:=wdReplaceAll
Next row
wrdDoc.Save
End Sub