This is an alternate for word document object using the System.IO.Packaging in VS.NET 2005.

You can able to create, open a document using XML and make any type of changes there, just like you do word object (letterApp = new Word.ApplicationClass ();)

Just add the reference to windowsbase.dll from directory "c:\Program Files\Reference Assemblies\Microsoft".

This will work only for MS Word 2007. It does not support backward compatability.

Open a C# project, add the reference to windowsbase.dll and the create new form and place the below code. pass the file (created in word 2007 extension should be .docx) , search string and replacement string. Try to run the application the old text will be replaced by the new one.


 private bool SearchDocx(string strfile, string searchFor,string strReplace)

                using (Package myPackage = Package.Open(strfile, FileMode.Open, FileAccess.ReadWrite))
                        PackagePart documentPart = null;

                        const string documentRelationshipType =

                        foreach (PackageRelationship relationship in myPackage.GetRelationshipsByType(documentRelationshipType))
                            documentPart = myPackage.GetPart(PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri));
                        XmlDocument doc = new XmlDocument();
                        XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                        nsmgr.AddNamespace("w", "");

                        string StrQuery = "//w:t[text()='" + searchFor + "']";

                        bool bolReplace=false;

                        foreach (XmlNode sdtNode in doc.SelectNodes(StrQuery, nsmgr))
                            sdtNode.InnerText = strReplace;
                            bolReplace = true;
                        documentPart.GetStream().Seek(0, SeekOrigin.Begin);

                        if (bolReplace == false)
                        { return false; }
                        { return true; }

                MessageBox.Show("Could not Open file: " + strfile);
                return false;



Like it on Facebook, Tweet it or share this article on other bookmarking websites.