Microsoft Word / Replacing text

I just got a project to create a webpage that when a user clicks on a button, the webpage will replace some pieces of text in a word document.  The word document has some pictures, but here is a snippet of the layout.  All items in brackets will need to be replaced with data from a dataset.  Is it possible to stream the document and just use the .net string replace function?  But would I lose the word documents formatting or style??  Or what is the best way??  Currently this is a word document and not a word template.



Dear [AdresseeName]

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You will need to use the Word API that comes with .Net

Here is a quick C# script on how to read the word document. You will then need to update as required and then save it again. Remember to reference the Word API in your references.

public System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
public bool ReadWordDocument(string filePath)
            Microsoft.Office.Interop.Word.Application wap = new Microsoft.Office.Interop.Word.ApplicationClass();
            object missing = System.Reflection.Missing.Value;
            object fileName = System.Environment.CurrentDirectory + "\\" + filePath;
            object objFalse = false;
            object objTrue = true;
                Microsoft.Office.Interop.Word.Document wdoc = wap.Documents.Open(ref fileName, ref objFalse, ref objTrue,
                 ref objFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue,
                ref missing, ref missing, ref missing, ref missing);


                Contents = data.GetData(System.Windows.Forms.DataFormats.Html).ToString();
                Contents = Contents.Replace("Â", "").Replace("…", "...");
                wap.Application.Quit(ref missing, ref missing, ref missing);
                return true;
            catch (Exception exc)
                wap.Application.Quit(ref missing, ref missing, ref missing);
                return false;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.