This example demonstrates how to find and replace words in a Word document.
Finding a text in a word document is like looking for a needle in a haystack , with proper searching tool you can locate exact match to your search term.
After locating your correct match mostly there is a need of replace that text with some other text.
Find and replace technique actually replaces all the occurances of the text in the choosen word file
Selecting a word document: We first choose a source file where we need to perform a search
Find what : Search term to look for.
Replace with : Replace the searched text with this text in word document
Match Case : When searching we often need to narrow it down to case sensitive search .
Match whole word: When searching we some time need to make a exact match to exclude string with containing search term
We have several version of this representation in words ,using dropdown you can make a choice within the following
After clicking generate button it will launch word document with above choosen word version file will show then replace text in the document depending upon user input
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;
namespace OfficeComponent.Samples
{
class ReplaceandAdvancedReplaceExample : WordExampleBase
#if WEB
, IUIExample
#endif
{
public string FileName;
public string FindWhat { get; set; }
public string ReplaceWith { get; set; }
public bool MatchCase { get; set; }
public bool MatchWord { get; set; }
public ReplaceandAdvancedReplaceExample()
: base(null, null)
{
}
public ReplaceandAdvancedReplaceExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public ReplaceandAdvancedReplaceExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
#if WEB
ProcessForm();
#endif
if (string.IsNullOrWhiteSpace(FileName))
{
ShowError("Please specify the word document.");
return null;
}
if (string.IsNullOrWhiteSpace(FindWhat))
{
ShowError("Please specify the keyword to search for.");
return null;
}
if (string.IsNullOrWhiteSpace(ReplaceWith))
{
ShowError("Please specify the replacement text.");
return null;
}
// Create a new instance of PdfDocument class.
WordDocument document = new WordDocument(FileName);
document.Replace(FindWhat, ReplaceWith, MatchCase, MatchWord);
string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
// Save the document.
document.Save(fileName, SaveAsFormat);
// Close the document.
document.Close();
return fileName;
}
public override string ActionTitle
{
get
{
return "Find and Replace";
}
}
#if WEB
void ProcessForm()
{
FileName = GetPostFile("SourceFile");
}
#endif
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Word
Namespace OfficeComponent.Samples
#If WEB Then
Friend Class ReplaceandAdvancedReplaceExample
Inherits WordExampleBase
Implements IUIExample
#Else
Friend Class ReplaceandAdvancedReplaceExample
Inherits WordExampleBase
#End If
Public FileName As String
Private privateFindWhat As String
Public Property FindWhat() As String
Get
Return privateFindWhat
End Get
Set(ByVal value As String)
privateFindWhat = value
End Set
End Property
Private privateReplaceWith As String
Public Property ReplaceWith() As String
Get
Return privateReplaceWith
End Get
Set(ByVal value As String)
privateReplaceWith = value
End Set
End Property
Private privateMatchCase As Boolean
Public Property MatchCase() As Boolean
Get
Return privateMatchCase
End Get
Set(ByVal value As Boolean)
privateMatchCase = value
End Set
End Property
Private privateMatchWord As Boolean
Public Property MatchWord() As Boolean
Get
Return privateMatchWord
End Get
Set(ByVal value As Boolean)
privateMatchWord = value
End Set
End Property
Public Sub New()
MyBase.New(Nothing, Nothing)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
#If WEB Then
ProcessForm()
#End If
If String.IsNullOrWhiteSpace(FileName) Then
ShowError("Please specify the word document.")
Return Nothing
End If
If String.IsNullOrWhiteSpace(FindWhat) Then
ShowError("Please specify the keyword to search for.")
Return Nothing
End If
If String.IsNullOrWhiteSpace(ReplaceWith) Then
ShowError("Please specify the replacement text.")
Return Nothing
End If
' Create a new instance of PdfDocument class.
Dim document As New WordDocument(FileName)
document.Replace(FindWhat, ReplaceWith, MatchCase, MatchWord)
'INSTANT VB NOTE: The variable fileName was renamed since Visual Basic does not handle local variables named the same as class members well:
Dim fileName_Renamed As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
' Save the document.
document.Save(fileName_Renamed, SaveAsFormat)
' Close the document.
document.Close()
Return fileName_Renamed
End Function
Public Overrides ReadOnly Property ActionTitle() As String
Get
Return "Find and Replace"
End Get
End Property
#If WEB Then
Private Sub ProcessForm()
FileName = GetPostFile("SourceFile")
End Sub
#End If
End Class
End Namespace