tags:

views:

573

answers:

3

Hi!

I need to do a simple search and replace of a string in a word document. I thought it would be pretty easy, but it's not (at least for me)

Check out this code (It takes a stream, opens the different part of the doc, searches for the string, and then it replaces it).

Problem is that only whats inside the MainDocumentPart and the FooterPart is saved. The HeaderPart is not saved. Strange...

        public static void ProcessDocument(Dictionary<string, string> properties, Stream fs)
    {
        using (WordprocessingDocument doc = WordprocessingDocument.Open(fs, true))
        {
            string docText = null;
            using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
            }
            docText = DoTheReplace(properties, docText);
            using (StreamWriter sw = new StreamWriter(doc.MainDocumentPart.GetStream(FileMode.Create)))
            {
                sw.Write(docText);
            }
            foreach (FooterPart footer in doc.MainDocumentPart.FooterParts)
            {
                string footerText = null;
                using (StreamReader sr = new StreamReader(footer.GetStream()))
                {
                    footerText = sr.ReadToEnd();
                }
                footerText = DoTheReplace(properties, footerText);
                using (StreamWriter sw = new StreamWriter(footer.GetStream(FileMode.Create)))
                {
                    sw.Write(footerText);
                }
            }
            foreach (HeaderPart header in doc.MainDocumentPart.HeaderParts)
            {
                string headerText = null;
                using (StreamReader sr = new StreamReader(header.GetStream()))
                {
                    headerText = sr.ReadToEnd();
                }
                headerText = DoTheReplace(properties, headerText);
                using (StreamWriter sw = new StreamWriter(header.GetStream(FileMode.Create)))
                {
                    sw.Write(headerText);
                }
            }
        }
    }

And yes if there are simpler ways of replacing a string in a word doc, please let me know.

Thanks for any help

Larsi

+1  A: 

When you call GetStream() on the part, I believe it returns the entire XML structure of the part, not just the text area. And Microsoft Word sometimes splits words in strange places, so a string like

Hello World!

might look like

<w:p><w:r><w:t>Hel</w:t><w:t>lo </w:t><w:t>World!</w:t></w:r><w:p>

So if you're trying to replace "Hello", it might not find it using a simple search and replace. Maybe the text in your header part is split up in a strange way like that.

Adam Sheehan
+1  A: 

I ended up using DocX. It's a great lib, and has a simple Replace function.

http://docx.codeplex.com/

Larsi
A: 

You can use Aspose.Words for .NET. It makes replace simple too.

romeok