Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

Get replace normal text from word document in C#

object o = Missing.Value;
object oFalse = false;
object oTrue = true;

Word._Application app = null;
Word.Documents docs = null;
Word.Document doc = null;

object path = @"C:pathfile.doc";

try
{
    app = new Word.Application();
    app.Visible = false;
    app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

    docs = app.Documents;
    doc = docs.Open(ref path, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
    doc.Activate();

    foreach (Word.Range range in doc.StoryRanges)
    {
        Word.Find find = range.Find;
        object findText = "[Company_Name]";
        object replacText = "Sacramento";
        object replace = Word.WdReplace.wdReplaceAll;
        object findWrap = Word.WdFindWrap.wdFindContinue;

        find.Execute(ref findText, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref replacText,
            ref replace, ref o, ref o, ref o, ref o);

        Marshal.FinalReleaseComObject(find);
        Marshal.FinalReleaseComObject(range);
    }

    doc.Save();
    ((Word._Document)doc).Close(ref o, ref o, ref o);
    app.Quit(ref o, ref o, ref o);
}
finally
{
    if (doc != null)
        Marshal.FinalReleaseComObject(doc);

    if (docs != null)
        Marshal.FinalReleaseComObject(docs);

    if (app != null)
        Marshal.FinalReleaseComObject(app);
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Get #replace #normal #text #word #document
ADD COMMENT
Topic
Name
1+4 =