View Single Post
 
Old 03-18-2017, 11:40 PM
Gowri Gowri is offline Windows 8 Office for Mac 2011
Novice
 
Join Date: Mar 2017
Posts: 4
Gowri is on a distinguished road
Default

Here is my code. Please check it.

Code:
Dim oWordApp As Object, oWordDoc As Object, rngStory As Object
    Dim sFolder As String, strFilePattern As String
    Dim strFileName As String, sFileName As String
 Dim i As Long, j As Long, FindWord As String, Ans As String, FoundWord As String, ReplaceAns As String
Call CopyingAFile
        Set oWordApp = CreateObject("Word.Application")
    oWordApp.Visible = True
        Set oWordDoc = oWordApp.Documents.Open("E:\Output\Original.docx")
For i = 2 To 40
            FindWord = Sheets("LookUp").Range("B" & i).Value
            If FindWord <> vbNullString Then
            FoundWord = FindWord
       Ans = Sheets("Answers").Range("C" & i).Value
       If Ans <> vbNullString Then
       ReplaceAns = Ans
        For Each rngStory In oWordDoc.StoryRanges
            With rngStory.Find
                .Text = FoundWord
                .Replacement.Text = ReplaceAns
                .Wrap = wdFindContinue
                .Execute Replace:=wdReplaceAll
            End With
        Next
        End If
End If
Next i
        oWordDoc.Close SaveChanges:=True
        MsgBox "File Saved"
    oWordApp.Quit
    MsgBox " Word File Modified"
    Set oWordApp = Nothing: Set oWordDoc = Nothing
End Sub

If i run the code the cell value in excel is ·Firstline
·Secondline
replaced in word as ·Firstline ·Seconline . But my requirement is to replace in word as ·Firstline
·Seconline

Last edited by macropod; 03-19-2017 at 12:06 AM. Reason: Added code tags to restore formatting
Reply With Quote