Microsoft Word / Replacing text

Posted on 2006-04-20
Last Modified: 2008-02-01
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]

Question by:macros14
    1 Comment
    LVL 15

    Accepted Solution

    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;

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now