Click here to Skip to main content
15,894,630 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dim sFind As String
Dim sReplace As String
sFind = "{{" & "" & "}}"
sReplace = "Naresh sharma"
Dim oWord As Word.Application
Dim oDoc As Word.Document
' Dim oWord As Object
'Dim oDoc As Object

Try

oWord = CreateObject("Word.Application")
'soWord.Visible = True
oDoc = oWord.Documents.Add

oDoc = oWord.Documents.Open(TextBox1.Text)
oDoc.Activate()
Dim myStoryRange As Microsoft.Office.Interop.Word.Range
For Each myStoryRange In oDoc.StoryRanges
Do
If myStoryRange.StoryType = Word.WdStoryType.wdMainTextStory Or myStoryRange.StoryType = Word.WdStoryType.wdTextFrameStory Then
With myStoryRange.Find
.Text = sFind
.Replacement.Text = sReplace
.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
.Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
End With

End If
myStoryRange = myStoryRange.NextStoryRange
Loop Until myStoryRange Is Nothing
Next myStoryRange
oDoc.Save()
'System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc)
'Marshal.ReleaseComObject(oDoc)
''oDoc.Close()

'oDoc = Nothing
'Marshal.ReleaseComObject(oWord)
'oWord.Application.Quit()
'oWord = Nothing
If oDoc IsNot Nothing Then
Marshal.ReleaseComObject(oDoc)
oDoc = Nothing
End If
If oWord IsNot Nothing Then
Marshal.ReleaseComObject(oWord)
oWord = Nothing
End If
Catch ex As ExternalException
MessageBox.Show(ex.ErrorCode, "Error code no")
MessageBox.Show(ex.StackTrace)
Catch Ex As Exception



End Try
Posted
Updated 8-Nov-11 18:57pm
v2

1 solution

Other One Solution that Simply we can find "{{" in the word and after that replace it. there is no long process
 
Share this answer
 
v2
Comments
RICKY_Xxx 9-Nov-11 2:34am    
but in .doc file there are {{ come two or more time e.g {{var1}}{{var1}}
so for that what will i do?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900