Find text in textbox in word document
I want c# code to replace text inside textbox in word document.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
CrishRank #1,487 · 1.1K reputationPosted May 16, 2010, 4:51 AM EDT
use below link for your solution
http://blogs.techrepublic.com.com/howdoi/?p=190
Don't forget to Mark Do you like this Answer that solved your problem!
Ahmed Ayman Yassin11 reputationPosted May 16, 2010, 7:54 AM EDT
Ahmed Ayman Yassin11 reputationPosted May 17, 2010, 9:57 AM EDT
Dim word As New Word.Application
Dim doc As Word.Document
word.Documents.Open("c:\abc.doc")
word.Visible = True
doc = word.ActiveDocument
FindandReplace(doc, "vbcode", "csharp code")
Sub FindandReplace(ByVal doc As Word.Document, ByVal Findtext As String, ByVal Replacetext As String)
Dim myStoryRange = doc.Range()
'First search the main document using the Selection
With myStoryRange.Find
.Text = Findtext
.Replacement.Text = Replacetext
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
'Now search all other stories using Ranges
For Each myStoryRange In doc.StoryRanges
If myStoryRange.StoryType <> Word.WdStoryType.wdMainTextStory Then
With myStoryRange.Find
.Text = Findtext
.Replacement.Text = Replacetext
.Wrap = Word.WdFindWrap.wdFindContinue
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = Findtext
.Replacement.Text = Replacetext
.Wrap = Word.WdFindWrap.wdFindContinue
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
Loop
End If
Next myStoryRange
End Sub
Bala murugan2 reputationPosted Jul 21, 2010, 3:44 PM EDT
I have got similar issue in C# while editing the word doc. Can you please share your fix for me?
Thanks,
Bala.